Michele  Coladangelo Michele Coladangelo - 2 months ago 11
iOS Question

Ask for Permission for Remote Notifications in iOS 8

I'm adding remote notifications with

ArenaDeamons
, but I'm getting an error (
MY APP KEY
is my private key):

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
BDArenaConnector.initializeWithAppKey("MY APP KEY", runInSandboxEnvironment: true)
BDArenaConnector.getInstance().requestAuth()
// register for remote notifications
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.settingsForTypes((UIUserNotificationTypeSound|UIUserNotificationTypeAlert|UIUserNotificationTypeBadge), categories: nil))
UIApplication.sharedApplication().registerForRemoteNotifications()
BDArenaConnector.getInstance().pushConnector.feedbackServiceDidFinishLaunchingWithOptions(launchOptions)
return true

UINavigationBar.appearance().barTintColor = UIColor.redColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UIBarButtonItem.appearance().tintColor = UIColor.blueColor()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()

return true
}


Error1:
use of unresolved identifier 'UIUserNotificationTypeBadge'


Error2:
use of unresolved identifier 'UIUserNotificationTypeSound'


Error3:
use of unresolved identifier 'UIUserNotificationTypeAlert'


I also have some errors here:

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("UIApplication : didFailToRegisterForRemoteNotificationsWithError : \(error.localizedDescription)")
var alert: UIAlertController = UIAlertController(title: "did Fail To Register For Remote Notifications", message: error.localizedDescription(), preferredStyle: UIAlertControllerStyleAlert)
var okAction: UIAlertAction = UIAlertAction.actionWithTitle("Ok!", style: UIAlertActionStyleDefault, handler: { (action: UIAlertAction) in
})
alert.addAction(okAction)
self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil)
}


Error1:
use of unresolved identifier 'UIalertcontrollerstylealert'


Error2:
use of unresolved identifier 'UIalertactionstyledefault'


How Can I fix it? The app is in Swift 2.

Answer

For ios 8 and above, you can use registerUserNotificationSettings, try this code, i have not tested it but, it will help you for sure.

let types = UIUserNotificationType.Badge.union(UIUserNotificationType.Alert).union(UIUserNotificationType.Sound)
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: nil))
UIApplication.sharedApplication().registerForRemoteNotifications()

Fix for Error 3

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print("UIApplication : didFailToRegisterForRemoteNotificationsWithError : \(error.localizedDescription)")
    let alert: UIAlertController = UIAlertController(title: "did Fail To Register For Remote Notifications", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert)
    let okAction: UIAlertAction = UIAlertAction(title: "Ok!", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction) in
    })
    alert.addAction(okAction)
    self.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil)
}
Comments