iOS
When people log into your app with Spaces, they can grant permissions to your app so you can retrieve information or perform actions on Spaces on their behalf.
The following steps are for adding Spaces Login to your iOS project.
1. Select an app or create a new app
Select an app or create a new one to pull info about your app into the following code snippets.
2. Set up Your Development Environment
Set up your development environment before using Spaces Login for iOS.
Using Swift Package Manager (SPM)
- In Xcode, click File > Swift Packages > Add Package Dependency.
- In the dialog that appears, enter the repository URL: https://github.com/spacesglobal/spaces-ios-sdk.
- In Version, enter the version number of the latest Spaces SDK for iOS.
- Complete the prompts to select the libraries you'd like to use in your project.
3. Register and Configure Your App with Spaces
Register and configure your app so you can use Spaces Login by adding your Bundle Identifier.
The bundle identifier (Bundle ID) should appear in the box below. If the box is empty, find your bundle identifier in your Xcode Project's iOS Application Target and paste it into the box below.
4. Configure Your Project
Configure the Info.plist file with an XML snippet that contains data about your app.
After you integrate Spaces Login, certain App Events are automatically logged and collected for Events Manager, unless you disable Automatic App Event Logging. For details about what information is collected and how to disable automatic app event logging, see Automatic App Event Logging.
- Right-click Info.plist, and choose Open As ▸ Source Code.
- Copy and paste the following XML snippet into the body of your file (
<dict>...</dict>).
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>SpacesAPP-ID</string>
</array>
</dict>
</array>
<key>SpacesAppID</key>
<string>APP-ID</string>
<key>SpacesClientToken</key>
<string>CLIENT-TOKEN</string>
<key>SpacesDisplayName</key>
<string>APP-NAME</string>
- In
<array><string>in the key[CFBundleURLSchemes], replace APP-ID with your App ID. - In
<string>in the key SpacesAppID, replace APP-ID with your App ID. - In
<string>in the key SpacesClientToken, replace CLIENT-TOKEN with the value found under Settings > Advanced > Client Token in your App Dashboard. - In
<string>in the key SpacesDisplayName, replace APP-NAME with the name of your app. - To use any of the Spaces dialogs (e.g., Login, Share, App Invites, etc.) that can perform an app switch to Spaces apps, your application's Info.plist also needs to include the following:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>spacesapi</string>
<string>spaces-messages-share-api</string>
</array>
You may directly set the automatic collection of App Events to “true” or “false” by adding SpacesAutoLogAppEventsEnabled as a key in Info.plist.
Your project will need to include the Keychain Sharing capability in order for login to work in Mac Catalyst applications.
- Select the + Capability button in the Signing & Capabilities tab when configuring your app target.
- Find and select the Keychain Sharing capability.
- Ensure that the Keychain Sharing capability is listed for the target.
5. Connect Your App Delegate
Replace the code in AppDelegate.swift method with the following code. This code initializes the SDK when your app launches, and allows the SDK to handle logins and sharing from the native Spaces app when you perform a Login or Share action. Otherwise, the user must be logged into Spaces to use the in-app browser to login.
// AppDelegate.swift
import UIKit
import SpacesCore
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
}
}
6. Add Spaces Login to Your Code
Use the Spaces Login button in your iOS app.
6a. Add Spaces Login to Your Code
To add a Spaces-branded Login button to your app, add the following code snippet to a view controller.
// Add this to the header of your file, e.g. in ViewController.swift
import SpacesLogin
// Add this to the body
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let loginButton = SpacesLoginButton()
loginButton.center = view.center
view.addSubview(loginButton)
}
}
At this point you should be able to run your app and log in using the Spaces Login button.
6b. Check Current Login Status
Your app can only have one person logged in at a time. We represent each person logged into your app with AccessToken.current.
The LoginManager sets this token for you and when it sets AccessToken.current it also automatically writes it to the keychain store.
The AccessToken contains userID which you can use to identify the user.
You should update your view controller to check for an existing token at load. This avoids unnecessary showing the login flow again if someone already granted permissions to your app:
override func viewDidLoad() {
super.viewDidLoad()
if let token = AccessToken.current,
!token.isExpired {
// User is logged in, do work such as go to next view controller.
}
}
6c. Ask for Permissions
When using Spaces Login, your app can ask for permissions on a subset of a person's data. Spaces Login requires advanced public_profile permission, to be used by external users.
Read Permissions for Spaces Login Button To request additional read permissions, set the permissions property on the SpacesLoginButton object.
// Extend the code sample from 6a. Add Spaces Login to Your Code
// Add to your viewDidLoad method:
loginButton.permissions = ["public_profile", "email"]
The user will be prompted to grant your app with the requested permissions. Note that some permissions will require a Login Review. See Managing Permissions for more information on permissions.
7. Next Steps
Congrats, you've added Spaces Login to your iOS app! Be sure to check out our other documentation pages for more advanced guides.
Implement a Data Deletion Callbackshare-external
Implement a data deletion callback to respond the people's request to delete their data from Spaces.
Add events to your app to view analytics, measure ad performance and build audiences for ad targeting.
Advanced Topics & Setupshare-external
Check out our advanced setup for Spaces Login for iOS apps.
Manage what data your app has access to through Spaces Login.
Check out how to responds to errors returned by the Spaces SDK.
Testing a Login Flowshare-external
Test and verify that your Spaces Login flow works.
Depending on the Spaces data you request from people using Spaces Login, you may need to submit your app for review prior to launch.
For building your own login flow, see Manually Build a Login Flow.