Set up your iOS client

DOCS

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:

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

  1. Add PayPal to your Podfile.

      1target 'MyApp' do
      2 pod 'PayPal'
      3end
    1. Run pod install.

    Use Carthage

    1. Add github "paypal/ios-sdk" to your Cartfile.

    2. Add the following frameworks to your project:

        1PayPal.framework
        2BraintreeApplePay.framework
        3BraintreeCard.framework
        4BraintreeCore.framework
        5BraintreePaymentFlow.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

      1. In Xcode, select your project in the Project Navigator and go to App Target > Info > URL Types.

      2. Select [+] to add a new URL type.

      3. 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 be com.your-company.your-app.payments.

      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.

      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 true
        6}

        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 in
          3 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 false
            7}

            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() {
              2
              3 // Switch this URL to your own authenticated API
              4 let lowScopeAuthURL = URL(string: "https://my-merchant-server.com/client-id-token")!
              5
              6 URLSession.shared.dataTask(with: lowScopeAuthURL) { (data, response, error) -> Void in
              7 // Handle errors
              8 // Obtain the token
              9 self.clientToken = String(data: data!, encoding: String.Encoding.utf8)
              10
              11 }.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 & UI
                3
                4 // Present the provided view controller
                5 self.present(viewController, animated: true)
                6}
                7
                8func paymentDriver(_ driver: Any, requestsDismissalOf viewController: UIViewController) {
                9 // Perform any necessary app clean up & UI
                10
                11 // Dismiss the provided view controller
                12 viewController.dismiss(animated: true)
                13}

                Next step

                Set up a payment