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

时间:2016-06-17 12:49:35

标签: 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但我希望将此数据存储在另一个类中时,它的工作正常吗

1 个答案:

答案 0 :(得分:0)

你需要打开它

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

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

    currentInstallation.saveInBackground()
}