我有一些代码,我用了一段时间后,完美的工作。现在,我想在我的新项目,这是SWIFT 3使用它,虽然我有固定的大多数错误,我仍然有两人离开如下:iOS Swift 3 POST问题
下面的代码行:var _: NSData? = NSURLConnection.sendSynchronousRequest(request as URLRequest, returning:&response) as NSData
产生以下错误:Call can throw, but it is not marked with 'try' and the error is not handled
以下代码行:NSLog("Response code: %ld", res?.statusCode ?? <#default value#>);
产生以下错误:Editor placeholder in source file
。
任何帮助表示赞赏。
下面是完整的代码,可以帮助解决这个问题:
func webViewDidFinishLoad(_ webView: UIWebView) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
let post:NSString = "userid=349&devicetoken=walan"
NSLog("PostData: %@",post);
let url:NSURL = NSURL(string: "https://str8red.com/updateAPN")!
let postData:NSData = post.data(using: String.Encoding.ascii.rawValue)! as NSData
let postLength:NSString = String(postData.length) as NSString
let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = postData as Data
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var _: NSError?
var response: URLResponse?
var _: NSData? = NSURLConnection.sendSynchronousRequest(request as URLRequest, returning:&response) as NSData
let res = response as! HTTPURLResponse!;
NSLog("Response code: %ld", res?.statusCode ?? <#default value#>);
}
如果您满意,请接受并回复它。如果您有其他问题,请发表评论。 – Wismin