使用NSURLConnection连接无效

时间:2016-04-01 06:09:01

标签: ios swift nsurlconnection

好吧,我会在NSURLConnection的帮助下尽快将数据发布到我的服务器上。这是我在swift中的代码

let requests : NSMutableURLRequest = NSMutableURLRequest(URL: url)
    let useragent : String
    if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Phone {
        useragent = "iPhone"
    }
    else {
        useragent = "iPad"
    }

    requests.setValue(useragent, forHTTPHeaderField: "User-Agent")
    requests.HTTPMethod = "POST"
    requests.setValue("application/json", forHTTPHeaderField: "Content-type")
    let jsonstring : String = String(data: urlData, encoding: NSUTF8StringEncoding)!
    NSLog("Data being posted to the server")
    NSLog("Post Data  : Data being posted to server")
    requests.timeoutInterval = 60.0
    requests.HTTPBody = jsonstring.dataUsingEncoding(NSUTF8StringEncoding)
    requests.setValue("\(jsonstring.characters.count)", forHTTPHeaderField: "Content-Length")
    let conn : NSURLConnection = NSURLConnection(request: requests, delegate: self)!
    conn.isProxy()

我尝试在设备日志中打印东西,网址和要发布的数据(字典名为json)都很完美。日志也成功打印这些行

NSLog("Data being posted to the server")
    NSLog("Post Data  : Data being posted to server")

但在此之后,应用程序崩溃,我可以在设备日志中看到“连接无效”。任何人都可以帮助我告诉我,如果我在这里任何一点都错了。提前谢谢。

0 个答案:

没有答案