ApiNetWork 3.0.0

ApiNetWork 3.0.0

TestsTested
LangLanguage SwiftSwift
许可证 MIT
Released上次发布2015年9月
SPM支持 SPM

Norbert Billa 维护。



  • Billa Norbert

ApiNetWork V2

管理 Swift 的异步网络

注意:基于 Xcode 6、swift1.2、swift2 构建,所有方法都是异步的。

安装

使用 CocoaPods

对于 swift 1.2 使用 ~> “2.X.X”,对于 swift 2 使用 ~> “3.X.X”

pod 'ApiNetWork'

Cocoapods 需要 iOS 8.0 或更高版本。如果您支持 iOS 7,或者您希望如此,您只需将 ApiNetWork.swift 添加到您的项目中即可。

基础

    ApiNetwork.launchRequest("http://www.google.fr", completion:
    { (response) -> Void in

    })

    or 

    let a  = ApiNetwork(stringURL: "your-url")
    a.launchRequest(didReceived: nil) { (response) -> Void in

    }

参数

//yourUrl?user=jack&password=test
 let n : ApiNetwork = ApiNetwork(stringURL: "yourUrl")
 n.addParameterWithKey("user", "jack")
 n.addParameterWithKey("password", "test")

下载

启动简单下载

let iv : UIImageView = UIImageView() 
let a = ApiNetwork.launchRequestDownloading("your-url", didReceived: nil)
 { (response) -> Void in
        if response.errors == nil {
            if let data = response.getResponseData() {
                iv.image = UIImage(data: data)
            }
        } else if response.didFailNotConnectedToInternet() == true {
            println("not connection to internet")
        }
}

下载并注册到路径

let a  = ApiNetwork(stringURL: "your-url")
a.setPathFileDownload("path-to-downlaod-file")
a.launchRequestDownloading(didReceived: nil) { (response) -> Void in

}

恢复下载

您可以简单重发请求来恢复下载。只有在使用 setPathFileDownload 时才有效。

停止下载

调用 stopDownloading() 方法。

请求方法

默认情况下,方法设置为 .GET,但可以更改为 .GET .POST .DELETE .PUT .PATCH .HEAD

let a = ApiNetwork(stringURL: "your-url")
a.setMethod(.POST)
a.launchRequest { (response) -> Void in
    // DO YOU WANT            
}

响应

对于每个请求,响应将返回一个 ApiNetworkResponse 类。

请求结果

使用此方法获取请求的结果。

 getResponseString() 
 getResponseData() 
 getResponseDictionary() 

选项 & ApiNetworkResponse 变量

status_code
errors                    
header                    
URL                       
mime_type                 
expectLengthDownloading   
totalLengthDownloaded     

仍然需要帮助吗?

在 hangout 上联系我,+BillaNorbert