Set up your iOS client
Last updated: Sept 23rd, 5:46pm
Set up your iOS client-side SDK as part of the iOS SDK workflow. The PayPal native iOS SDK uses the Braintree client SDK with PayPal's Orders v2 API.
Know before you code
You need the following:
- Xcode 10+.
- iOS 9.0+ deployment target.
- PayPal API credentials.
Step 1: Choose your integration method
You can integrate the SDK using CocoaPods or Carthage. See the GitHub repository to access the SDK source code.
Use CocoaPods
-
Add
PayPalto your Podfile.1target 'MyApp' do2 pod 'PayPal'3end -
Run
pod install.
Use Carthage
-
Add
github "paypal/ios-sdk"to your Cartfile. -
Add the following frameworks to your project:
1PayPal.framework2BraintreeApplePay.framework3BraintreeCard.framework4BraintreeCore.framework5BraintreePaymentFlow.framework
Step 2: Set up app switching
For payment flows that switch to SFSafariViewController for authentication, register a URL type and set up your app to return from the app switches.
These flows include PayPal checkout and card checkout flows that result in a 3D Secure challenge. The SDK uses the Braintree BTAppSwitch to route the returnURL back to your app.
Register a URL type
-
In Xcode, select your project in the Project Navigator and go to App Target > Info > URL Types.
-
Select [+] to add a new URL type.
-
Under URL Schemes, enter your app switch return URL scheme.
This scheme must start with your app's bundle ID and must be dedicated to PayPal app switch returns. For example, if the app bundle ID is
com.your-company.your-app, then your URL scheme could becom.your-company.your-app.payments.Important: Make sure your URL scheme is all lowercase. If you have multiple app targets, add the return URL type for each target.
Test the URL type
On your iOS device or simulator, launch the URL <your URL scheme>://test in a mobile web browser.
For example, if your URL scheme is com.companyx.appz.payments, go to com.companyx.appz.payments://test.
Note: If you use a simulator to test, make sure to test that app-switching works on a real device after you integrate the SDK.
Set the return URL scheme
In your application delegate's application:didFinishLaunchingWithOptions:, set up the BTAppSwitch.setReturnURLScheme with your registered URL type.
1func application(_ application: UIApplication,2 didFinishLaunchingWithOptions launchOptions:3 [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {4 BTAppSwitch.setReturnURLScheme("com.your-company.your-app.payments")5 return true6}
Handle app switch
Pass the payment authorization URL to Braintree to finalize.
If you use UISceneDelegate (iOS 13 or newer), call BTAppSwitch's handleOpenURLContext method from the scene:openURLContexts scene delegate method.
1func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {2 URLContexts.forEach { context in3 if context.url.scheme?.localizedCaseInsensitiveCompare("com.my-app.your-app.payments") == .orderedSame {4 BTAppSwitch.handleOpenURLContext(context)5 }6 }7}
If you don't use UISceneDelegate, call BTAppSwitch's handleOpenURL method from the application:openURL:options app delegate method.
1func application(_ app: UIApplication, open url: URL,2 options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {3 if url.scheme?.localizedCaseInsensitiveCompare("com.your-company.your-app.payments") == .orderedSame {4 return BTAppSwitch.handleOpen(url, options: options)5 }6 return false7}
Step 3: Fetch a client token to initialize the SDK
Send the client-side token that was generated from your server to your client.
1func fetchClientToken() {23 // Switch this URL to your own authenticated API4 let lowScopeAuthURL = URL(string: "https://my-merchant-server.com/client-id-token")!56 URLSession.shared.dataTask(with: lowScopeAuthURL) { (data, response, error) -> Void in7 // Handle errors8 // Obtain the token9 self.clientToken = String(data: data!, encoding: String.Encoding.utf8)1011 }.resume()12}
Step 4: Create a PYPLClient
Use the client token to create the PYPLClient.
// Create a PYPLClient self.client = PYPLClient(accessToken: self.clientToken)
Conform to BTViewControllerPresentingDelegate
During the checkout process, you can present other view controllers to the buyer. By conforming to this protocol, you are notified of these transitions.
The BTViewControllerPresentingDelegate is a required delegate that is responsible for presenting and dismissing an SFSafariViewController. Implement this by using the following:
1func paymentDriver(_ driver: Any, requestsPresentationOf viewController: UIViewController) {2 // Perform any necessary setup & UI34 // Present the provided view controller5 self.present(viewController, animated: true)6}78func paymentDriver(_ driver: Any, requestsDismissalOf viewController: UIViewController) {9 // Perform any necessary app clean up & UI1011 // Dismiss the provided view controller12 viewController.dismiss(animated: true)13}