ios - 将 deviceToken 从 AppDelegate 传递到 swift 中的另一个类时出错

标签 ios xcode swift parse-platform swift2

我在 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/

相关文章:

ios - 使用 Reactive Cocoa 的按钮操作

android - 是否有任何理由使用 Google GCM 与 Apple 消息传递来推送通知?

ios - 自定义 CIKernel 和 iOS7

ios - 从命令行终端使用位码启用和位码禁用为设备和通用配置创建 iOS 框架

swift - 如何将 PFUser 转换为字符串?

Swift 枚举关联值

IOS取消本地通知

关闭优化时 C 库崩溃

ios - iOS 中的相对路径

ios - DrawView保存组合图(相乘)