[英]Ask for Permission for Remote Notifications in iOS 8
我正在使用ArenaDeamons
添加远程通知,但出现错误( MY APP 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
}
错误1: use of unresolved identifier 'UIUserNotificationTypeBadge'
错误2: use of unresolved identifier 'UIUserNotificationTypeSound'
无法use of unresolved identifier 'UIUserNotificationTypeSound'
错误3: use of unresolved identifier 'UIUserNotificationTypeAlert'
无法use of unresolved identifier 'UIUserNotificationTypeAlert'
我在这里也有一些错误:
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)
}
错误1: use of unresolved identifier 'UIalertcontrollerstylealert'
错误2: use of unresolved identifier 'UIalertactionstyledefault'
无法use of unresolved identifier 'UIalertactionstyledefault'
我该如何解决? 该应用程序在Swift 2中。
您需要使用UIAlertActionStyle.Default
和UIAlertControllerStyle.Alert
。
对于ios 8及更高版本,您可以使用registerUserNotificationSettings,尝试以下代码,但我尚未对其进行测试,但是它将肯定会对您有所帮助。
let types = UIUserNotificationType.Badge.union(UIUserNotificationType.Alert).union(UIUserNotificationType.Sound)
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: nil))
UIApplication.sharedApplication().registerForRemoteNotifications()
修复错误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)
}
部分固定。 现在我只有两个错误。 感谢Prabhu.Somasundaram。
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)
}
错误1:无效使用'()'来调用非函数类型'String'的值
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
BDArenaConnector.getInstance().pushConnector.feedbackServiceDidReceiveRemoteNotification(userInfo)
// handle push
BDArenaConnector.getInstance().pushConnector.handlePush(userInfo, withPresentingController: self.window!.rootViewController, withActionButtonHandler: {
}, withCancelButtonHandler: {
})
}
错误2:未展开可选类型'UIWindow'的值; 你是说用'!' 要么 '?'?
我该如何解决?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.