Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle with a default URL scheme

I want to build URI (or URL scheme) support in my app.

I do a LSSetDefaultHandlerForURLScheme() in my + (void)initialize and I setted the specific URL schemes also in my info.plist. So I have URL schemes without Apple Script or Apple Events.

When I call myScheme: in my favorite browser the system activates my app.

The problem is, how to handle the schemes when they are called. Or better said: How can I define what my app should do, when myScheme: is called.

Is there a special method that I have to implement or do I have to register one somewhere?

like image 653
papr Avatar asked Jan 02 '10 09:01

papr


People also ask

What does URL scheme mean?

Custom URL schemes provide a way to reference resources inside your app. Users tapping a custom URL in an email, for example, launch your app in a specified context. Other apps can also trigger your app to launch with specific context data; for example, a photo library app might display a specified image.

How do I find my app scheme URL?

Go to the Play store, find the app and view its page. Copy the URL from the browser address bar. Make a note of this as the "Android Fallback URL." You'll see a parameter called "id" followed by an equal sign (=).


2 Answers

As you are mentioning AppleScript, I suppose you are working on Mac OS X.

A simple way to register and use a custom URL scheme is to define the scheme in your .plist:

<key>CFBundleURLTypes</key> <array>     <dict>         <key>CFBundleURLName</key>         <string>URLHandlerTestApp</string>         <key>CFBundleURLSchemes</key>         <array>             <string>urlHandlerTestApp</string>         </array>     </dict> </array> 

To register the scheme, put this in your AppDelegate's initialization:

[[NSAppleEventManager sharedAppleEventManager]     setEventHandler:self         andSelector:@selector(handleURLEvent:withReplyEvent:)       forEventClass:kInternetEventClass          andEventID:kAEGetURL]; 

Whenever your application gets activated via URL scheme, the defined selector gets called.

A stub for the event-handling method, that shows how to get the URL string:

- (void)handleURLEvent:(NSAppleEventDescriptor*)event         withReplyEvent:(NSAppleEventDescriptor*)replyEvent {     NSString* url = [[event paramDescriptorForKeyword:keyDirectObject]                         stringValue];     NSLog(@"%@", url); } 

Apple's documentation: Installing a Get URL Handler

Update I just noticed a problem for sandboxed apps that install the event handler in applicationDidFinishLaunching:. With enabled sandboxing, the handler method doesn't get called when the app is launched by clicking a URL that uses the custom scheme. By installing the handler a bit earlier, in applicationWillFinishLaunching:, the method gets called as expected:

- (void)applicationWillFinishLaunching:(NSNotification *)aNotification {     [[NSAppleEventManager sharedAppleEventManager]         setEventHandler:self             andSelector:@selector(handleURLEvent:withReplyEvent:)           forEventClass:kInternetEventClass              andEventID:kAEGetURL]; }  - (void)handleURLEvent:(NSAppleEventDescriptor*)event         withReplyEvent:(NSAppleEventDescriptor*)replyEvent {     NSString* url = [[event paramDescriptorForKeyword:keyDirectObject]                         stringValue];     NSLog(@"%@", url); } 

On the iPhone, the easiest way to handle URL-scheme activation is, to implement UIApplicationDelegate's application:handleOpenURL: - Documentation

like image 191
Thomas Zoechling Avatar answered Sep 21 '22 06:09

Thomas Zoechling


All credits should go to weichsel and kch

I'm just adding swift(2.2/3.0) code for your convenience

func applicationWillFinishLaunching(_ notification: Notification) {     NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleGetURL(event:reply:)), forEventClass: UInt32(kInternetEventClass), andEventID: UInt32(kAEGetURL) ) }  @objc func handleGetURL(event: NSAppleEventDescriptor, reply:NSAppleEventDescriptor) {     if let urlString = event.paramDescriptor(forKeyword: keyDirectObject)?.stringValue {         print("got urlString \(urlString)")     } } 
like image 23
Laimonas Avatar answered Sep 19 '22 06:09

Laimonas