BitlyDeveloper

Bitly iOS SDK

Required Mobile App Version: 2.9.4

Configure your iOS app

Before integrating the SDK you will need to configure your iOS app in Bitly. To do this, sign in to Bitly, open the profile menu, select Settings, click Mobile deep links below your account name, and click Add a mobile application.

When creating the iOS app you will need to provide your Apple ID. The Apple ID is your Apple Team ID and bundle ID joined with a period (for example, 1A234H7ABC.com.yourdomain.YourApp). To get your Team ID, log in to Apple’s Developer Portal and click Membership in the navigation menu. Your Team ID is displayed under Membership Details. If you choose to implement a Custom Scheme, you can also enter that during the app creation process.

Once the app is created, an app ID will be available on Bitly's app detail panel. Copy the ID for later in the setup.

To associate your app to your custom link domain in Bitly, you'll need to create an Apple Site Association File. To create the file, go to the profile menu, select Settings, and click Custom domains below your account name. Select your domain, click Configure in the Mobile behavior section, and then select the newly created iOS app. After saving the changes you can validate the file exists by going to https://<i></i>yourdomain.com/.well-known/apple-app-site-association.

Install the SDK

CocoaPods

  1. Add the following to your Podfile

    pod 'BitlySDK'
    
  2. Run the following to download and install the SDK

    pod install
    

Carthage

  1. Add the following to your Cartfile

    github "bitly/bitly_ios_sdk_release"
    
  2. Run the following to download and build the framework

    carthage update
    
  3. Drag the built BitlySDK.framework into your Xcode project

Configure the Universal Links

  1. In Xcode open your project file

  2. Go to the Capabilities tab

  3. Ensure that Associated Domains is enabled and expand it

  4. Click on the + button and add your domain as applinks:yourdomain.com

  5. Ensure that the yourprojectname.entitlements file is added to the appropriate build target

Configure the Custom Scheme

  1. In Xcode open your Info.plist

  2. Add a row for "URL types" as an array

  3. Add an entry to that array as "URL Schemes" also as an array

  4. Add a row with your custom scheme as the value

Custom Schemes are the older way of handling deep linking. However having an app that supports both Universal Links and Custom Schemes allows Bitly to work with Facebook App Links, Facebook Ads and other apps that launch content utilizing Custom Scheme deep links.

Configure the SDK for Deep Links

  1. Import the SDK in your AppDelegate

    import BitlySDK
  2. Initialize the SDK on application initialization

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        Bitly.initialize("YOUR_APP_ID", supportedDomains:["yourdomain.com","yourotherdomain.com"], supportedSchemes:["yourscheme"]) { response, error in
            // response provides a BitlyResponse object which contains the full URL information
            // response includes a status code
            // error provides any errors in retrieving information about the URL
            // Your custom logic goes here...
        }
        return true
    }

    By default the Bitly SDK utilizes the IDFV to identify the user. If you which to use the IDFA to track the user simply provide the deviceId to the appropriate method when initializing the SDK. You can also provide the access token for shortening with the accessToken provided to the initializing methods.

  3. Handle incoming links

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        return Bitly.handle(userActivity)
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return Bitly.handleOpen(url)
    }
  4. Retry on error

    Bitly.retryError(error)

    If there are any issues in handling a Universal Link the operation can be retried using the following line of code in your response handler from step 2

Configure the SDK for Shortening

  1. Configure an OAuth token for your app in your Bitly settings.

  2. Import the SDK in your AppDelegate

    import BitlySDK
  3. Initialize the SDK on application initialization

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        Bitly.initialize("YOUR_APP_ACCESS_TOKEN")
        return true
    }

    You can combine this initialization with the App Link initialization

  4. You can shorten from anywhere in your application

    Bitly.shorten("http://theurlyouwishtoshorten.com") { response, error in
        // response provides a BitlyResponse object which contains the shortened Bitlink
        // response includes a status code
        // error provides any errors in retrieving information about the URL
        // Your custom logic goes here...
    }