Skip to content

CKalnasy/ApiSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ApiSwift

CI Status Version License Platform

Usage

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.

How it works

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.

Limitations

Currently the only server-side language that can handle this request is PHP.

Running the Example

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.

Installation

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

pod "ApiSwift"

Contributing

Will accept all valid pull requests, feature requests, and other issues. Want to help, just ask!

License

ApiSwift is available under the MIT license. See the LICENSE file for more info.

About

Call a specified function on a server from Swift 2.0 and pass Swift objects as parameters

Resources

License

Stars

Watchers

Forks

Packages

No packages published