Latest 2.0.1
Homepage https://github.com/SeongBrave/MikerNetCore
License MIT
Platforms ios 8.0
Dependencies RxSwift, RxCocoa, Alamofire, SwiftyJSON, Result
Frameworks UIKit
Authors

说明

该项目是针对 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]

Latest podspec

{
    "name": "MikerNetCore",
    "version": "2.0.1",
    "summary": "u89c5u5ba2u7f51u7edcu8bf7u6c42",
    "description": "TODO: u89c5u5ba2u7f51u7edcu8bf7u6c42u5c01u88c5u5e93uff0c u5305u542bu4e86u7f51u7edcu8bf7u6c42uff0cu6570u636eu89e3u6790u7b49...",
    "homepage": "https://github.com/SeongBrave/MikerNetCore",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "seongbrave": "[email protected]"
    },
    "source": {
        "git": "https://github.com/SeongBrave/MikerNetCore.git",
        "tag": "2.0.1"
    },
    "social_media_url": "http://seongbrave.github.io/",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "MikerNetCore/Classes/*.swift",
    "frameworks": "UIKit",
    "dependencies": {
        "RxSwift": [
            "~> 2.6.0"
        ],
        "RxCocoa": [
            "~> 2.6.0"
        ],
        "Alamofire": [
            "~> 3.5.0"
        ],
        "SwiftyJSON": [
            "~> 2.4.0"
        ],
        "Result": [
            "~> 2.0.0"
        ]
    },
    "pushed_with_swift_version": "2.3"
}

Pin It on Pinterest

Share This