これは、Swift3以降ではreduceを使用するとはるかに簡単です。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.reduce("") { $0 + String(format: "%02x", $1) }
DispatchQueue.global(qos: .background).async {
let url = URL(string: "https://example.com/myApp/apns.php")!
var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: [
"token" : token,
"ios" : UIDevice.current.systemVersion,
"languages" : Locale.preferredLanguages.joined(separator: ", ")
])
URLSession.shared.dataTask(with: request).resume()
}
}