如何在 swift 3 在 ios 9 中使用 Alamofire?

How to use Alamofire in swift 3 in ios 9?

我制作了 swift 应用程序,我将使用 Alamofire 进行 swift json 解析。

为此,我安装了以下 pod:

pod 'Alamofire', '~> 4.4'

之后我更新了 pod,但它会给我以下错误:

[!] CocoaPods 无法更新 master 存储库。如果这是一个意外问题并且仍然存在,您可以检查它 运行 pod repo update --verbose

所以谁有解决方案,请帮助我。

我使用 xcode 8.3.1,Swift 3 和 ios 9.0.

的应用程序兼容性

This answer 可以解决您的问题:

卸载CocoaPods(选择卸载所有版本):

sudo gem uninstall cocoapods

删除旧的主仓库:

sudo rm -fr ~/.cocoapods/repos/master

不使用 sudo 安装 CocoaPods:

gem install --user-install cocoapods

设置 CocoaPods 和主存储库:

pod setup

注意:第 3 步将确保您将 pod 安装在您的用户名下,而不是 sudo 执行的 root 下。

对于iOS Swift 4 :

完成这个 link :

http://ashishkakkad.com/2015/10/how-to-use-alamofire-and-swiftyjson-with-swift/

第 1 步 - 安装 pod

pod 'Alamofire'

第 2 步 - 导入 Alamofire(在您的 VC 中)

第 3 步 - 创建 URL 和参数

let params:[String:String] = ["Name":self.name]
or
let params = nil

第 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 header = ["Content-Type":"application/x-www-form-urlencoded", "Authorization": "Basic aW50cmFpZXAwMDE6dWZaeWFyUlp3NE1CeUg4RA==" ]

let url = API.GET_NEWS

**Info.plis : make sure you have added -> App transport Security Setting : Allow Arbitrary Loads : YES **

 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:

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

pod 'SDWebImage' pod 'Alamofire','4.9.1'