【问题标题】:Error when passing deviceToken from AppDelegate to another class in swift将 deviceToken 从 AppDelegate 快速传递到另一个类时出错
【发布时间】: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


【解决方案1】:

你需要打开它

if let deviceToken = delegate.deviceTokenToPass
{
    var currentInstallation = PFInstallation.currentInstallation()

    currentInstallation.setDeviceTokenFromData(deviceToken)
    currentInstallation.channels = ["global"]

    currentInstallation.saveInBackground()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多