- Stitch >
- Client SDKs
Initialize the StitchAppClient¶
Overview¶
In each of the MongoDB Stitch SDKs, there is a StitchAppClient
class that
offers functionality for communicating with the Stitch backend.
This functionality includes methods for authentication, executing functions,
and accessing services. In order to setup an Android,
iOS, or Web Application to access the StitchAppClient
consult the
following steps.
- Javascript SDK (In Browser)
- JavaScript SDK (React)
- Android SDK
- iOS SDK
StitchAppClient
The Stitch class is the entry point into the Stitch Javascript In-Browser API. Using this class, you can instantiate a StitchAppClient instance to interact with Stitch.
You can obtain a StitchAppClient
instance in one of two ways:
- Default
StitchAppClient
- Use the static Stitch.initializeDefaultAppClient()
method to initialize the default
StitchAppClient
. You typically will call this at the top of your application. - Custom
StitchAppClient
Some users may need to connect to several different Stitch applications, or customize their
StitchAppClient
instance to use more advanced features of the Stitch SDK.To do this, you must initialize it using the static Stitch.initializeAppClient() method.
StitchAppClient
The Stitch class is the entry point into the Stitch Javascript In-Browser API. Using this class, you can instantiate a StitchAppClient instance to interact with Stitch.
You can obtain a StitchAppClient
instance in one of two ways:
- Default
StitchAppClient
- Use the static Stitch.initializeDefaultAppClient()
method to initialize the default
StitchAppClient
. You typically will call this at the top of your application. - Custom
StitchAppClient
Some users may need to connect to several different Stitch applications, or customize their
StitchAppClient
instance to use more advanced features of the Stitch SDK.To do this, you must initialize it using the static Stitch.initializeAppClient() method.
StitchAppClient
The Stitch class is the entry point into the Stitch Android API. Using this class, you can instantiate a StitchAppClient instance to interact with Stitch.
You can obtain a StitchAppClient
instance in one of two ways:
- Default
StitchAppClient
- Use the static Stitch.getDefaultAppClient()
method to obtain the default
StitchAppClient
initialized at application start using the Stitch App ID specified in your Android application’sstrings.xml
file. - Custom
StitchAppClient
Some users may need to connect to several different Stitch applications, or customize their
StitchAppClient
instance to use more advanced features of the Stitch SDK.To do this, you must initialize it using the static Stitch.initializeAppClient() method.
StitchAppClient
The Stitch class is the entry point into the Stitch iOS API. Using this class, you can instantiate a StitchAppClient instance to interact with Stitch.
You can obtain a StitchAppClient
instance in one of two ways:
- Default
StitchAppClient
Use the static Stitch.initializeDefaultAppClient method to initialize the default Stitch
StitchAppClient
client. You should typically call this in the application(_:didFinishLaunchingWithOptions:) method of your project’sAppDelegate
.You can then use the static Stitch.defaultAppClient property.
- Custom
StitchAppClient
Some users may need to connect to several different Stitch applications, or customize their
StitchAppClient
instance to use more advanced features of the Stitch SDK.To do this, you must initialize it using the static Stitch.initializeAppClient() method.
Procedure¶
The following sample code demonstrates how to add StitchAppClient
to
a vanilla JavaScript and HTML project. You should adapt it to fit your application’s
architecture.
For Stitch Core SDK functionality and the Remote MongoDB Service, import the following
For additional and more customized dependencies, import any of the following
Initialize the app client by calling the Stitch.initializeDefaultAppClient() method with your application’s App ID
You can use the app client to authenticate with Stitch, call functions, and generate service clients, as in the following example:
React is a widely used JavaScript library for
building user interfaces. The following sample code demonstrates how to add StitchAppClient
to
a React project. You should adapt it to fit your application’s architecture.
Install NPM if you hve not already done so. You can check if you have npm by running the following shell command:
For Stitch Core SDK functionality and the Remote MongoDB Service, run the following command in the root directory of your NPM project:
For additional and more customized dependencies, instead use the following:
Initialize the app client by calling the Stitch.initializeDefaultAppClient() method with your application’s App ID
You can use the app client to authenticate with Stitch, call functions, and generate service clients, as in the following example:
For a more comprehensive view of how the StitchAppClient
can be
initialized and used in a React application, view the
To-Do Web App tutorial and associated
sample code.
The following sample code demonstrates how to add StitchAppClient
to
an Android project. You should adapt it to fit your application’s
architecture.
We need to add the Stitch Android SDK to our application before we can initialize a client. Add the following line to the Android block of your project’s build.gradle to import the core SDK as well as the Remote MongoDB service client:
Important
Stitch supports Android projects that target SDK Version 21 and above. Ensure that the minimum target SDK version for your project falls in this range. You can specify the minimum supported version when you first create your Android project or modify the value for an existing project by setting the
minSdkVersion
field inbuild.gradle
.If you want to use services other than MongoDB, or have more customized dependencies, make sure to add that service’s client to
build.gradle
in addition to the main SDK implementation:Add the following Java compilation settings to the Android block of your project’s build.gradle file:
The following XML resource configures your application name and points Stitch to your application. Copy the resource XML to your
strings.xml
resource file, change the application name value, and replaceSTITCH-APP-ID
with your application’s App ID. You can find your App ID on the Clients page of the Stitch UI.Note
Adding your App ID to your
strings.xml
resource file is optional. You may skip this step and instead make use of the ‘ Stitch.initializeDefaultAppClient() should you want.The Stitch Android SDK will automatically initialize a default app client based on the App ID you specify in
strings.xml
. The following sample code demonstrates how to access and use this default StitchAppClient from an Android Activity.Note
You may need to adjust this code to fit your particular application’s architecture.
Warning
When it comes to authentication events such as logging in or logging
out, the StitchAppClient
class is not thread-safe. If your
application can perform authentication events from
different threads simultaneously, be sure to use a
Lock
to protect authentication calls to the StitchAppClient
.
The following sample code demonstrates how to add StitchAppClient
to
an iOS project. You should adapt it to fit your application’s
architecture.
Install CocoaPods if you have not already done so:
Note
CocoaPods 1.1.0+ is required to build Stitch iOS 4.0+
Initialize a Podfile by running the following shell command from the project directory:
Specify the iOS SDK in the Podfile you initialized to integrate it with your project:
Install the updated Podfile by running the following shell command from the project directory:
Note
You will need to re-run
pod install
to install new pods any time you update your Podfile.Open the
.xcworkspace
file generated bypod install
to access your project with all of its necessary Stitch dependencies automatically linked.Initialize the app client by calling the Stitch.initializeDefaultAppClient method with your application’s App ID in the application(_:didFinishLaunchingWithOptions:) method of your project’s
AppDelegate
:Once you have initialized the default app client, you can use the static Stitch.defaultAppClient property to access it. You can use the app client to authenticate with Stitch, call functions, and generate service clients, as in the following example:
Warning
When it comes to authentication events like logging in or logging
out, the StitchAppClient
class is not thread-safe. If your application can perform
authentication events from different threads simultaneously, be sure
to use an
NSLock
to protect authentication calls to the StitchAppClient
.