ARTICLES

How to open an iOS app with custom URL

This tutorial shows you how to open another iOS app from within your iOS app or from the safari browser using a custom URL scheme.

Beginning with iOS 9, you must whitelist the apps you want to open.  If you do not, you will get the error -canOpenURL: failed for URL: “customURLScheme://” – error: “This app is not allowed to query for scheme”.  If you get this error skip to the beginning of step 3.

Step 1 – Register your custom URL Scheme

If the app you wish to open belongs to you then you need to register it’s custom url scheme.  If you are opening someone else’s app then skip to step 2.

  1. Use Xcode’s Project Navigator to open your app’s Info.plist file.
  2. Right click and select “Add Row”.
  3. Scroll to the bottom and select “URL types” or if viewing the Raw keys select “CFBundleURLTypes”.  URLTypes
  4. Open the arrow menu for URL types till you get to the URL Identifier.  Enter a unique string such as your apps reverse domain.  e.g. com.companyName.appNameURLIdentifier
  5. Select Item 0, and press the + sign to add a row.  Select “URL Schemes”.  URLSchemes
  6. Enter a unique url scheme that no other app would have.  URLSchemesFilledOut
  7. Run your app.

Step 2 – Test with Safari

  1. Install the app on your device or simulator.
  2. Open Safari and type in the custom url scheme in the address bar. In the example above it would look like kitefasterCustomUrlScheme://safari
  3. An alert will ask you if you want to “Open this page in ‘Your app Name’?”.  Select Open, and the app should be presented.

Step 3 – Open one iOS app from another iOS app

Set up Whitelist

  1. Use Xcode’s Project Navigator to open your app’s (the app that will be opening the app in step 1 above) Info.plist file.
  2. Right click and select “Add Row”.
  3.  Add the key: LSApplicationQueriesSchemes of type Array.LSApplicationQueriesSchemes
  4. Press the + button to add a new Item.
  5. Enter the custom URL Scheme you would like to open.
  6. Repeat the above two steps for each additional app you would like to open.

Add Code

Below is some sample Swift 3 code used to open the custom URL scheme.

import Foundation
import UIKit
...
    static let kCustomURLScheme = "kitefasterCustomUrlScheme://"

    class func openCustomApp() {
        if openCustomURLScheme(kCustomURLScheme) {
            // app was opened successfully
        } else {
            // handle unable to open the app, perhaps redirect to the App Store
        }
    }


    class func openCustomURLScheme(customURLScheme: String) -> Bool {
        let customURL = URL(string: customURLScheme)!
        if UIApplication.shared.canOpenURL(customURL) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(customURL)
            } else {
                UIApplication.shared.openURL(customURL)
            }
            return true
        }

        return false
    }

View the Apple Documentation on Inter-App Communication for Using URL Schemes to Communicate with Apps.

 

Get insights into your Apple Search Ads performance and optimize your bids and keywords for maximum revenue with Kitemetrics by Kitefaster.

 

Tested with:

Xcode 7.3, Xcode 8.3.2

iOS 9.3, iOS 10.3



Our Products

Kitemetrics
Keyword level attribution for Apple Search Ads. Optimize your bids and increase your revenue. Sign up for free at kitemetrics.com.

White Noise and Deep Sleep Sounds
Calming and relaxing sounds that will help you sleep like a baby tonight.
Download for free from the App Store.
Get it on Google Play.




Our Sponsors

Get Sleepy
Get Sleepy
The free podcast that puts you to sleep with meditative bedtime stories. Listen at getsleepy.com.