MongoDB Stitch Users - Google Group
MongoDB Stitch Announcements - Google Group
- Go to https://stitch.mongodb.com/ and log in
- Create a new app with your desired name
- Take note of the app's client App ID by going to Clients under Platform in the side pane
- Go to Authentication under Control in the side pane and enable "Allow users to log in anonymously"
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapods
CocoaPods 1.1.0+ is required to build Stitch iOS 0.2.0+.
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
pod 'StitchCore', '~> 3.0.2'
# optional: for accessing a mongodb client
pod 'MongoDBService', '~> 3.0.2'
# optional: for using mongodb's ExtendedJson
pod 'ExtendedJson', '~> 2.0.4'
end
Then, run the following command:
$ pod install
If you prefer not to use any of the aforementioned dependency managers, you can integrate the iOS SDK into your project manually.
-
Open up Terminal,
cd
into your top-level project directory, and run the following command "if" your project is not initialized as a git repository:$ git init
-
Add the iOS SDK as a git submodule by running the following command:
$ git submodule add https://github.com/10gen/stitch-ios-sdk.git
-
Open the new
stitch-ios-sdk
folder, and drag theStitchCore.xcodeproj
into the Project Navigator of your application's Xcode project.It should appear nested underneath your application's blue project icon. Whether it is above or below all the other Xcode groups does not matter.
-
Select the
StitchCore.xcodeproj
in the Project Navigator and verify the deployment target matches that of your application target. -
Next, select your application project in the Project Navigator (blue project icon) to navigate to the target configuration window and select the application target under the "Targets" heading in the sidebar.
-
In the tab bar at the top of that window, open the "General" panel.
-
Click on the
+
button under the "Embedded Binaries" section. -
You will see two different
StitchCore.xcodeproj
folders each with two different versions of theStitchCore.framework
nested inside aProducts
folder.It does not matter which
Products
folder you choose from, but it does matter whether you choose the top or bottomStitchCore.framework
. -
Select the top
StitchCore.framework
for iOS and the bottom one for OS X. -
For adding the other modules,
MongoDBService
,ExtendedJson
, orStitchGCM
, follow the same process above but with the respective.xcodeproj
files. -
And that's it!
The
StitchCore.framework
is automagically added as a target dependency, linked framework and embedded framework in a copy files build phase which is all you need to build on the simulator and a device.
-
To initialize our connection to Stitch, use the static
StitchClientFactory.create()
method to asynchronously create aStitchClient
that can be used to make requests to Stitch.StitchClientFactory .create(appId: "<your-client-app-id>") .done { (client: StitchClient) in // Perform requests to Stitch using the variable "client", // or assign the value of the variable "client" to some // property accessible outside this closure. // For example, if this is in a class which has a // stored StitchClient property named "stitchClient": self.stitchClient = client }.cauterize()
This will only instantiate a client but will not make any outgoing connection to Stitch.
-
For guidance on how to perform this initialization cleanly in the context of developing an iOS app, see the page Initialize StitchClient in the MongoDB Stitch documentation.
-
Since we enabled anonymous log in, let's log in with it; add the following after you've initialized your new
StitchClient
:self.stitchClient.fetchAuthProviders().then { (authProviderInfo: AuthProviderInfo) in if (authProviderInfo.anonymousAuthProviderInfo != nil) { print("logging in anonymously") return self.stitchClient.anonymousAuth() } else { print("no anonymous provider") } }.then { (userId: String) in print("logged in anonymously as user \(userId)") }.catch { error in print("failed to log in anonymously: \(error)") }
-
Now run your app in XCode by going to product, Run (or hitting ⌘R).
-
Once the app is running, open up the Debug Area by going to View, Debug Area, Show Debug Area.
-
You should see log messages like:
logging in anonymously logged in anonymously as user 58c5d6ebb9ede022a3d75050
-
Once logged in, executing a function happens via the StitchClient's
executeFunction()
methodself.stitchClient .executeFunction(name: "echoArg", args: "Hello world!") .done { (echoedArg: Any) in print(echoedArg as? String ?? "return value not a string") }.catch { error in print("Could not execute function: \(error)") }
-
If you've configured your Stitch application to have a function named "echoArg" that returns its argument, you should see a message like:
Hello world!
- Create a Firebase Project
- Click Add Firebase to your iOS app
- Skip downloading the config file
- Skip adding the Firebase SDK
- Click the gear next to overview in your Firebase project and go to Project Settings
- Go to Cloud Messaging and take note of your Legacy server key and Sender ID
- In Stitch go to the Notifications section and enter in your API Key (legacy server key) and Sender ID
-
Currently, StitchGCM needs to be added as a submodule.
-
To create a GCM Push Provider by asking Stitch, you must use the getPushProviders method and ensure a GCM provider exists:
self.stitchClient.getPushProviders().done { (result: AvailablePushProviders) in
if let gcm = result.gcm {
let listener = MyGCMListener(gcmClient: StitchGCMPushClient(stitchClient: self.stitchClient, info: gcm))
StitchGCMContext.sharedInstance().application(application,
didFinishLaunchingWithOptions: launchOptions,
gcmSenderID: "<YOUR-GCM-SENDER-ID>",
stitchGCMDelegate: listener)
}
}
- To begin listening for notifications, set your
StitchGCMDelegate
to the StitchGCMContext:
class MyGCMDelegate: StitchGCMDelegate {
let gcmClient: StitchGCMPushClient
init(gcmClient: StitchGCMPushClient) {
self.gcmClient = gcmClient
}
func didFailToRegister(error: Error) {
}
func didReceiveToken(registrationToken: String) {
}
func didReceiveRemoteNotification(application: UIApplication,
pushMessage: PushMessage,
handler: ((UIBackgroundFetchResult) -> Void)?
}
}
- To register for push notifications, use the registerToken method on your StitchClient:
func didReceiveToken(registrationToken: String) {
gcmClient.registerToken(token: registrationToken)
}