GitHub - mongodb/stitch-ios-sdk
source link: https://github.com/mongodb/stitch-ios-sdk
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
MongoDB Stitch iOS/Swift SDK
The official MongoDB Stitch SDK for iOS/Swift.
Index
Documentation
Discussion
Installation
XCode/iOS
CocoaPods
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 4.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 # For core functionality and the Remote MongoDB Service pod 'StitchSDK', '= 4.1.2' # optional: for using the AWS service pod 'StitchSDK/StitchAWSService', '= 4.1.2' # optional: for using the Firebase Cloud Messaging service pod 'StitchSDK/StitchFCMService', '= 4.1.2' # optional: for using the HTTP service pod 'StitchSDK/StitchHTTPService', '= 4.1.2' # optional: for using the twilio service pod 'StitchSDK/StitchTwilioService', '= 4.1.2' 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.
Example Usage
Creating a new app with the SDK (iOS)
Set up an application on Stitch
- Go to https://stitch.mongodb.com/ and log in to MongoDB Atlas.
- Create a new app in your project with your desired name.
- Go to your app in Stitch via Atlas by clicking Stitch Apps in the left side pane and clicking your app.
- Copy your app's client app id by going to Clients on the left side pane and clicking copy on the App ID section.
- Go to Providers from Users in the left side pane and edit and enable "Allow users to log in anonymously".
Set up a project in XCode/CocoaPods using Stitch
- Download and install XCode
- Create a new app project with your desired name. Ensure that Swift is the selected language.
- Navigate to the directory of the project in a command line, and run
pod init
. - In the
Podfile
that is generated, add the following line under the dependencies for your app target:
pod 'StitchSDK', '= 4.1.2'
- Run
pod install
- Open the generated
.xcworkspace
file - Your app project will have all the necessary dependencies configured to communicate with MongoDB Stitch.
- To use basic Stitch features,
import StitchCore
in a source file. - To access a remote MongoDB instance via Stitch,
import StitchRemoteMongoDBService
in a source file.
Using the SDK
Initialize the SDK
- When your app is initialized, run the following code to initialize the Stitch SDK. The
application(_:didFinishLaunchWithOptions)
method of yourAppDelegate.swift
can be an appropriate place for this initialization step. Be sure toimport StitchCore
.
// at the top of the file import StitchCore // in `application(_:didFinishLaunchWithOptions)` do { _ = try Stitch.initializeDefaultAppClient( withClientAppID: "your-client-app-id" ) } catch { print("Failed to initialize MongoDB Stitch iOS SDK: \(error)") // 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. }
- To get a client to use for logging in and communicating with Stitch, use
Stitch.defaultAppClient
.
// in a view controller's properties, for example private lazy var stitchClient = Stitch.defaultAppClient!
Logging In
- We enabled anonymous log in, so let's log in with it; add the following anywhere in your code:
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)") } }
- 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
Executing a Function
- Once logged in, executing a function happens via the StitchClient's
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))") } }
- If you've configured your Stitch application to have a function named "echoArg" that returns its argument, you should see a message like:
String result: Hello world!
Getting a StitchAppClient without Stitch.defaultAppClient
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 a 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")
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK