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

问题描述 投票:1回答:2

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

为此我安装以下pod:

根据'Alamofire','〜> 4.4'

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

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

所以任何人都有解决方案然后请帮助我。

我使用了xcode 8.3.1,Swift 3和ios 9.0的app兼容性。

ios swift cocoapods afnetworking alamofire
2个回答
0
投票

This answer可以解决您的问题:

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

sudo gem uninstall cocoapods

删除旧的主仓库:

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

在没有sudo的情况下安装CocoaPods:

gem install --user-install cocoapods

设置CocoaPods和Master repo:

pod setup

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


0
投票

对于iOS Swift 4:

通过这个链接:

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步 - 调用func

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)
    }
}

让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:确保您已添加 - > App transport安全设置:允许任意负载:是**

 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)
© www.soinside.com 2019 - 2024. All rights reserved.