Collect physical addresses and phone numbers
Learn how to collect customer addresses and phone numbers in your mobile app with the Address Element.
To collect complete addresses for billing or shipping, use the Address Element.
You can also use the Address Element to:
- Collect customer phone numbers
- Utilize autocomplete (enabled by default)
- Prefill billing information in the Payment Element by passing in a shipping address
Stripe combines the collected address information and the payment method to create a PaymentIntent.
Set up Stripe Server-side Client-side
First, you need a Stripe account. Register now.
The Stripe iOS SDK is open source, fully documented, and compatible with apps supporting iOS 13 or above.
To install the SDK, follow these steps:
- In Xcode, select File > Add Package Dependencies… and enter https://github. the relevant part of the product as the repository URL.
- Select the latest version number from our releases page .
- Add the StripePaymentSheet product to the target of your app .
Note
For details on the latest SDK release and past versions, see the Releases page on GitHub. To receive notifications when a new release is published, watch releases for the repository.
Configure the SDK with your Stripe publishable key on app start. This enables your app to make requests to the Stripe API.
AppDelegate.swift
Swift
import UIKit
import StripePaymentSheet
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
StripeAPI.defaultPublishableKey = "pk_test_TYooMQauvdEDq54NiTphI7jx"
// do any other necessary launch configuration
return true
}
}
Note
Use your test keys while you test and develop, and your live mode keys when you publish your app.
Set up address autocomplete suggestions
Stripe provides autocomplete suggestions automatically when your customer selects a supported country. No additional integration steps are required.
Note
By using autocomplete, you agree to comply with the Google Maps Platform Acceptable Use Policy. If you violate this policy, Stripe might disable autocomplete or take other necessary action.
Configure the Address Element
You can configure the Address Element with details such as displaying default values, setting allowed countries, customizing the appearance, and so on. Refer to AddressViewController.Configuration for the complete list of configuration options.
let addressConfiguration = AddressViewController.Configuration(
additionalFields: .init(phone: .required),
allowedCountries: ["US", "CA", "GB"],
title: "Shipping Address"
)
Retrieve address details
Retrieve the address details by conforming to AddressViewControllerDelegate and then using addressViewControllerDidFinish to dismiss the view controller. The address value is either a valid address or nil.
extension MyViewController: AddressViewControllerDelegate {
func addressViewControllerDidFinish(_ addressViewController: AddressViewController, with address: AddressViewController.AddressDetails?) {
addressViewController.dismiss(animated: true)
self.addressDetails = address
}
}
Present the Address Element
Create an AddressViewController using the address configuration and delegate from the previous steps. You can either present it in a navigation controller or push it onto a navigation controller.
self.addressViewController = AddressViewController(configuration: addressConfiguration, delegate: self)
let navigationController = UINavigationController(rootViewController: addressViewController)
present(navigationController, animated: true)
