超简单 Alamofire4.1 的详细使用步骤

写在前面


这里 可以搜索到最新的 Alamofire~本文中 demo 使用的是 Alamofire4.1.0版本~点击 这里 可以下载~
本文项目均基于 xcode 8.1 版本
本文分为两个部分:适合对 swift 的初学者
第一部分~我们一起学习怎么一步一步将 Alamofire 导入工程
第二部分~我们一起学习怎么使用 Alamofire 文章结尾作者也做了对 Alamofire 的简单封装~

第一部分,怎么导入工程


  1. 根据前面下载一个版本的 Alamofire 文件~
  2. 新建一个工程~


    新建工程
  3. 拷贝 Alamofire 文件到我们新建的工程中~
    拷贝文件到工程
  4. 添加文件到我们自己的工程中~


    添加文件1

    添加文件2

    添加文件3
  5. 检查一下我们静态库有没有加到工程没有的话我们手动添加一下
    检查静态库1

    结果应该是这样
    检查静态库2
  6. 导入头文件import Alamofire检查是否配置错误这里可能没有提示直接敲完编译一下就OK了
    导入头文件

第二部分,怎么使用 Alamofire

这是作者的 demo`demo`写的很详细直接看demo就可以了~
这边我们也分四个部分来学习先来上个图

第一部分 --> 响应链

第二部分 --> 请求方式

第三部分 --> 下载

第四部分 --> 上传

demo中对应的方法为如下图~
对应demo中的方法

这里简单贴几行代码~
let urlStr = "\(SERVICE_URL)type=\(TOP)&key=\(APPKEY)"

    //  1. response()
    // 官方解释:The response handler does NOT evaluate any of the response data. It merely forwards on all information directly from the URL session delegate. It is the Alamofire equivalent of using cURL to execute a Request.
    Alamofire.request(urlStr).response { (returnResult) in
        if let data = returnResult.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("firstMethod --> response() --> utf8Text = \(utf8Text)")
        }
    }

    //  2. responseData()
    //  官方解释:The responseData handler uses the responseDataSerializer (the object that serializes the server data into some other type) to extract the Data returned by the server. If no errors occur and Data is returned, the response Result will be a .success and the value will be of type Data.
    Alamofire.request(urlStr).responseData { (returnResult) in
        debugPrint("firstMethod --> responseData() --> returnResult = \(returnResult)")
        if let data = returnResult.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("firstMethod --> responseData() --> utf8Text = \(utf8Text)")
        }
    }
    
    // 3. responseString()
    // 官方解释:The responseString handler uses the responseStringSerializer to convert the Data returned by the server into a String with the specified encoding. If no errors occur and the server data is successfully serialized into a String, the response Result will be a .success and the value will be of type String.
    Alamofire.request(urlStr).responseString { (returnResult) in
        debugPrint("firstMethod --> responseString() --> Sucess = \(returnResult.result.isSuccess)")
        print("firstMethod --> responseString() --> returnResult = \(returnResult)")
    }
    
    // 4. responseJSON()
    // 官方解释:The responseJSON handler uses the responseJSONSerializer to convert the Data returned by the server into an Any type using the specified JSONSerialization.ReadingOptions. If no errors occur and the server data is successfully serialized into a JSON object, the response Result will be a .success and the value will be of type Any.
    Alamofire.request(urlStr).responseJSON { (returnResult) in
        debugPrint("firstMethod --> responseJSON --> \(returnResult)")
        if let json = returnResult.result.value {
            print("firstMethod --> responseJSON --> \(json)")
            /*  返回请求地址、数据、和状态结果等信息
             print("firstMethod --> responseJSON() --> \(returnResult.request!)")
             print("firstMethod --> responseJSON() --> \(returnResult.data!)")
             print("firstMethod --> responseJSON() --> \(returnResult.result)")
             */
        }
    }

    // 1. GET请求
    let urlStr = "\(SERVICE_URL)type=\(YULE)&key=\(APPKEY)"
    Alamofire.request(urlStr, method: .get).responseJSON { (returnResult) in
        print("secondMethod --> GET 请求 --> returnResult = \(returnResult)")
    }

    // 2. POST请求
    Alamofire.request(urlStr, method: .post).responseJSON { (returnResult) in
        print("secondMethod --> POST 请求 --> returnResult = \(returnResult)")
    }

    // 3. 参数、编码
    // 官方解释:Alamofire supports three types of parameter encoding including: URL, JSON and PropertyList. It can also support any custom encoding that conforms to the ParameterEncoding protocol.
    let param = [
        "type": YULE,
        "key" : APPKEY
    ]
    Alamofire.request(SERVICE_URL, method: .post, parameters: param).responseJSON { (returnResult) in
        print("secondMethod --> 参数 --> returnResult = \(returnResult)")
    }
    //Alamofire.request(SERVICE_URL, method: .post, parameters: param, encoding: URLEncoding.default)
    //Alamofire.request(SERVICE_URL, method: .post, parameters: param, encoding: URLEncoding(destination: .methodDependent))
    
    // 4.请求头
    let headers: HTTPHeaders = [
        "Accept": "application/json"
    ]
    Alamofire.request(urlStr, headers: headers).responseJSON { (returnResult) in
        print("secondMethod --> 请求头 --> returnResult = \(returnResult)")
    }

最后附上上面代码的 demo 地址~以及笨笨自己班门弄斧基于Alamofire封装的 demo
如有问题可随时给我联系欢迎程序媛骚扰~QQ:2638006336

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,631评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,222评论 4 61
  • 我原本就是个爱睡懒觉的女生!喜欢饱饱 地睡上一觉!可是,现实却事与愿违!现在自然入睡真的是一件很困难的事!!明明身...
    Conny_2a63阅读 3,222评论 0 0
  • 有妈妈会有这样的疑惑:自己宝宝平时也会吃很多奶啊,但怎么看起来那么的瘦小呢,体重也总是增不上去。原因是什么呢? 原...
    陈宇洛燕阅读 2,325评论 0 0
  • 我已经研究生毕业两年。我是一个会读书的人,我是一个不爱读书的人。 什么意思呢? 我从小到大都是老师眼中的好学生,会...
    宋小song阅读 4,421评论 4 12