Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell if the app is launched from a URL scheme or a normal launch?

Basically, whenever the app launches or opens I have a check to see if there's a link on the clipboard, and if so I ask the user if he/she wants to add it.

However, I recently added the ability to use x-callback-urls to add URLs into the app. If they use an app like LaunchPad to open a URL in my app, the "do you want to add the URL from your clipboard?" notification still opens, which doesn't make much sense as they've already launched it to add that URL.

Problem is, the openURL: method in my AppDelegate is called to handle the URL scheme after appDidFinishLaunching and appWillEnterForeground, so I can't put a check in those to see if the openURL method was already called. I could use a dispatch_after, but that seems lazy and hacky.

Is there a way to check how the app was launched, i.e.: via a URL scheme or via a manual open?

like image 897
Doug Smith Avatar asked Jan 18 '14 19:01

Doug Smith


People also ask

What is an app URL?

Android App Links are HTTP URLs that bring users directly to specific content in your Android app. Android App Links can drive more traffic to your app, help you discover which app content is used most, and make it easier for users to share and find content in an installed app.

What is URL scheme in iOS?

A URL scheme allows you to launch a native iOS application from another iOS app or a web application. You can set options in the URL that will be passed to the launched application.

What does URL scheme do?

A URL scheme is the part of a link that specifies the type of application your device uses to open a URL. Many apps support URL schemes: FaceTime uses URL schemes to make calls when a URL starting with facetime:// is opened, just like Safari handles URLs starting with http:// .


1 Answers

Don't implement applicationDidFinishLaunching:. Use application:didFinishLaunchingWithOptions: instead. Likewise don't use application:handleOpenURL:, use application:openURL:sourceApplication:annotation: instead.

If you are launched from a URL the options dictionary will contain a UIApplicationLaunchOptionsURLKey key, and the value for that key will be the URL.

However, if your app is already running but is in the background and the user invokes a URL that re-opens your app, you will get a application:openURL:sourceApplication:annotation: message instead. What you really need to do is implement a handleURL method, and then call that method from both application:didFinishLaunchingWithOptions: and application:openURL:sourceApplication:annotation:

EDIT:

Note that in iOS 9, Apple deprecated application:openURL:sourceApplication:annotation: and added the new method application:openURL:options:.

If your app is iOS 9 and later only, you should implement the new application:openURL:options: method. If you need to support iOS 9 and earlier versions, you should probably implement both application:openURL:sourceApplication:annotation: and the new iOS 9 method application:openURL:options:. The OS will call the correct version for the OS version you are running. I would then create a common method that both of those call. That way you get called from both OS versions, but your code to handle opening an URL is only in one place.

like image 57
Duncan C Avatar answered Oct 05 '22 14:10

Duncan C