Launch custom app with URL by domain
Asked Answered
L

3

9

Even though the YouTube app is now not a built-in app by Apple, it looks like when tapping a youtube link (in mail, for example), which starts with http://www.youtube.com, opens the YouTube app right away.

Is there a way to this for custom apps in iOS 6? I only know of custom schemes as the way to launch an app via URL.

Limburger answered 20/9, 2012 at 19:33 Comment(3)
Here is the link I previously tried posting in my answer that was deleted. Sorry, @ChrisF...still learning the ropes.Geerts
I posted an answer with as much depth as I think you may need. I followed the exact same steps when I was registering my app for a custom URL. Can you see if my answer below answers your question ?Hartzel
@Hartzel Your indepth answer has nothing to do with the answer, as it does not explain how the YouTube app does what it does.Histopathology
W
2

I'm not sure I understand your question, but here is my attempt at an answer. You ask if there is a way to open third party apps from within another app. The answer is you can if the app has implemented a custom URL scheme (see here and navigate to the Communicating With Other Apps section).

But you also seem to say that you already know about this. In which case, I'm pretty sure there is no other way.

Watertight answered 25/9, 2012 at 2:9 Comment(0)
H
2

To register a URL type for your app

Include the CFBundleURLTypes key in your app’s Info.plist file. The CFBundleURLTypes key contains an array of dictionaries, each of which defines a URL scheme the app supports.

Keys and values of the CFBundleURLTypes property

Call the URL (registered as above)

NSURL *myURL = [NSURL URLWithString:@"todolist://www.acme.com?Quarterly%20Report#200806231300"];
[[UIApplication sharedApplication] openURL:myURL];

Handle Calls to Custom URL Schemes

An app that has its own custom URL scheme must be able to handle URLs passed to it. All URLs are passed to your app delegate, either at launch time or while your app is running or in the background. To handle incoming URLs, your delegate should implement the following methods:

Use the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods to retrieve information about the URL and decide whether you want to open it. If either method returns NO, your app’s URL handling code is not called. In iOS 4.2 and later, use the application:openURL:sourceApplication:annotation: method to open the file. In iOS 4.1 and earlier, use the application:handleOpenURL: method to open the file. If your app is not running when a URL request arrives, it is launched and moved to the foreground so that it can open the URL. The implementation of your application:willFinishLaunchingWithOptions: or application:didFinishLaunchingWithOptions: method should retrieve the URL from its options dictionary and determine whether the app can open it. If it can, return YES and let your application:openURL:sourceApplication:annotation: (or application:handleOpenURL:) method handle the actual opening of the URL. (If you implement both methods, both must return YES before the URL can be opened.)

If your app is running but is in the background or suspended when a URL request arrives, it is moved to the foreground to open the URL. Shortly thereafter, the system calls the delegate’s application:openURL:sourceApplication:annotation: to check the URL and open it. If your delegate does not implement this method (or the current system version is iOS 4.1 or earlier), the system calls your delegate’s application:handleOpenURL: method instead.

Note

If two or more applications have registered the same Custom URL, there is no guarentee of which application the iOS will open if the Custom URL is called.

Further Reading

iOS App Programming Guide :: Advanced Tips and Trics

Hartzel answered 8/4, 2013 at 16:56 Comment(4)
Thanks, I've mentioned in the question I'm aware of custom URL schemes, and am looking for a way to do without them, the same as youtube does.Limburger
Youtube uses custom url schemes. What makes you think it doesnt ? The www.youtube.com/watch? is the custom URL scheme allocated to YouTubeHartzel
What makes you think www.youtube.com/watch? is an URL scheme?Boyett
the fact that it launches the app ?! thats the only way you can do it.Hartzel
G
0

I imagine the native Mail app uses the same method all apps use to open URLs (which will open an app or use Mobile Safari to complete the request if no installed app can process the URL).

You should be able to accomplish the same result using the following:

NSString *youtubeURL = @"http://www.youtube.com/watch?v=c2JTu22qxms";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:youtubeURL]];

This will open the YouTube app if it is installed or use safari to play the video if the YouTube app is not installed.

---- UPDATE -----

I decided to look into how exactly the YouTube app does it.

I found something interesting!

Here are the URLs the YouTube app declares:

  • fb[appID]

  • vnd.youtube

  • youtube

This implies that http://www.youtube.com redirects the request to one of the URLs on the list. But when I try it, I don't see Mobile Safari opening and then redirecting.

I will keep investigating, it is very intriguing.

Gaidano answered 8/4, 2013 at 13:58 Comment(2)
The question is how to imitate this behavior for custom apps other than youtube.Limburger
@Limburger you are absolutely right! I got confused by the wording.Gaidano

© 2022 - 2024 — McMap. All rights reserved.