在 Swift 3 中,申请为 ios 8.1 注册通知
Posted
技术标签:
【中文标题】在 Swift 3 中,申请为 ios 8.1 注册通知【英文标题】:In Swift 3, Application filed to register notification for ios 8.1 【发布时间】:2017-09-20 04:27:48 【问题描述】:以下是应用程序抛出错误注册通知时的错误详情:
<EXPR>:3:1: error: expected member name or constructor call after type name
Error
^
<EXPR>:3:1: note: use '.self' to reference the type object
Error
^
.self
这是在 ios 10.1 中成功运行的代码
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
print("Failed to register Notification Error = ", Error)
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
print("Device Token = ", Data)
func registerForRemoteNotification()
if #available(iOS 10.0, *)
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge])
(granted, error) in
if error == nil
UIApplication.shared.registerForRemoteNotifications()
else
if #available(iOS 8.0, *)
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
else
UIApplication.shared.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
if let dicAps = notification.request.content.userInfo["aps"] as? NSDictionary
AppUtilities.showAlertWithMessage(title: APP_TITLE as NSString, message: "\(dicAps.object(forKey: "msg")!)" as NSString)
completionHandler([.alert, .badge, .sound])
//Called to let your app know which action was selected by the user for a given notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
print("User Info = ",response.notification.request.content.userInfo)
我正在尝试实现一个可以在 iOS 8 到 10.1 上运行的应用程序。它在 iOS 10.1.1 的 iPhone 5s 上运行良好。但是当我在iOS 8.1的模拟器中运行该应用程序时,它总是会抛出上述错误,如果我遗漏了什么,请纠正我。
谢谢。
【问题讨论】:
哪些行导致错误? @JK Patel:推送通知在模拟器中不起作用,***.com/questions/21741259/… @rmaddy 我在模拟器中运行应用程序时从 didFailToRegisterForRemoteNotificationsWithError 收到错误。 【参考方案1】:很抱歉,您需要找到一些硬件来测试此功能。
推送通知在模拟器中不可用。它们需要来自 iTunes Connect 的配置文件,因此需要安装在设备上。这也意味着你可能必须被苹果 iPhone 开发者计划接纳并支付 99 美元。
来源:How can I test Apple Push Notification Service without an iPhone?
【讨论】:
我正在查看苹果开发者文档以了解此错误。以上是关于在 Swift 3 中,申请为 ios 8.1 注册通知的主要内容,如果未能解决你的问题,请参考以下文章
Xcode 8.1 Swift 3 错误:无法将“String”类型的值转换为预期的参数类型“UnsafeMutableRawPointer”
我编译 Swift 2.3 项目并在 Xcode 8.1 上有这个问题
ios 8.1:类型“ViewController”不符合协议“UICollectionViewDataSource”