Ably iOS, tvOS and macOS Objective-C and Swift client library SDK
Ably is the platform that powers synchronized digital experiences in realtime. Whether attending an event in a virtual venue, receiving realtime financial information, or monitoring live car performance data – consumers simply expect realtime digital experiences as standard. Ably provides a suite of APIs to build, extend, and deliver powerful digital experiences in realtime for more than 250 million devices across 80 countries each month. Organizations like Bloomberg, HubSpot, Verizon, and Hopin depend on Ably’s platform to offload the growing complexity of business-critical realtime data synchronization at global scale. For more information, see the Ably documentation.
# For Xcode 7.3 and newer
github "ably/ably-cocoa" >= 1.2
And then run
for iOS: carthage update --use-xcframeworks --platform iOS --no-use-binaries
for macOS: carthage update --use-xcframeworks --platform macOS --no-use-binaries
for tvOS: carthage update --use-xcframeworks --platform tvOS --no-use-binaries
to build the framework and drag the built (in [PROJECT_ROOT]/Carthage/Build)
Ably.xcframework
AblyDeltaCodec.xcframework
msgpack.xcframework
into the “Frameworks, Libraries, and Embedded Content” section of the General tab of your Xcode target’s settings. If your target is an application, select “Embed & Sign”, otherwise “Do Not Embed”.
If you see, for example, a dyld: Library not loaded: @rpath/AblyDeltaCodec.framework/AblyDeltaCodec error, then most likely you forgot to add all the dependencies to your project. You have more detailed information here.
Manual installation
Get the code from GitHub from the release page, or clone it to get the latest, unstable and possibly underdocumented version: git clone git@github.com:ably/ably-cocoa.git
Drag the directory ably-cocoa/ably-cocoa into your project as a group.
The library makes the following thread-safety guarantees:
The whole public interface can be safely accessed, both for read and writing, from any thread.
“Value” objects (e. g. ARTTokenDetails, data from messages) returned by the library can be safely read from and written to.
Objects passed to the library must not be mutated afterwards. They can be safely passed again, or read from; they won’t be written to by the library.
All internal operations are dispatched to a single serial GCD queue. You can specify a custom queue for this, which must be serial, with ARTClientOptions.internalDispatchQueue.
All calls to callbacks provided by the user are dispatched to the main queue by default.
This allows you to react to Ably’s output by doing UI operations directly. You can specify a different queue with ARTClientOptions.dispatchQueue. It shouldn’t be the same queue as the ARTClientOptions.internalDispatchQueue, since that can lead to deadlocks.
ARTPushRegistererDelegate defines 3 delegate methods to handle the outcome of push activation, deactivation and update events. By default, the Ably SDK will check if UIApplication.sharedApplication.delegate conforms to ARTPushRegistererDelegate, and call the delegate methods when appropriate. Therefore, specifying the ARTPushRegistererDelegate is optional. To use a different class implementing ARTPushRegistererDelegate, you must provide this class to Ably, by setting the ARTClientOptions#pushRegistererDelegate delegate. In SwiftUI applications, you must set the ARTClientOptions#pushRegistererDelegate delegate property.
Only one instance of ARTRest or ARTRealtime at a time must be activated for receiving push notifications. Having more than one activated instance at a time may have unexpected consequences.
macOS & tvOS
Be aware that Push Notifications are currently unsupported for macOS and tvOS. You can only use the Push Admin functionalities, for example:
let recipient: [String: Any] = [
"clientId": "C04BC116-8004-4D78-A71F-8CA3122734DB"
]
let data: [String: Any] = [
"notification": [
"title": "Hello from Ably!",
"body": "Example push notification from Ably."
],
"data": [
"foo": "bar",
"baz": "qux"
]
]
realtime.push.admin.publish(recipient, data: data) { error in
print("Push published:", error ?? "nil")
}
Subscribing to a channel in delta mode enables delta compression. This is a way for a client to subscribe to a channel so that message payloads sent contain only the difference (ie the delta) between the present message and the previous message on the channel.
Request a Vcdiff formatted delta stream using channel options when you get the channel:
Swift
let channelOptions = ARTRealtimeChannelOptions()
channelOptions.params = [
"delta": "vcdiff"
]
let channel = client.channels.get("test", options: channelOptions)
Beyond specifying channel options, the rest is transparent and requires no further changes to your application. The message.data instances that are delivered to your subscription callback continue to contain the values that were originally published.
If you would like to inspect the ARTMessage instances in order to identify whether the data they present was rendered from a delta message from Ably then you can see if message.extras["delta"]["format"] equals "vcdiff".
channel.history { messagesPage, error in
let messagesPage = messagesPage!
print(messagesPage.items)
print(messagesPage.items.first)
print((messagesPage.items.first as? ARTMessage)?.data) // payload for the message
print(messagesPage.items.count) // number of messages in the current page of history
messagesPage.next { nextPage, error in
// retrieved the next page in nextPage
}
print(messagesPage.hasNext) // true, there are more pages
}
Objective-C
[channel history:^(ARTPaginatedResult<ARTMessage *> *messagesPage, ARTErrorInfo *error) {
NSLog(@"%@", messagesPage.items);
NSLog(@"%@", messagesPage.items.firstObject);
NSLog(@"%@", messagesPage.items.firstObject.data); // payload for the message
NSLog(@"%lu", (unsigned long)[messagesPage.items count]); // number of messages in the current page of history
[messagesPage next:^(ARTPaginatedResult<ARTMessage *> *nextPage, ARTErrorInfo *error) {
// retrieved the next page in nextPage
}];
NSLog(@"%d", messagesPage.hasNext); // true, there are more pages
}];
Presence on a channel
Swift
let channel = client.channels.get("test")
channel.presence.enter("john.doe") { errorInfo in
channel.presence.get { members, errorInfo in
// members is the array of members present
}
}
Objective-C
[channel.presence enter:@"john.doe" callback:^(ARTErrorInfo *errorInfo) {
[channel.presence get:^(ARTPaginatedResult<ARTPresenceMessage *> *result, ARTErrorInfo *error) {
// members is the array of members present
}];
}];
Querying the presence history
Swift
channel.presence.history { presencePage, error in
let presencePage = presencePage!
if let first = presencePage.items.first as? ARTPresenceMessage {
print(first.action) // Any of .Enter, .Update or .Leave
print(first.clientId) // client ID of member
print(first.data) // optional data payload of member
presencePage.next { nextPage, error in
// retrieved the next page in nextPage
}
}
}
Objective-C
[channel.presence history:^(ARTPaginatedResult<ARTPresenceMessage *> *presencePage, ARTErrorInfo *error) {
ARTPresenceMessage *first = (ARTPresenceMessage *)presencePage.items.firstObject;
NSLog(@"%lu", (unsigned long)first.action); // Any of ARTPresenceEnter, ARTPresenceUpdate or ARTPresenceLeave
NSLog(@"%@", first.clientId); // client ID of member
NSLog(@"%@", first.data); // optional data payload of member
[presencePage next:^(ARTPaginatedResult<ARTPresenceMessage *> *nextPage, ARTErrorInfo *error) {
// retrieved the next page in nextPage
}];
}];
Using the authCallback
A callback to call to obtain a signed token request. ARTClientOptions and ARTRealtime objects can be instantiated as follow:
Swift
let clientOptions = ARTClientOptions()
clientOptions.authCallback = { params, callback in
getTokenRequestJSONFromYourServer(params) { json, error in
//handle error
do {
callback(try ARTTokenRequest.fromJson(json), nil)
} catch let error as NSError {
callback(nil, error)
}
}
}
let client = ARTRealtime(options:clientOptions)
channel.history { messagesPage, error in
let messagesPage = messagesPage!
print(messagesPage.items.first)
print((messagesPage.items.first as? ARTMessage)?.data) // payload for the message
messagesPage.next { nextPage, error in
// retrieved the next page in nextPage
}
print(messagesPage.hasNext) // true, there are more pages
}
Objective-C
[channel history:^(ARTPaginatedResult<ARTMessage *> *messagesPage, ARTErrorInfo *error) {
NSLog(@"%@", messagesPage.items.firstObject);
NSLog(@"%@", messagesPage.items.firstObject.data); // payload for the message
NSLog(@"%lu", (unsigned long)[messagesPage.items count]); // number of messages in the current page of history
[messagesPage next:^(ARTPaginatedResult<ARTMessage *> *nextPage, ARTErrorInfo *error) {
// retrieved the next page in nextPage
}];
NSLog(@"%d", messagesPage.hasNext); // true, there are more pages
}];
Presence on a channel
Swift
channel.presence.get { membersPage, error in
let membersPage = membersPage!
print(membersPage.items.first)
print((membersPage.items.first as? ARTPresenceMessage)?.data) // payload for the message
membersPage.next { nextPage, error in
// retrieved the next page in nextPage
}
print(membersPage.hasNext) // true, there are more pages
}
Objective-C
[channel.presence get:^(ARTPaginatedResult<ARTPresenceMessage *> *membersPage, ARTErrorInfo *error) {
NSLog(@"%@", membersPage.items.firstObject);
NSLog(@"%@", membersPage.items.firstObject.data); // payload for the message
[membersPage next:^(ARTPaginatedResult<ARTMessage *> *nextPage, ARTErrorInfo *error) {
// retrieved the next page in nextPage
}];
NSLog(@"%d", membersPage.hasNext); // true, there are more pages
}];
Querying the presence history
Swift
channel.presence.history { presencePage, error in
let presencePage = presencePage!
if let first = presencePage.items.first as? ARTPresenceMessage {
print(first.clientId) // client ID of member
presencePage.next { nextPage, error in
// retrieved the next page in nextPage
}
}
}
Objective-C
[channel.presence history:^(ARTPaginatedResult<ARTPresenceMessage *> *presencePage, ARTErrorInfo *error) {
ARTPresenceMessage *first = (ARTPresenceMessage *)presencePage.items.firstObject;
NSLog(@"%@", first.clientId); // client ID of member
NSLog(@"%@", first.data); // optional data payload of member
[presencePage next:^(ARTPaginatedResult<ARTPresenceMessage *> *nextPage, ARTErrorInfo *error) {
// retrieved the next page in nextPage
}];
}];
Generate token
Swift
client.auth.requestToken(nil, withOptions: nil) { tokenDetails, error in
let tokenDetails = tokenDetails!
print(tokenDetails.token) // "xVLyHw.CLchevH3hF....MDh9ZC_Q"
let client = ARTRest(token: tokenDetails.token)
}
client.stats { statsPage, error in
let statsPage = statsPage!
print(statsPage.items.first)
statsPage.next { nextPage, error in
// retrieved the next page in nextPage
}
}
Objective-C
[client stats:^(ARTPaginatedResult<ARTStats *> *statsPage, ARTErrorInfo *error) {
NSLog(@"%@", statsPage.items.firstObject);
[statsPage next:^(ARTPaginatedResult<ARTStats *> *nextPage, ARTErrorInfo *error) {
// retrieved the next page in nextPage
}];
}];
Fetching the Ably service time
Swift
client.time { time, error in
print(time) // 2016-02-09 03:59:24 +0000
}
Ably iOS, tvOS and macOS Objective-C and Swift client library SDK
Ably is the platform that powers synchronized digital experiences in realtime. Whether attending an event in a virtual venue, receiving realtime financial information, or monitoring live car performance data – consumers simply expect realtime digital experiences as standard. Ably provides a suite of APIs to build, extend, and deliver powerful digital experiences in realtime for more than 250 million devices across 80 countries each month. Organizations like Bloomberg, HubSpot, Verizon, and Hopin depend on Ably’s platform to offload the growing complexity of business-critical realtime data synchronization at global scale. For more information, see the Ably documentation.
This is an iOS, tvOS and macOS Objective-C and Swift client library SDK for Ably, written in Objective-C. The library currently targets the Ably client library features spec Version 1.2. You can jump to the ‘Known Limitations‘ section to see the features this client library does not yet support or view our client library SDKs feature support matrix to see the list of all the available features.
Supported platforms
This SDK is compatible with projects that target:
We maintain compatibility and explicitly support these platform versions.
We do not explicitly maintain compatibility with older platform versions. Any known incompatibilities with older versions can be found here.
If you find any issues with unsupported platform versions, please raise an issue in this repository or contact Ably customer support for advice.
Continuous Integration Testing
We perform CI testing on the following operating systems:
macos-latest
runner image.Known Limitations
This client library is currently not compatible with some of the Ably features:
Documentation
Visit ably.com/docs for a complete API reference and more examples.
Installation Guide
You can install Ably for iOS and macOS through Package Manager, CocoaPods, Carthage or manually.
Installing through Swift Package Manager
ably-cocoa
package in your Xcode Project:https://github.com/ably/ably-cocoa
in the Swift Packages search box. ( Xcode project → Swift Packages.. . →+
button)Ably
SDK for your target.ably-cocoa
package in another Swift Package, then add the following to yourPackage.Swift
:Installing through CocoaPods
If you intend to use Swift, using
use_frameworks!
in your Podfile is recommended (this will create a Framework that can be used in Swift natively).Add this line to your application’s Podfile:
And then install the dependency:
Installing through Carthage
Add this line to your application’s Cartfile:
And then run
carthage update --use-xcframeworks --platform iOS --no-use-binaries
carthage update --use-xcframeworks --platform macOS --no-use-binaries
carthage update --use-xcframeworks --platform tvOS --no-use-binaries
to build the framework and drag the built (in
[PROJECT_ROOT]/Carthage/Build
)Ably.xcframework
AblyDeltaCodec.xcframework
msgpack.xcframework
into the “Frameworks, Libraries, and Embedded Content” section of the General tab of your Xcode target’s settings. If your target is an application, select “Embed & Sign”, otherwise “Do Not Embed”.
If you see, for example, a
dyld: Library not loaded: @rpath/AblyDeltaCodec.framework/AblyDeltaCodec
error, then most likely you forgot to add all the dependencies to your project. You have more detailed information here.Manual installation
git clone git@github.com:ably/ably-cocoa.git
ably-cocoa/ably-cocoa
into your project as a group.Thread-safety
The library makes the following thread-safety guarantees:
ARTTokenDetails
, data from messages) returned by the library can be safely read from and written to.All internal operations are dispatched to a single serial GCD queue. You can specify a custom queue for this, which must be serial, with
ARTClientOptions.internalDispatchQueue
.All calls to callbacks provided by the user are dispatched to the main queue by default. This allows you to react to Ably’s output by doing UI operations directly. You can specify a different queue with
ARTClientOptions.dispatchQueue
. It shouldn’t be the same queue as theARTClientOptions.internalDispatchQueue
, since that can lead to deadlocks.Push Notifications
If you haven’t yet, you should first check the detailed documentation. An example app for push notifications is also available.
Activation and device registration
For more information, see Push Notifications - Device activation and subscription.
ARTPushRegistererDelegate
defines 3 delegate methods to handle the outcome of push activation, deactivation and update events. By default, the Ably SDK will check ifUIApplication.sharedApplication.delegate
conforms toARTPushRegistererDelegate
, and call the delegate methods when appropriate. Therefore, specifying theARTPushRegistererDelegate
is optional. To use a different class implementingARTPushRegistererDelegate
, you must provide this class to Ably, by setting theARTClientOptions#pushRegistererDelegate
delegate. In SwiftUI applications, you must set theARTClientOptions#pushRegistererDelegate
delegate property.Do not forget that
ARTPush
has two corresponding methods that you should call from yours application(_:didRegisterForRemoteNotificationsWithDeviceToken:) and application(_:didFailToRegisterForRemoteNotificationsWithError:), passing to them also anARTRest
orARTRealtime
instance, configured with the authentication setup and other options you need:Only one instance of
ARTRest
orARTRealtime
at a time must be activated for receiving push notifications. Having more than one activated instance at a time may have unexpected consequences.macOS & tvOS
Be aware that Push Notifications are currently unsupported for macOS and tvOS. You can only use the Push Admin functionalities, for example:
Available demos: macOS and tvOS.
Using the Realtime API
Introduction
All examples assume a client has been created as follows:
Swift
Objective-C
Connection
Instantiating
ARTRealtime
starts a connection by default. You can catch connection success or error by listening to the connection’s state changes:Swift
Objective-C
You can also connect manually by setting the appropriate option.
Swift
Objective-C
Subscribing to a channel
Given:
Swift
Objective-C
Subscribe to all events:
Swift
Objective-C
Only certain events:
Swift
Objective-C
Subscribing to a channel in delta mode
Subscribing to a channel in delta mode enables delta compression. This is a way for a client to subscribe to a channel so that message payloads sent contain only the difference (ie the delta) between the present message and the previous message on the channel.
Request a Vcdiff formatted delta stream using channel options when you get the channel:
Swift
Objective-C
Beyond specifying channel options, the rest is transparent and requires no further changes to your application. The
message.data
instances that are delivered to your subscription callback continue to contain the values that were originally published.If you would like to inspect the
ARTMessage
instances in order to identify whether thedata
they present was rendered from a delta message from Ably then you can see ifmessage.extras["delta"]["format"]
equals"vcdiff"
.Publishing to a channel
Swift
Objective-C
Querying the history
Swift
Objective-C
Presence on a channel
Swift
Objective-C
Querying the presence history
Swift
Objective-C
Using the
authCallback
A callback to call to obtain a signed token request.
ARTClientOptions
andARTRealtime
objects can be instantiated as follow:Swift
Objective-C
Using the REST API
Introduction
All examples assume a client and/or channel has been created as follows:
Swift
Objective-C
Publishing a message to a channel
Swift
Objective-C
Querying the history
Swift
Objective-C
Presence on a channel
Swift
Objective-C
Querying the presence history
Swift
Objective-C
Generate token
Swift
Objective-C
Fetching your application’s stats
Swift
Objective-C
Fetching the Ably service time
Swift
Objective-C
Support, feedback and troubleshooting
Please visit https://support.ably.com/ for access to our knowledgebase and to ask for any assistance.
You can also view the community reported Github issues.
Contributing
See CONTRIBUTING.md for details.