iOS 网络库 Pitaya 发布 1.0 (Swift)

2,513 阅读1分钟
原文链接: github.com

Pitaya is a sweet HTTP networking library especially for large file uploads written in Swift. Inspired by Alamofire and JustHTTP.

Pitaya logo

Features

  • Fast file upload through "Content-Type: multipart/form-data"
  • HTTP Basic Authorization supported
  • Asynchronous & Blocking(blocked in thread II)
  • Multi-level API to keep your code clean
  • Well tested

Requirements

  • iOS 8.0+
  • Xcode 6.3 (Swift 1.2)

Installation

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Pitaya into your Xcode project using Carthage, specify it in your Cartfile:

github "JohnLui/Pitaya" >= 0.1

Manually

git clone https://github.com/johnlui/Pitaya
open Pitaya/Pitaya

then drag Pitaya.xcodeproj to your Project, that's it!

Usage

Make a request:

Pitaya.request(.GET, "http://pitayaswift.sinaapp.com/pitaya.php", { (error) -> Void in
    NSLog(error.localizedDescription)
    }) { (string) -> Void in
        println(string)
}

with params:

Pitaya.request(.GET, "http://pitayaswift.sinaapp.com/pitaya.php", ["get": "pitaya"], { (error) -> Void in
    NSLog(error.localizedDescription)
    }) { (string) -> Void in
        println(string)
}

upload files:

let file = File(name: "photo", url: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Info", ofType: "plist")!)!)
Pitaya.request(.POST, "http://pitayaswift.sinaapp.com/pitaya.php", files: [file], { (error) -> Void in
    NSLog(error.localizedDescription)
    }) { (string) -> Void in
        println(string)
}

POST params and files:

let file = File(name: "photo", url: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Info", ofType: "plist")!)!)
Pitaya.request(.POST, "http://pitayaswift.sinaapp.com/pitaya.php", ["post": "pitaya", "post2": "pitaya2"], files: [file], { (error) -> Void in
    NSLog(error.localizedDescription)
    }) { (string) -> Void in
        println(string)
}

HTTP Basic Authorization

let pitaya = PitayaManager.build(.GET, url: "http://httpbin.org/basic-auth/user/passwd")
pitaya.fireWithBasicAuth(("user", "passwd"), errorCallback: { (error) -> Void in
    NSLog(error.localizedDescription)
}) { (string) -> Void in
    println(string)
}

Params and Files with HTTP Basic Authorization

let pitaya = PitayaManager.build(.GET, url: "http://httpbin.org/basic-auth/user/passwd")

// add params
pitaya.addParams(["hello": "pitaya"])

// add files
let file = File(name: "file", url: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Pitaya", ofType: "png")!)!)
pitaya.addFiles([file])

pitaya.fireWithBasicAuth(("user", "passwd"), errorCallback: { (error) -> Void in
    NSLog(error.localizedDescription)
}) { (string) -> Void in
    println(string)
}

They are all Asynchronous.

Play with JSON

You can use SwiftyJSON to parse string to JSON:

extension String {
    var nsdata: NSData {
        return self.dataUsingEncoding(NSUTF8StringEncoding)!
    }
}

Pitaya.request(.GET, "http://pitayaswift.sinaapp.com/pitaya.php", { (error) -> Void in
    NSLog(error.localizedDescription)
    }) { (string) -> Void in
        let json = JSON(data: string.nsdata)
        ... ...
}

Contribution

You are welcome to fork and submit pull requests.

License

Pitaya is open-sourced software licensed under the MIT license.