Skip to content

Instantly share code, notes, and snippets.

@troystribling
Created November 23, 2014 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troystribling/cae8be0244320db87854 to your computer and use it in GitHub Desktop.
Save troystribling/cae8be0244320db87854 to your computer and use it in GitHub Desktop.
UIAlertControllerExtensions and Notification handler
import UIKit
class Notify {
class func resetEventCount() {
eventCount = 0;
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
}
class func withMessage(message:String) {
if UIApplication.sharedApplication().applicationState != .Active && self.getEnabled(){
eventCount += 1
let localNotification = UILocalNotification()
localNotification.alertBody = message
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.applicationIconBadgeNumber = eventCount
UIApplication.sharedApplication().presentLocalNotificationNow(localNotification)
}
}
class func setEnable(enabled:Bool = true) {
NSUserDefaults.standardUserDefaults().setBool(enabled, forKey:"notifications")
}
class func getEnabled() -> Bool {
return NSUserDefaults.standardUserDefaults().boolForKey("notifications")
}
}
var eventCount = 0
import UIKit
extension UIAlertController {
class func alertOnError(error:NSError, handler:((UIAlertAction!) -> Void)? = nil) -> UIAlertController {
var alert = UIAlertController(title: "Error", message:error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:handler))
return alert
}
class func alertOnErrorWithMessage(message:String, handler:((UIAlertAction!) -> Void)? = nil) -> UIAlertController {
var alert = UIAlertController(title: "Error", message:message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:handler))
return alert
}
class func alertWithMessage(message:String, handler:((UIAlertAction!) -> Void)? = nil) -> UIAlertController {
var alert = UIAlertController(title: "Alert", message:message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:handler))
return alert
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment