首页 > 代码库 > ios开发之--swift下AFNetworking的使用

ios开发之--swift下AFNetworking的使用

1,首先使用cocoapods导入,如果有不会的同学,可以去看我写的关于cocopods使用的那篇博客

2,直接上代码:

import UIKit
import AFNetworking

//枚举定义请求方法
enum HTTPRequestType{
    case GET
    case POST
}

class AFNetTools: AFHTTPSessionManager {
    
    //单例类
    static let shareInstance : AFNetTools = {
        let toolInstance = AFNetTools()
        toolInstance.responseSerializer.acceptableContentTypes?.insert("text/html")
        return toolInstance
    }()
    
    /// 封装GET和POST 请求
    ///
    /// - Parameters:
    ///   - requestType: 请求方式
    ///   - urlString: urlString
    ///   - parameters: 字典参数
    ///   - completion: 回调
    func request(requestType: HTTPRequestType, urlString: String, parameters: [String: AnyObject]?, completion: @escaping (AnyObject?) -> ()) {
        
        //成功回调
        let success = { (task: URLSessionDataTask, json: Any)->() in
            completion(json as AnyObject?)
        }
        
        //失败回调
        let failure = { (task: URLSessionDataTask?, error: Error) -> () in
            print("网络请求错误 \(error)")
            completion(nil)
        }
        
        if requestType == .GET {
            get(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        } else {
            post(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
    
    // 将成功和失败的回调分别写在两个逃逸闭包中
    func request(requestType : HTTPRequestType, url : String, parameters : [String : Any], succeed : @escaping([String : Any]?) -> (), failure : @escaping(Error?) -> ()) {
        
        // 成功闭包
        let successBlock = { (task: URLSessionDataTask, responseObj: Any?) in
            succeed(responseObj as? [String : Any])
        }
        
        // 失败的闭包
        let failureBlock = { (task: URLSessionDataTask?, error: Error) in
            failure(error)
        }
        
        // Get 请求
        if requestType == .GET {
            get(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
        
        // Post 请求
        if requestType == .POST {
            post(url, parameters: parameters, progress: nil, success: successBlock, failure: failureBlock)
        }
    }
    
}

以上就是二次封装的请求类

3,调用:

@IBAction func startRequest(_ sender: Any) {

        let paramerts = [
            "opt_type": 1,
            "size" : 50,
            "offset" : 50
        ]
        
        AFNetTools.shareInstance.request(requestType: .GET, url: "http://apiv2.yangkeduo.com/operation/15/groups", parameters: paramerts, succeed: { (response) in
            guard let dicArray = response?["goods_list"] as? [[String: Any]] else {
                return
            }
            print(dicArray)
            
        }) { (error) in
            guard let error = error else {
                return
            }
            print(error)
        }

        
    }

 打印如下:

技术分享

 

ios开发之--swift下AFNetworking的使用