Created
November 23, 2014 16:53
-
-
Save troystribling/cae8be0244320db87854 to your computer and use it in GitHub Desktop.
UIAlertControllerExtensions and Notification handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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