[英]How to use Alamofire in swift 3 in ios 9?
I made swift app and i will use Alamofire for swift json parsing.我制作了 swift 应用程序,我将使用 Alamofire 进行快速 json 解析。
For that i install following pod:为此,我安装了以下吊舱:
pod 'Alamofire', '~> 4.4' pod 'Alamofire', '~> 4.4'
After that i update pod but it will give me following error :之后我更新 pod 但它会给我以下错误:
[!] CocoaPods was not able to update the master
repo. [!] CocoaPods 无法更新
master
库。 If this is an unexpected issue and persists you can inspect it running pod repo update --verbose
如果这是一个意外问题并且持续存在,您可以检查它运行
pod repo update --verbose
so any one have solution of that then please help me.所以任何人都有解决方案,然后请帮助我。
I used xcode 8.3.1 ,Swift 3 and app compatibility from ios 9.0.我使用了 xcode 8.3.1 、Swift 3 和 ios 9.0 的应用程序兼容性。
This answer could fix your problem:这个答案可以解决您的问题:
Uninstall CocoaPods (choose to uninstall all versions):卸载 CocoaPods(选择卸载所有版本):
sudo gem uninstall cocoapods
Remove old master repo:删除旧的主仓库:
sudo rm -fr ~/.cocoapods/repos/master
Install CocoaPods without sudo:无需 sudo 即可安装 CocoaPods:
gem install --user-install cocoapods
Setup CocoaPods and Master repo:设置 CocoaPods 和 Master repo:
pod setup
NOTE: Step 3 is going to make sure you install pod under your user name instead of root which sudo does.注意:第 3 步将确保以您的用户名而不是 sudo 的 root 安装 pod。
For iOS Swift 4 :对于 iOS Swift 4:
go through this link :通过这个链接:
http://ashishkakkad.com/2015/10/how-to-use-alamofire-and-swiftyjson-with-swift/
step 1 - install pod第 1 步 - 安装 pod
pod 'Alamofire'
step 2 - import Alamofire (in your VC)第 2 步 - 导入 Alamofire(在您的 VC 中)
step 3 - create URL & parameters for that第 3 步 - 为此创建 URL 和参数
let params:[String:String] = ["Name":self.name]
or
let params = nil
step 4 - call the func第 4 步 - 调用函数
public func GET_ALL(params:[String:String])
{
let baseURL = "your url"
if NetworkReachabilityManager()?.isReachable == true
{
AppDelegate.sharedInstance().ShowSpinnerView()
Alamofire.request(baseURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).response { (response) in
AppDelegate.sharedInstance().HideSpinnerView()
do
{
let dict = try? JSONSerialization.jsonObject(with: response.data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSMutableDictionary
if dict?.count==0 || dict == nil
{
let alert = UIAlertController(title: APPNAME, message: "No reposnse from server.", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: { (action) in
})
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
else
{
self.checkDictionary(dict!)
if dict?.object(forKey: "data") is NSMutableDictionary
{
self.arrAllData = (dict?.value(forKeyPath: "data.myName") as? NSMutableArray)!
if(self.arrAllData.count == 0)
{
}
else
{
}
self.TableView.reloadData()
}
else
{
}
}
}
catch let error as NSError
{
print(error.localizedDescription)
let alert = UIAlertController(title: APPNAME, message: error.localizedDescription, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default)
{ (action) in
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
self.hideSpinner()
}
}
}
else
{
AppDelegate.sharedInstance().HideSpinnerView()
let alert = UIAlertController(title: APPNAME, message: "No network available.", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default)
{ (action) in
}
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
}
let params:[String:String] = ["slug":self.strSlug]
let params:[String:String] = ["slug":self.strSlug]
let header = ["Content-Type":"application/x-www-form-urlencoded", "Authorization": "Basic aW50cmFpZXAwMDE6dWZaeWFyUlp3NE1CeUg4RA==" ]
let header = ["Content-Type":"application/x-www-form-urlencoded", "Authorization": "Basic aW50cmFpZXAwMDE6dWZaeWFyUlp3NE1CeUg4RA==" ]
let url = API.GET_NEWS
让 url = API.GET_NEWS
**Info.plis : make sure you have added -> App transport Security Setting : Allow Arbitrary Loads : YES **
**Info.plis:确保您已添加 -> 应用程序传输安全设置:允许任意加载:是 **
self.ArrData = dict?.value(forKeyPath: "results") as? NSMutableArray
for i in 0...self.ArrData.count-1{
let dict = self.ArrData[i] as? NSMutableDictionary
let dictGeometry = dict?.value(forKeyPath: "geometry") as? NSMutableDictionary
let TempDict = NSMutableDictionary()
TempDict.setObject(String(dictGeometry?.value(forKeyPath: "location.lat") as? Double ?? 0.0), forKey: "lat" as NSCopying)
TempDict.setObject(String(dictGeometry?.value(forKeyPath: "location.lng") as? Double ?? 0.0), forKey: "lng" as NSCopying)
TempDict.setObject(dict?.value(forKeyPath: "icon") as? String ?? "", forKey: "icon" as NSCopying)
TempDict.setObject(dict?.value(forKeyPath: "name") as? String ?? "", forKey: "name" as NSCopying)
TempDict.setObject(dict?.value(forKeyPath: "vicinity") as? String ?? "", forKey: "vicinity" as NSCopying)
print(TempDict)
self.ArrFinalData.add(TempDict)
}
print(self.ArrFinalData.count)
Swift Alamofire Method Calling:
Swift Alamofire 方法调用:
func getMovieList(page: Int){
let url = API.SERVER_URL + "\(page)"
Alamofire.request(URL(string: url)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (responseObject) in
let response = self.formatResponse(data: responseObject)
let arr = response["results"] as! [[String : AnyObject]]
self.arrMovie.append(contentsOf: arr)
self.MoviewList = MovieDataList(data: self.arrMovie)
self.tableView.reloadData()
self.isLoadingList = false
self.deleteAllRecords()
}
}
func formatResponse(data:DataResponse<Any>)-> [String:AnyObject]
{
let responseObject = data.result.value as? [NSObject: AnyObject]
let response = responseObject as? [String : AnyObject]
return response ?? [:]
}
github.com/tej1107/MVC_MOVIEDEMO github.com/tej1107/TableView_CollectionView github.com/tej1107/TableView_CollectionView github.com/tej11695/MVC_UserList github.com/tej11695/New_MVC github.com/tej11695/EmployeeRegistration github.com/tej11695/List-SignUp_API
github.com/tej1107/MVC_MOVIEDEMO github.com/tej1107/TableView_CollectionView github.com/tej1107/TableView_CollectionView github.com/tej11695/MVC_UserList github.com/tej11695/New_MVC github.com/tj1107/TableView_CollectionView注册_API
pod 'SDWebImage' pod 'Alamofire','4.9.1'
pod 'SDWebImage' pod 'Alamofire','4.9.1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.