Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift snippet using the authCallback. #470

Merged
merged 4 commits into from
Aug 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,61 @@ channel.presence.history { presencePage, error in
}];
```

### Using the `authCallback`

A callback to call to obtain a signed token request.
`ARTClientOptions` and `ARTRealtime` objects can be instantiated as follow:

**Swift**

```swift
let clientOptions = ARTClientOptions()
clientOptions.authCallback = { params, callback in
getTokenRequestJSONFromYourServer(params) { json, error in
//handle error
let tokenParams = ARTTokenParams(clientId:json["clientId"])

let tokenRequest = ARTTokenRequest(tokenParams:tokenParams,
keyName:json["keyName"],
nonce:json["nonce"],
mac:json["mac"])
tokenRequest.clientId = json["clientId"]
tokenRequest.ttl = json["ttl"]
tokenRequest.capability = json["capability"]
tokenRequest.timestamp = NSDate(timeIntervalSince1970:(json["timestamp"] / 1000))

callback(tokenRequest, nil)
}
}

let client = ARTRealtime(options:clientOptions)
```

**Objective-C**

```objective-c
ARTClientOptions *clientOptions = [[ARTClientOptions alloc] init];
clientOptions.authCallback = ^(ARTTokenParams *params, void(^callback)(id<ARTTokenDetailsCompatible>, NSError*)) {
[self getTokenRequestJSONFromYourServer:params completion:^(NSDictionary *json, NSError *error) {
//handle error
ARTTokenParams *tokenParams = [[ARTTokenParams alloc] initWithClientId:json[@"clientId"]];

ARTTokenRequest *tokenRequest = [[ARTTokenRequest alloc] initWithTokenParams:tokenParams
keyName:json[@"keyName"]
nonce:json[@"nonce"]
mac:json[@"mac"]];
tokenRequest.clientId = json[@"clientId"];
tokenRequest.ttl = [json[@"ttl"] doubleValue];
tokenRequest.capability = json[@"capability"];
tokenRequest.timestamp = [NSDate dateWithTimeIntervalSince1970:[json[@"timestamp"] doubleValue] / 1000];

callback(tokenRequest, nil);
}];
};

ARTRealtime *client = [[ARTRealtime alloc] initWithOptions:clientOptions];
```

## Using the REST API

### Introduction
Expand Down