URLService 繼承 NSObject

? ? //請求搜索的城市數據

? ? func searchmeng(search:String,vc:UIViewController,completion:@escaping(Any,Bool) ->Void) {

? ? ? ? //(1)判斷無網絡狀態

? ? ? ? if Reachability.forLocalWiFi().currentReachabilityStatus() == NotReachable && Reachability.forInternetConnection().currentReachabilityStatus() == NotReachable {

? ? ? ? ? ? vc.showAlert(msg:"網絡錯誤請檢查網絡", sec:2.0)

? ? ? ? ? ? completion("error",false)

? ? ? ? ? ? return

? ? ? ? }

? ? ? ? //(2)狀態欄中的菊花開始轉動

? ? ? ? UIApplication.shared.isNetworkActivityIndicatorVisible = true

? ? ? ? // (3)網址字符串封裝

? ? ? ? let url = URL.init(string: "http://api.jisuapi.com/illegaladdr/city")

? ? ? ? // (4)創建請求對象

? ? ? ? var req =URLRequest.init(url: url!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval:15.0)

? ? ? ? //設置請求方式為POST

? ? ? ? req.httpMethod="POST"

? ? ? ? //將所有參數拼成一個字符串

? ? ? ? let str = "city=\(search)&num=10&appkey=de394933e1a3e2db"

? ? ? ? //設置請求對象的請求體

? ? ? ? req.httpBody= str.data(using: .utf8)

? ? ? ? //(5)會話對象請求網絡數據

? ? ? ? URLSession.shared.dataTask(with: req){(data:Data?,response,error)in

? ? ? ? ? ? //停止菊花轉動

? ? ? ? ? ? DispatchQueue.main.async{

? ? ? ? ? ? ? ? UIApplication.shared.isNetworkActivityIndicatorVisible = false

? ? ? ? ? ? }

? ? ? ? ? ? //如果服務器連接失敗

? ? ? ? ? ? if error !=nil{

? ? ? ? ? ? ? ? DispatchQueue.main.async{

? ? ? ? ? ? ? ? ? ? vc.showAlert(msg:"服務器超時", sec:2.5)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? return

? ? ? ? ? ? }

? ? ? ? ? ? //JSON解析

? ? ? ? ? ? let jsonData =? try?JSONSerialization.jsonObject(with: data!, options: .allowFragments)

? ? ? ? ? ? if jsonData ==nil{

? ? ? ? ? ? ? ? DispatchQueue.main.async{

? ? ? ? ? ? ? ? ? ? vc.showAlert(msg:"網絡數據錯誤", sec:2.5)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? return

? ? ? ? ? ? }

? ? ? ? ? ? //如果正確,將解析的json數據返回給Controller

? ? ? ? ? ? let jsonDic = jsonDataas!NSDictionary

? ? ? ? ? ? let status = jsonDic.value(forKey:"status")as!NSString

? ? ? ? ? ? let msg = jsonDic.value(forKey:"msg")as!String


? ? ? ? ? ? if status.intValue!=0{

? ? ? ? ? ? ? ? //返回主線程

? ? ? ? ? ? ? ? DispatchQueue.main.async{

? ? ? ? ? ? ? ? ? ? vc.showAlert(msg: msg, sec:2.0)

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? return

? ? ? ? ? ? }

? ? ? ? ? ? //得到json數據中的result字段對應的字典

? ? ? ? ? ? let resultArr = jsonDic.value(forKey:"result")as!NSArray



? ? ? ? ? ? //Model封裝

? ? ? ? ? ? var modelArr :[city] = []

? ? ? ? ? ? //遍歷數組中的每個字典

? ? ? ? ? ? for item in resultArr

? ? ? ? ? ? {

? ? ? ? ? ? ? ? let itemDic = item as! ?NSDictionary

? ? ? ? ? ? ? ? let ?one =city()

? ? ? ? ? ? ? ? one.province= itemDic.value(forKey:"province")as? String

? ? ? ? ? ? ? ? one.content= itemDic.value(forKey:"content")as? String

? ? ? ? ? ? ? ? one.city= itemDic.value(forKey:"city")as? String

? ? ? ? ? ? ? ? one.town= itemDic.value(forKey:"towm")as? String

? ? ? ? ? ? ? ? one.address= itemDic.value(forKey:"address")as? String

? ? ? ? ? ? ? ? one.num= itemDic.value(forKey:"num")as? String

? ? ? ? ? ? ? ? one.lat= itemDic.value(forKey:"lat")as? String

? ? ? ? ? ? ? ? one.lng= itemDic.value(forKey:"lng")as? String

? ? ? ? ? ? ? ? modelArr.append(one)


? ? ? ? ? ? }

? ? ? ? ? ? completion(modelArr,true)

? ? ? ? ? ? }.resume()


? ? }

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容