Can';t urlencode deviceToken(Swift)

Can';t urlencode deviceToken(Swift),swift,ios8,nsdata,nsurl,Swift,Ios8,Nsdata,Nsurl,我想使用NSURL会话将deviceToken发送到我的服务器,但每次都会崩溃。我试图找到一种将数据对象(deviceToken)转换为NSString的方法,但到目前为止没有成功 错误:“致命错误:在展开可选值时意外发现nil” 非常感谢您的帮助。这是我的密码 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData!)

我想使用NSURL会话将deviceToken发送到我的服务器,但每次都会崩溃。我试图找到一种将数据对象(deviceToken)转换为NSString的方法,但到目前为止没有成功

错误:“致命错误:在展开可选值时意外发现nil”

非常感谢您的帮助。这是我的密码

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken   deviceToken: NSData!) {
    let urlPath = "http://example.com/deviceToken=\(deviceToken)"
    let url = NSURL(string: urlPath)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
        if(error != nil) {
            // If there is an error in the web request, print it to the console
            println(error.localizedDescription)
        }
        var err: NSError?

    })

    task.resume()  
}

你能精确地指出什么变量被展开并返回零吗?除了URL之外,我看不到任何可能导致此错误的内容,因此您的错误可能是无效的URL。请记住,NSURL根据严格的语法()验证给定的字符串。尝试此URL(不使用deviceToken),看看这是否有什么不同:

let urlPath = "http://example.com/?deviceToken"
另一方面,设备令牌需要进行URL编码,请参阅。您的整个方法如下所示:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for var i = 0; i < deviceToken.length; i++ {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }
    let urlPath = "http://example.com/?deviceToken=\(tokenString)"
    let url = NSURL(string: urlPath)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
        if(error != nil) {
            // If there is an error in the web request, print it to the console
            println(error.localizedDescription)
        }
        var err: NSError?

    })

    task.resume() 

}
func应用程序(应用程序:UIApplication,didRegisterForRemotionTificationswithDeviceToken deviceToken:NSData){
让tokenChars=UnsafePointer(deviceToken.bytes)
var tokenString=“”
对于变量i=0;i中的无效
如果(错误!=nil){
//如果web请求中有错误,请将其打印到控制台
println(错误。本地化描述)
}
变量错误:n错误?
})
task.resume()
}

您能精确地指出哪个变量被展开并返回nil吗?除了URL之外,我看不到任何可能导致此错误的内容,因此您的错误可能是无效的URL。请记住,NSURL根据严格的语法()验证给定的字符串。尝试此URL(不使用deviceToken),看看这是否有什么不同:

let urlPath = "http://example.com/?deviceToken"
另一方面,设备令牌需要进行URL编码,请参阅。您的整个方法如下所示:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""

    for var i = 0; i < deviceToken.length; i++ {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }
    let urlPath = "http://example.com/?deviceToken=\(tokenString)"
    let url = NSURL(string: urlPath)
    let session = NSURLSession.sharedSession()
    let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
        if(error != nil) {
            // If there is an error in the web request, print it to the console
            println(error.localizedDescription)
        }
        var err: NSError?

    })

    task.resume() 

}
func应用程序(应用程序:UIApplication,didRegisterForRemotionTificationswithDeviceToken deviceToken:NSData){
让tokenChars=UnsafePointer(deviceToken.bytes)
var tokenString=“”
对于变量i=0;i中的无效
如果(错误!=nil){
//如果web请求中有错误,请将其打印到控制台
println(错误。本地化描述)
}
变量错误:n错误?
})
task.resume()
}

”[http://example.com]&deviceToken=\(deviceToken)“
应该是
”http://example.com&deviceToken=\(deviceToken)”
,不带方括号。你从哪里得到的?实际上它应该是
“http://example.com?deviceToken=\(deviceToken)“
”[http://example.com]&deviceToken=\(deviceToken)“
应该是
”http://example.com&deviceToken=\(deviceToken)”
,不带方括号。你从哪里得到的?实际上它应该是
“http://example.com?deviceToken=\(deviceToken)“
。非常感谢。我正试图打开deviceToken,所以您的解决方案运行良好。@马特很高兴我能提供帮助:)非常小的注释-
格式:“%02hhx”
就足够了。无需精度,因为
设备令牌的每个字节都是整数。请看这在Xcode 7.3中已被弃用,请更新您的答案谢谢。我正试图打开deviceToken,所以您的解决方案运行良好。@马特很高兴我能提供帮助:)非常小的注释-
格式:“%02hhx”
就足够了。无需精度,因为
设备令牌的每个字节都是整数。请参阅Xcode 7.3中不推荐使用的内容,请更新您的答案