我是swift的新手,并正在开发一个应用程序以正确学习swift。
print("checking version tab bar")
let headers = [
"Content-Type" : "application/json",
"Accept" : "application/json",
]
let postData = NSMutableData(data: "client=ios".data(using: String.Encoding.utf8)!)
postData.append("&sourcePath=sourcePath".data(using: String.Encoding.utf8)!)
postData.append("¤tVersion=\(version)".data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://app.avatejaratsaba1.com/api/Values/CheckUpdate")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error as Any)
}
else
{
do { let json = try JSONSerialization.jsonObject(with: postData as Data, options: .allowFragments) as? NSDictionary
let HttpResponse = response as? HTTPURLResponse
//print( "first \(String(describing: (json!).value(forKey: "access_token")))")
if let parseJSON = json
{
let SourcePath = parseJSON["sourcePath"] as? String
let minVersionRequired = parseJSON["minVersion"] as? String
let updateVersion = parseJSON["updateVersion"] as? String
uupp.update.link = SourcePath!
if minVersionRequired?.compare(self.version , options: .numeric) == .orderedDescending
{
// compulsory update is avaiable
uupp.update.compulsory = true
print("opop")
let next = self.storyboard?.instantiateViewController(withIdentifier: "updatePopUp")
self.navigationController?.pushViewController(next!, animated: true)
}
else
{
if updateVersion?.compare(self.version , options: .numeric) == .orderedDescending
{
print("roro")
uupp.update.compulsory = false
//Optional update is availabel
let next = self.storyboard?.instantiateViewController(withIdentifier: "updatePopUp")
self.navigationController?.pushViewController(next!, animated: true)
}
else
{
//nothing
}
}
}
else
{
print("yo")
//display an aler dialog with a friendly error message
}
}
catch
{
print(response)
print("toto")
//Display an aler dialog with a friendly error message
print(error)
}
}
})
dataTask.resume()
如您所见,上面的代码将应用程序的版本发送到api,并作为响应,它接收到一些数据,其中包括(最低版本为Required,更新版本为sourcePath)。正如您在我的文章中所看到的,我有一些IF语句来检查哪些条件成立或失败!但是当我运行该应用程序时,我得到了:
可选({URL:http://app.avatejaratsaba1.com/api/Values/CheckUpdate} {状态代码:500,标题{ “内容长度” =( 0 ); 日期=( “ 2018年7月23日星期一12:26:39 GMT” ); 服务器=( 红est ); “ X-Powered-By” =( “ ASP.NET” ); }} 托托 错误域= NSCocoaErrorDomain代码= 3840“字符0周围的值无效。” UserInfo = {NSDebugDescription =字符0周围的值无效。}
我应该发送:
{
"client": "string",
"sourcePath": "string",
"currentVersion": "string"
}
到API,响应为 { “ sourcePath”:“字符串”, “ minVersion”:“ string”, “ updateVersion”:“字符串” }
有什么建议吗?