Alamofire是swift下使用最多的第三方网络请求库,看一下用Alamofire怎么发送网络请求
环境:Xcode7.2 swift2.1.1 Alamofire2.0
首先用CocoaPods下载Alamofire
target "LKQSwift" do
platform :ios,'8.0'
use_frameworks!
pod 'Alamofire','~>2.0'
pod 'SDWebImage','~>3.7'
end
在需要用到网络请求的类 import Alamofire
POST请求:
let url = "http://xxxxxxxxxxxxx"
let dic = ["act":"homepage","act_2":"index","r_type":"1"]
Alamofire.request(.POST, url, parameters: dic, encoding: .URL, headers: nil).validate().responseJSON { (request, response, result) -> Void in
let jsonDic:NSDictionary = result.value as! NSDictionary
self.array = jsonDic.objectForKey("list") as! NSArray
self.tableView.reloadData()
}
GET请求
let url = "http://xxxxxxxxxxxxx?act=homepage&act_2=index&r_type=1"
Alamofire.request(.GET, url).validate().responseJSON(completionHandler: { (request, response, result) -> Void in
print(result.value)
})
GitHub:AlamofireTest