我在 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 中时,它工作正常,但我想将此数据存储在另一个类中
最佳答案
你需要打开它
if let deviceToken = delegate.deviceTokenToPass
{
var currentInstallation = PFInstallation.currentInstallation()
currentInstallation.setDeviceTokenFromData(deviceToken)
currentInstallation.channels = ["global"]
currentInstallation.saveInBackground()
}
关于ios - 将 deviceToken 从 AppDelegate 传递到 swift 中的另一个类时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881997/