import CoreLocation
import ApiSwift
import SwiftSerialize
let url = NSURL(string: "http://localhost:8000/Api.php")
let obj1 = TestClass(string: "Test str", number: 4.5, location: CLLocationCoordinate2D(latitude: 33.2, longitude: 44), array: [3, 5, 84, 23], map: ["key1": ["key2" : 82]], set: [3.3, 55.3, 32.1])
let obj2 = 4321
let params:[Any] = [obj1, obj2]
let additionalParams:[String: String] = [
"id": "1234567890",
]
let request = NSMutableURLRequest(url: url!, className: "MyClass", functionName: "MyFunc", params: params, additionalParams: additionalParams)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
// handle response
let object = Serializer.deserialize(data!)
}.resume()
This example calls the MyFunc static function on the MyClass class. The 2 parameters are of type TestClass and an integer. Traditional HTTP parameters can also be added to the request in the additionalParams.
The objects passed as params are serialzed into JSON before being sent to the server. The server will deserialze the objects and recreate the objects on the server. Then, the static class function specified is called with the deserialzed objects.
This library uses SwiftSerialize to serialize objects. It can also deserialize objects to classes (custom classes you make or built-in Swift classes) with virtually no work on your end. Head over there to find out how to fully utilize this library.
Currently the only server-side language that can handle this request is PHP.
To run the example project, clone the repo, and run pod install
from the Example directory first.
You'll also need to uncomment the commented lines in SwiftSerialize/Initializer.swift (in the SwiftSerialize pod) to run the test cases.
ApiSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "ApiSwift"
Will accept all valid pull requests, feature requests, and other issues. Want to help, just ask!
ApiSwift is available under the MIT license. See the LICENSE file for more info.