The PayPal iOS SDK provides a simple and secure way to integrate PayPal payments into your iOS app. With this SDK, you can accept both PayPal and credit card payments, making it easier for users to make purchases within your app.
Prerequisites
In order to integrate the PayPal iOS SDK into your app, you’ll need the following:
- iOS 9.0 or later
- Xcode 11 or later
- A PayPal Business account
Installation
To get started with the PayPal iOS SDK, you can install it using the CocoaPods dependency manager:
“`ruby
# Add the PayPal iOS SDK to your Podfile
pod ‘PayPal-iOS-SDK’
“`
Then, run the following command in Terminal:
“`shell
$ pod install
“`
Configuration
After installing the PayPal iOS SDK, you’ll need to configure it in your app’s AppDelegate. Open your AppDelegate.swift file and add the following code:
“`swift
import PayPalMobile
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
PayPalMobile.initializeWithClientIds(forEnvironments: [PayPalEnvironmentProduction: “YOUR_PRODUCTION_CLIENT_ID”,
PayPalEnvironmentSandbox: “YOUR_SANDBOX_CLIENT_ID”])
return true
}
// Other AppDelegate methods…
}
“`
Replace “YOUR_PRODUCTION_CLIENT_ID” and “YOUR_SANDBOX_CLIENT_ID” with your respective client IDs obtained from the PayPal Developer Dashboard.
Usage
With the SDK configured, you can now start using it to accept payments in your app. Here’s a basic example:
- Import the PayPalMobile module:
“`swift
import PayPalMobile
“`
- Create a PayPalPaymentViewController:
“`swift
let payment = PayPalPayment(amount: NSDecimalNumber(string: “9.99”), currencyCode: “USD”, shortDescription: “Awesome Product”, intent: .sale)
let paymentViewController = PayPalPaymentViewController(payment: payment, configuration: PayPalConfiguration())
“`
- Present the paymentViewController:
“`swift
self.present(paymentViewController, animated: true, completion: nil)
“`
- Handle the payment response in your AppDelegate:
“`swift
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
if PayPalMobile.sharedInstance().handleOpen(url) {
return true
}
return false
}
“`
Additional Information
For more details on how to integrate PayPal payments into your iOS app, you can refer to the official PayPal iOS SDK documentation.
Note: The PayPal iOS SDK is no longer actively supported, and you may want to consider using the PayPal Native Checkout SDK for newer integrations.