Before you can run sales, you need to set up your project and connect your app to Mobile Sync.
Note: The code examples in these guides use placeholder functions, such as showMessage(_:), for your app's own user interface.
Integration steps
Step 1. Set up your project
To get started, download our App Switch Kit and add it to your project. You also need to register your URL schemes so that your app and Mobile Sync can open each other.
Download the App Switch Kit
- Contact our Integrations Team at payroc-integrations@payroc.com and ask for the latest version of the App Switch Kit. We send it as a zip file.
- Extract the contents of the zip file. The zip file contains the following folders:
| Folder | Description |
|---|---|
| AppSwitchKit/ | The Swift package that contains the kit. |
| AppSwitchingSample/ | A sample app that uses the kit. |
| docs/ | The kit's API reference. |
Add the kit to your Xcode project
- In Xcode, select File > Add Package Dependencies.
- Select Add Local, and then select the AppSwitchKit folder.
- Select your app target, open the Build Settings tab, and then set Default Actor Isolation to MainActor. The App Switch Kit and the code examples in these guides run on the main actor.
- Add the following products to your app target:
| Product | Description | Required or recommended |
|---|---|---|
AppSwitchContract | Library that contains the signals and URL formats that Mobile Sync expects. | Required |
AppSwitchOrchestration | Library that opens Mobile Sync, waits until it's ready, and handles pairing. | Recommended |
AppSwitchGatewayReference | Gateway client that authenticates your requests, sends instructions, checks their status, and gets the result. Our guides use it in every example. | Recommended |
Note: If you already have a client for the Payroc API, you can use it instead of AppSwitchGatewayReference.
Register your URL schemes
iOS apps use URL schemes to interact with each other. Register the following URL schemes for your app and Mobile Sync:
- Your app's URL scheme - Mobile Sync uses this scheme to return the merchant to your app.
- Mobile Sync's URL scheme - Your app uses this scheme to check that Mobile Sync is installed. For testing, use
com.payroc.uat.switch. When you're ready to run live sales, usecom.payroc.switch.
We recommend that you choose a scheme for your app that other apps are unlikely to use, for example, a scheme that's based on your bundle ID.
To register the URL schemes, add them to your Info.plist file:
<!-- Your app's URL scheme: Mobile Sync uses it to return to your app. -->
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.yourcompany.yourapp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>yourscheme</string>
</array>
</dict>
</array>
<!-- Mobile Sync's URL scheme: your app uses it to check that Mobile Sync is installed. -->
<key>LSApplicationQueriesSchemes</key>
<array>
<string>com.payroc.uat.switch</string> <!-- Use com.payroc.switch in your production app. -->
</array>
In your code, define the URLs that you pass to the kit. The result and identity values in the URL tell your app whether it's receiving a sale response or a pairing response. They aren't entries in your Info.plist file.
Important: The scheme in Callback.scheme must match the scheme that you registered in CFBundleURLTypes.
/// Mobile Sync's URL scheme.
enum MobileSync {
static let scheme = "com.payroc.uat.switch" // Use com.payroc.switch in your production app.
}
/// Your app's URL scheme. Mobile Sync opens these URLs to return to your app.
enum Callback {
static let scheme = "yourscheme"
static let sale = "\(scheme)://result" // return from a sale or signature capture
static let pairing = "\(scheme)://identity" // return from pairing
}
Note: When you're ready to run live sales, replace com.payroc.uat.switch with com.payroc.switch.
Step 2. Install the Mobile Sync UAT app
Use the Mobile Sync UAT app to test your integration before you go live. Mobile Sync UAT is a separate app from the live Mobile Sync and has its own switch scheme, so you can install both apps on one iPhone. It connects to the UAT gateway, so use a test terminal ID and the Payroc API key and Mobile Sync API key for that environment.
Mobile Sync UAT is a beta app that we distribute through TestFlight and requires a Mobile Sync API key. To install the Mobile Sync UAT app, complete the following steps:
- Email payroc-integrations@payroc.com and ask to be invited to the Mobile Sync UAT beta. Include your company name and the email address of the Apple Account that you want us to invite.
- Accept the TestFlight invitation, and then install Mobile Sync UAT on the iPhone that you test with.
- Install the Proximity Reader Certification configuration profile on the same iPhone. Mobile Sync UAT points at the UAT environment, so it needs this profile. Our Integrations Team provides the profile.
- Use your Mobile Sync API key to set up Mobile Sync UAT for Payroc Cloud mode. Until you set up Mobile Sync, pairing returns
.unprovisioned. - Point your app at the UAT switch scheme,
com.payroc.uat.switch. For more information, go to Register your URL schemes.
Important: Register com.payroc.uat.switch in your test builds only. Your production app should register and open only the production scheme, com.payroc.switch.
Step 3. Pair your app with Mobile Sync on an iPhone
On each iPhone, you need to pair your app with Mobile Sync. When pairing finishes, Mobile Sync sends a reader identifier to your app in the identity URL that it opens. The kit reads the identifier from the URL and stores it, and your app uses it as the device ID in every sale request.
You need to pair your app only once for each iPhone. If pairing later stops working, you must pair your app again. For more information, go to The reader identifier is out of date.
Until pairing is successful, disable the buttons in your app that start a sale or a signature capture.
SwiftUI doesn't update your view when pairing.isPaired changes, because AppSwitchPairing isn't observable. Store the value in your own observable state, and update it each time that your app pairs or unpairs. The isPaired value and the unpair() function in Send a pairing request do this.
Important: Make isPaired a property of an @Observable class, such as your view model. If it's a plain variable, SwiftUI never enables the button. The sample app keeps it in PaymentViewModel.
For example:
// isPaired is the value that you define in Send a pairing request. pay is from Run a sale.
Button("Pay") { pay(amountInCents: 1000, terminalId: terminalId, apiKey: apiKey) { openURL($0) } } // $10.00
.disabled(!isPaired) // enabled when pairing succeeds
Send a pairing request
To pair your app with Mobile Sync, use pairURL(targetScheme:callback:) to build the URL and pass it to openURL.
Important: Before every switch to Mobile Sync, including pairing, call canOpenURL to check that Mobile Sync is installed. If Mobile Sync isn't installed, opening the URL does nothing.
import AppSwitchOrchestration
import UIKit
/// Stores the reader identifier from Mobile Sync in UserDefaults.
let pairing = AppSwitchPairing()
/// Make this a property of an @Observable class, such as your view model, so that SwiftUI updates your buttons.
var isPaired = pairing.isPaired
/// Forgets the reader identifier, and disables the buttons that start a sale.
func unpair() {
pairing.unpair()
isPaired = false
}
func pair(openURL: (URL) -> Void) {
guard let url = pairing.pairURL(targetScheme: MobileSync.scheme, callback: Callback.pairing) else { return }
// If Mobile Sync isn't installed, opening the URL silently does nothing.
guard UIApplication.shared.canOpenURL(url) else {
showMessage("Install Mobile Sync, then pair again.")
return
}
openURL(url)
}
Handle the pairing response
When Mobile Sync finishes the pairing request, it opens your identity URL. Pass the URL to your pairing handler:
import AppSwitchContract
.onOpenURL { url in
guard AppSwitchURL.isCallback(url, callbackScheme: Callback.scheme),
AppSwitchURL.isIdentityReturn(url) else { return }
handlePairingReturn(url)
}
Mobile Sync returns one of the following responses:
| Response | Description | What to do |
|---|---|---|
.paired | Mobile Sync sent its identifier, and the kit stored it. | Enable the buttons that start a sale or a signature capture. |
.unprovisioned | Mobile Sync is installed but not set up. | Ask the merchant to finish setting up Mobile Sync. |
.unavailable | Mobile Sync is offline, or couldn't connect within 60 seconds. | Keep any existing pairing. Ask the merchant to check the connection, and then pair again. |
status=readerUnavailable | Mobile Sync's card reader couldn't start. handleReturn doesn't return this value, so check for it first, as in the following code. | Keep any existing pairing. Ask the merchant to open Mobile Sync for details, and then pair again. |
.noIdentity | The response contained an error, or no identifier. | Show the error message. |
| No response | The merchant left Mobile Sync during pairing, so it sent nothing back. | When your app becomes active again, wait about 1 second. If no response arrives, show "Pairing didn't finish. Open Mobile Sync and try again." |
import AppSwitchContract
import AppSwitchOrchestration
func handlePairingReturn(_ url: URL) {
// Check for readerUnavailable first. Otherwise, the kit caches the identifier.
if AppSwitchURL.errorCode(from: url) == nil,
AppSwitchURL.returnStatus(from: url) == .readerUnavailable {
showMessage("Mobile Sync's card reader couldn't start. Open Mobile Sync for details, then pair again.")
return
}
switch pairing.handleReturn(url) {
case .paired:
showMessage("Paired with Mobile Sync.")
case .unprovisioned:
showMessage("Finish setting up Mobile Sync, then pair again.")
case .unavailable:
showMessage("Mobile Sync couldn't connect. Check the internet connection and try again.")
case .noIdentity:
// An error response. Nothing is cached. For errorMessage(for:), see Handle errors.
showMessage(errorMessage(for: AppSwitchURL.errorCode(from: url)))
}
isPaired = pairing.isPaired // update your observable state so that SwiftUI enables the buttons
}
Troubleshooting
| Problem | What to check |
|---|---|
| Mobile Sync doesn't open | com.payroc.uat.switch or com.payroc.switch is in LSApplicationQueriesSchemes. |
| Mobile Sync doesn't return to your app | Your scheme is in CFBundleURLTypes, and it matches Callback.scheme. |
Next step
After you pair your app with Mobile Sync, program your app to run a sale.