原始问题:
我的功能看起来是这样的。问题是它在我的设备上工作,而对其他设备不起作用。我不知道我做错了什么
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作为参数。我没有想到的是,设备名称可以包含空白空间。而且,因为我自己的设备没有空白,所以它可以工作,但在其他设备上不起作用。我真笨,但我会从中吸取教训的。感谢所有人
发布于 2017-03-25 13:23:10
你每次都可以拯救它。不要检查它是否存在。
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")
}https://stackoverflow.com/questions/43016644
复制相似问题