The Waterwheel Swift SDK provides classes to natively connect iOS, macOS, tvOS, and watchOS applications to Drupal 7 and 8.
Drupal
Features • Configuration • Usage • Installation • Requirements
import waterwheel
The code below will give you access to the baseline of features for communicating to a Drupal site.
// Sets the URL to your Drupal site.
waterwheel.setDrupalURL("http://waterwheel-swift.com")
It is important to note that waterwheel makes heavy uses of Closures, which allows us to pass functions as returns, or store them in variables.
The code below will set up Basic Authentication for each API call.
// Sets HTTPS Basic Authentication Credentials.
waterwheel.setBasicAuthUsernameAndPassword("test", password: "test2");
If you do not want to use Basic Auth, and instead use a cookie, waterwheel provides an authentication method for doing so.
Sessions are handled for you, and will restore state upon closing an app and reopening it.
waterwheel.login(usernameField.text!, password: passwordField.text!) { (success, response, json, error) in
if (success) {
print("logged in")
} else {
print("failed to login")
}
}
Waterwheel provides a waterwheelAuthButton
to place anywhere in your app. The code below is iOS specific because of its dependence on UIKit.
let loginButton = waterwheelAuthButton()
// When we press Login, lets show our Login view controller.
loginButton.didPressLogin = {
waterwheel.login(usernameField.text!, password: passwordField.text!) { (success, response, json, error) in
if (success) {
print("successful login")
} else {
print("failed to login")
}
}
}
loginButton.didPressLogout = { (success, error) in
print("logged out")
}
self.view.addSubview(loginButton)
Taking this one step further, waterwheel also provides a waterwheelLoginViewController
. You can subclass this controller and overwrite if needed. For our purposes we will use the default implementation.
First, we build our waterwheelLoginViewController
and set our loginRequestCompleted
and logoutRequestCompleted
closures:
// Lets build our default waterwheelLoginViewController.
let vc = waterwheelLoginViewController()
//Lets add our closure that will be run when the request is completed.
vc.loginRequestCompleted = { (success, error) in
if (success) {
// Do something related to a successful login
print("successful login")
self.dismissViewControllerAnimated(true, completion: nil)
} else {
print (error)
}
}
vc.logoutRequestCompleted = { (success, error) in
if (success) {
print("successful logout")
// Do something related to a successful logout
self.dismissViewControllerAnimated(true, completion: nil)
} else {
print (error)
}
}
Once that is done we can now tell our waterwheelAuthButton
what to do when someone presses Login. Of course this can all be handled manually in your own implementation, but for our purposes, were just using what waterwheel provides.
Here we instantiate a new waterwheelAuthButton
and tell it what we want to happen when someone presses login, and logout.
let loginButton = waterwheelAuthButton()
// When we press Login, lets show our Login view controller.
loginButton.didPressLogin = {
// Lets Present our Login View Controller since this closure is for the loginButton press
self.presentViewController(vc, animated: true, completion: nil)
}
loginButton.didPressLogout = { (success, error) in
print("logged out")
}
self.view.addSubview(loginButton)
Because these two Views know whether you are logged in or out, they will always show the correct state of buttons(Login, or Logout) and perform the approriate actions. The UI is up to you, but at its default you get username, password, submit, and cancel button. With all that said, you can ingore these classes and use the methods that waterwheel provides and deeply integrate into your own UI.
// Get Node 36
waterwheel.nodeGet(nodeId: "36", params: nil, completionHandler: { (success, response, json, error) in
print(response)
})
//build our node body
let body = [
"type": [
[
"target_id": "article"
]
],
"title": [
[
"value": "Hello World"
]
],
"body": [
[
"value": "How are you?"
]
]
]
// Create a new node.
waterwheel.entityPost(entityType: .Node, params: body) { (success, response, json, error) in
if (success) {
print(response)
} else {
print(error)
}
}
// Update an existing node
waterwheel.nodePatch(nodeId: "36", node: body) { (success, response, json, error) in
print(response);
}
// Delete an existing node
waterwheel.nodeDelete(nodeId: "36", params: nil, completionHandler: { (success, response, json, error) in
print(response)
})
Since Node is rather specific, Watherweel provides entity methods as well for all entityTypes
waterwheel.entityGet(entityType: .Node, entityId: "36", params: params, completionHandler: completionHandler)
waterwheel.sharedInstance.entityPost(entityType: .Node, params: node, completionHandler: completionHandler)
waterwheel.entityPatch(entityType: .Node, entityId: "36", params: nodeObject, completionHandler: completionHandler)
waterwheel.entityDelete(entityType: .Node, entityId: entityId, params: params, completionHandler: completionHandler)
Waterwheel offers two installations paths. Pick your poison!
If you’re using CocoaPods, just add this line to your Podfile:
pod 'waterwheel'
Install by running this command in your terminal:
pod install
Then import the library in all files where you use it:
import waterwheel
Just add to your Cartfile:
github "acquia/waterwheel-swift"
Run carthage update
to build the framework and drag the built waterwheel.framework
into your Xcode project.
waterwheel version | Drupal Version |
---|---|
4.x | Drupal 8 (Swift) |
3.x | Drupal 8 (Obj-C) |
2.x | Drupal 6-7 (Obj-C) |