ad_on_is
ad_on_is

Reputation: 1550

Deep link into macOS app is not recognized

I'm trying to implement a deep link into an macOS application, but nothing seems to work.

So far, my AppDelegate.swift contains the following

func application(app: NSApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    print("opened by link");
    return true
}

I also configured the info.plist with URLSchemes beeing my bundle identifier and URLIdentifier beeing simply zst

In a simple html-file I use the following code to test the deep link

<a href="zst://test/link">Test</a>

My app gets opened (or becomes active when already running), but the print statement is not executed.

What am I doing wrong here?

Upvotes: 4

Views: 4446

Answers (1)

ad_on_is
ad_on_is

Reputation: 1550

Thanks to @Ssswift I found a solution. Used this code: How do you set your Cocoa application as the default web browser?

and converted it to swift with: https://objectivec2swift.com

works with Swift 3

in AppDelegate.swift added these lines

func applicationDidFinishLaunching(_ aNotification: Notification) {
    var em = NSAppleEventManager.shared()
    em.setEventHandler(self, andSelector: #selector(self.getUrl(_:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}

func getUrl(_ event: NSAppleEventDescriptor, withReplyEvent replyEvent: NSAppleEventDescriptor) {
    // Get the URL
    var urlStr: String = event.paramDescriptor(forKeyword: keyDirectObject)!.stringValue!
    print(urlStr);

}

Upvotes: 7

Related Questions