Alamofire是swift下使用最多的第三方網(wǎng)絡(luò)請求庫,看一下用Alamofire怎么發(fā)送網(wǎng)絡(luò)請求
環(huán)境: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
在需要用到網(wǎng)絡(luò)請求的類 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