Stitch SDK functionality has been moved into the Realm SDKs. Applications using a Stitch SDK can be migrated to the corresponding Realm SDK by using one of the Stitch SDK Migration Guides. Support for the Stitch SDKs will fully end on November 1, 2021.
The official MongoDB Stitch SDK for iOS/Swift.
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
To integrate the iOS SDK into your Xcode project using CocoaPods, specify it in your Podfile
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!
target '<Your Target Name>' do
# For core functionality and the Remote MongoDB Service
pod 'StitchSDK', '= 6.4.0'
# optional: for using the AWS service
pod 'StitchSDK/StitchAWSService', '= 6.4.0'
# optional: for using the Firebase Cloud Messaging service
pod 'StitchSDK/StitchFCMService', '= 6.4.0'
# optional: for using the HTTP service
pod 'StitchSDK/StitchHTTPService', '= 6.4.0'
# optional: for using the twilio service
pod 'StitchSDK/StitchTwilioService', '= 6.4.0'
end
Then, run the following command:
$ pod install
Open the .xcworkspace
file generated by pod install
to access your project with all of its necessary Stitch dependencies automatically linked.
Download and install Xcode.
Create a new app project with your desired name. Ensure that Swift is the selected language.
pod init
.Podfile
that is generated, add the following line under the dependencies for your app target: pod 'StitchSDK', '= 6.4.0'
See above for the list of optional Stitch service pods that you may add to your Podfile.
pod install
..xcworkspace
file. Your app project will have all the necessary dependencies configured to communicate with MongoDB Stitch.import StitchCore
in a source file.import MongoSwift
in a source file.import StitchRemoteMongoDBService
in a source file.application(_:didFinishLaunchWithOptions)
method of your AppDelegate.swift
can be an appropriate place for this initialization step. Be sure to import StitchCore
. // at the top of the file
import StitchCore
// ...
// in `application(_:didFinishLaunchWithOptions)`
do {
_ = try Stitch.initializeDefaultAppClient(
withClientAppID: "your-client-app-id"
)
print("Successfully initialized default Stitch app client!");
} catch {
// note: This initialization will only fail if an incomplete configuration is
// passed to a client initialization method, or if a client for a particular
// app ID is initialized multiple times. See the documentation of the "Stitch"
// class for more details.
print("Failed to initialize MongoDB Stitch iOS SDK: \(error)")
}
Stitch.defaultAppClient
. // in a view controller's properties, for example
private lazy var stitchClient = Stitch.defaultAppClient!
let client = Stitch.defaultAppClient!
print("logging in anonymously")
client.auth.login(withCredential: AnonymousCredential()) { result in
switch result {
case .success(let user):
print("logged in anonymous as user \(user.id)")
DispatchQueue.main.async {
// update UI accordingly
}
case .failure(let error):
print("Failed to log in: \(error)")
}
}
logging in anonymously
logged in anonymously as user 58c5d6ebb9ede022a3d75050
callFunction()
method: client.callFunction(
withName: "echoArg", withArgs: ["Hello world!"], withRequestTimeout: 5.0
) { (result: StitchResult<String>) in
switch result {
case .success(let stringResult):
print("String result: \(stringResult)")
case .failure(let error):
print("Error retrieving String: \(String(describing: error))")
}
}
// Stitch Function called 'echoArg'
exports = function(arg) {
return arg;
};
Then you should see a message in the Xcode Debug Area like:
String result: Hello world!
In the case that you don't want a single default initialized StitchAppClient, you can use the following with as many client app IDs as you'd like to initialize clients for multiple app IDs:
do {
let client1 = try Stitch.initializeAppClient(withClientAppID: "your-first-client-app-id")
let client2 = try Stitch.initializeAppClient(withClientAppID: "your-second-client-app-id")
} catch {
print("Failed to initialize MongoDB Stitch iOS SDK: \(error.localizedDescription)")
}
You can use the client returned there or anywhere else in your app you can use the following:
let client1 = try! Stitch.appClient(forAppID: "your-first-client-app-id")
let client2 = try! Stitch.appClient(forAppID: "your-second-client-app-id")