Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ios Swift 3应用程序在didRegisterForRemoteNotificationsWithDeviceToken函数中崩溃_Ios_Swift3_Notifications - Fatal编程技术网

ios Swift 3应用程序在didRegisterForRemoteNotificationsWithDeviceToken函数中崩溃

ios Swift 3应用程序在didRegisterForRemoteNotificationsWithDeviceToken函数中崩溃,ios,swift3,notifications,Ios,Swift3,Notifications,原始问题: 我的函数是这样的。问题是它在我的设备上工作,但在其他设备上不工作。我不知道我做错了什么 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { // Convert token to string let deviceTokenString = deviceToken.reduce("", {

原始问题:

我的函数是这样的。问题是它在我的设备上工作,但在其他设备上不工作。我不知道我做错了什么

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Convert token to string
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})

    let preferences = UserDefaults.standard

    let savedtoken = preferences.string(forKey: "token")


    if(savedtoken != deviceTokenString)
    {

        var request = URLRequest(url: URL(string: "myurl")!)
        request.httpMethod = "POST"
        let session = URLSession.shared

        session.dataTask(with: request) {data, response, err in
            print("Entered the completionHandler")
            }.resume()

        preferences.set(deviceTokenString, forKey: "token")
        let didSave = preferences.synchronize()
    }
}
问题是:

抱歉,伙计们犯了这样一个愚蠢的错误:(在“myurl”中,我将UIDevice.current.name作为参数发送。我没有想到的是,设备名称可以包含空格。因为我自己的设备名称中没有空格,所以它可以工作,但在其他设备上不能。
我真傻,但我会从中吸取教训的。谢谢大家。你每次都可以保存它。不要检查它是否存在

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print(deviceTokenString)
        UserDefaults.standard.setObject(deviceTokenString, forKey: "device_token")
    }

你每次都可以保存它。不要检查它是否存在

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print(deviceTokenString)
        UserDefaults.standard.setObject(deviceTokenString, forKey: "device_token")
    }

异常消息是什么,它在哪一行崩溃?此行将返回nil initially let savedtoken=preferences.string(forKey:“token”),这可能导致崩溃…用空字符串初始化它。异常消息是什么,它在哪一行崩溃?此行将返回nil initially let savedtoken=preferences.string(forKey:“token”)可能导致崩溃…使用空字符串初始化它切勿使用
setValue:forKey
UserDefaults
使用,除非您可以解释明确需要KVC的原因。专用方法是
setObject:forKey
。是的,您应该。和
synchronize()在这种情况下,也不需要使用
。框架会定期更新数据库。我建议在userdefault上使用synchronize()on ever write操作以确保您的更改已保存。无需使用UserDefaults.standard.synchronize(),它还增加了内存和时间,除非您能解释为什么明确需要KVC,否则,使用
setValue:forKey
UserDefaults
时也可以使用
setObject:forKey
。是的,您应该。和
synchronize()在这种情况下也不需要使用
。框架会定期更新数据库。我建议在userdefault上使用synchronize()on ever write操作以确保您的更改已保存。不需要使用userdefault.standard.synchronize(),它还会增加内存和时间