【发布时间】:2016-10-11 07:44:37
【问题描述】:
我正在尝试将我在didRegisterForRemoteNotificationsWithDeviceToken 中获得的令牌发送到服务器。但我在发送时出错:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (Foundation._SwiftNSData)。对于请求,我使用 Alamofire 框架。我的代码:
func signUp(withToken token: Data, completion: (Error) -> Void) {
let parameters: Parameters = ["registration_id": token]
print("token = \(token)")
Alamofire.request(baseUrl + signUpPath, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON(completionHandler: {response in
})
}
Print 告诉我:token = 32 bytes。有什么建议么?在将Data 类型发送到服务器之前,也许我需要一些额外的步骤?
更新
我尝试将令牌转换为NSString 类型但得到nil
let tokenNSString: NSString? = NSString(data: token, encoding: String.Encoding.utf8.rawValue)
print("nsstrgin from token = \(tokenNSString)")
【问题讨论】:
-
您可以将 NSData 转换为 String 并将其发送到服务器(stackoverflow.com/questions/4994302/…)。如果您想将其作为数据发送到服务器,请尝试多部分上传(stackoverflow.com/questions/26121827/…)。
-
@vishnuvarthan 请看我的更新。
-
用它来转换成字符串。 stackoverflow.com/questions/9372815/…
标签: ios apple-push-notifications token swift3 alamofire