【发布时间】:2016-06-17 12:49:34
【问题描述】:
我在 AppDelegate 中创建这个变量来存储 deviceToken,然后将它传递给另一个类
var deviceTokenToPass:NSData?
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
deviceTokenToPass = deviceToken
}
在另一个类中,我试图将 deviceToken 传递给 Parse.com,但在 deviceToken 的传递值行中出现异常
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
let deviceToken = delegate.deviceTokenToPass
var currentInstallation = PFInstallation.currentInstallation()
currentInstallation.setDeviceTokenFromData(deviceToken) // Exception in this line
currentInstallation.channels = ["global"]
currentInstallation.saveInBackground()
当我将它放在 AppDelegate 中时,它的工作原理相同,但我想将此数据存储在另一个类中
【问题讨论】:
-
你遇到什么样的错误?
-
异常原因是什么?
-
您必须确保在调用该函数时,收到了deviceToken(调用了
didRegisterForRemoteNotificationsWithDeviceToken)。 -
@JigarTarsariya 它给了 null 但现在它确实有效我没有改变任何东西! :D
-
为什么将设备令牌保存在其他类中?令牌是异步到达的,因此当您尝试访问它时可能不会设置 deviceToken。从应用委托更新 PFInstallation 更简单。
标签: ios xcode swift parse-platform swift2