CocoaPods trunk is moving to be read-only. Read more on the blog, there are 10 months to go.

MikerNetCore 2.0.1

MikerNetCore 2.0.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Dec 2016
SwiftSwift Version 2.3
SPMSupports SPM

Maintained by seongbrave.



 
Depends on:
RxSwift~> 2.6.0
RxCocoa~> 2.6.0
Alamofire~> 3.5.0
SwiftyJSON~> 2.4.0
Result~> 2.0.0
 

说明

该项目是针对 Alamofire的封装 , 通过使用RxSwiftSwiftyJSON可以将一次请求转换成信号并且可以借助SwiftyJSONAccelerator非常方便的转换成model对象

如果是http的访问记得在info.plist中添加App Transport Security Settings设置

基础配置

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        MikerNetCore.baseUrl = "http://www.baokan.name/e/api"
        MikerNetCore.statusKey = "err_code"
        // Override point for customization after application launch.
        return true
    }

路由文件

import Foundation
import Alamofire
import MikerNetCore


public enum UserApi{
    //  UserApi
    case login(loginName: String, password: String)

    case getNewsList

}
extension UserApi: TargetType {

    //设置请求路径
    public var path: String {
        switch self {
        case .login:
            return "/login.html"
        case .getNewsList:
            return "/getNewsList.php"
        }
    }

    //设置请求方式 get post等
    public var method: Alamofire.Method {
        switch self {
        case .login:
            return .POST
        default :
            return .GET

        }
    }
    /// 设置请求参数
    public var parameters: [String: AnyObject]? {
        switch self {
        case let .login(loginName, password):
            return ["username": loginName, "password": password]
        default :
            return nil
        }
    }

}

使用

self.requestBtn.rx_tap.map{ _ in
            return UserApi.getNewsList
            }.emeRequestApiForJson().subscribeNext{ result in
                switch result {
                case .Success(let data):
                    print(data)
                case .Failure(let error):
                    print(error)
                }
            }.addDisposableTo(disposeBag)

mikerErrorCode是服务端定义的一个错误状态码,通过返回的status状态码,可以转换成对应的error 返回到界面中来, 这样界面中同时可以监控到正确是返回的数据和错误时需要的错误信息方便提示给用户, 这块简化了不少,这套网络请求主要还是配合mvvm来使用的, 这块有个缺点就是返回的数据格式是必须是:

 public class Reg: MikerSwiftJSONAble {
    var status: Int!
    var data: JSON
    required public init?(json:JSON){
        self.status = json[MikerNetCore.statusKey].intValue
        self.data = json[MikerNetCore.dataKey]
    }
}

才可以配合SwiftyJSONAccelerator该库方便直接解析成model 对象

Installation

MikerNetCore is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "MikerNetCore"

Author

seongbrave, [email protected]