based on this answer I modified my code. Is this the correct approach now?
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
var userId = "001" //I just use it as USER number 1
let token = deviceToken.hexString()
var postBody = NSString(format: "user=%@&token=%@", userId, token)
var endBody = NSURL(string: "http://www.myServer.com/api/v1.0/register.php")
var request = NSMutableURLRequest(URL: endBody!, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 30.0)
request.HTTPMethod = "POST";
request.HTTPBody = postBody.dataUsingEncoding(NSUTF8StringEncoding)
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
var response: NSURLResponse?
var error: NSError?
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {
(response, data, error) -> Void in
if data != nil {
println("data: \(response)")
} else {
println("failed: \(error.localizedDescription)")
}
}
}
extension NSData {
func hexString() -> String {
// "Array" of all bytes:
let bytes = UnsafeBufferPointer<UInt8>(start: UnsafePointer(self.bytes), count:self.length)
// Array of hex strings, one for each byte:
let hexBytes = map(bytes) { String(format: "%02hhx", $0) }
// Concatenate all hex strings:
return "".join(hexBytes)
}
}