ios API请求

URLSession 网络请求

Alamofire 网络请求第三方包封装,推荐使用

1、使用alamofire

        导入: import Alamofire

2、发起请求:

      

AF.request("url").response { response in
    debugPrint(response)
}

3、可执行的操作:     

发起请求、下载文件、上传文件

4、关于http请求设置:保证可以向http请求数据

        info.plist

         App Transport Security Settings:[

                Allow Arbilirary loads : Yes

        ]

        这样配置之后可以请求http网址

5、代码异步执行:

        alamofire         网络请求为异步执行,

                       

6、使用swifty JSON解析返回json数据

        pod ‘SwiftJSON’,‘~>4.0’

        import SwiftyJSON

//创建URL对象

let  url =  URL (string: "http://www.baidu.com/csdcsdcssw" )!

 

Alamofire .request(url).validate().responseJSON { response  in

     switch  response.result.isSuccess {

     case  true :

         if  let  value = response.result.value {

             let  json =  JSON (value)

             if  let  number = json[0][ "grade" ][0][ "level" ].string {

                 // 找到

                 print ( "数据" ,number)

             }

         }

     case  false :

         print (response.result.error)

     }

}

猜你喜欢

转载自blog.csdn.net/qq_31319235/article/details/120744587
今日推荐