简体   繁体   中英

Mac Widgets: NSApplication openURLs not called from widget

I am working on a widget for my existing Mac app. The problem I am running into is that when I add a Link from the widget to the main app, the method - (void) application: (NSApplication *)application openURLs:(NSArray<NSURL *> *)urls isn't called at all, so I'm not able to direct the app on how to best handle the widget tap.

I'm just adding a simple Link like this:

Link(destination: URL(string: "widgetAddToDoButtonLink")!) {
    Image(systemName: "plus.circle").imageScale(.medium)
}

It does foreground the main app when the Link is tapped, but doesn't call the openURLs delegate method. Now I've tried the same thing with a new app project and widget, and it works fine. But not with my existing Mac app project, which has a deployment target of 10.15 (changing the deployment target to 12.0 didn't work).

Is there something about an old project that might be causing the openURLS method to not be called? It works otherwise, when the "URL Type" is defined in the Info.plist (though I don't believe you need to do that to handle Widgets). In either case, adding the "widgetAddToDoButtonLink" to a new URL Schemes in the Info.plist file doesn't help either.

So I found the culprit, and thought I'd mention it here in case it helps someone else, My Mac app was also using the Dropbox v2 SDK, which requires the app to register for Apple 'events': using this:

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

With this handler in place, the actual NSApplication "application openURLs" method doesn't get called. Removing this 'event handler' gets it working again (or can handler the event in this 'handleAppleEvent' method, like this:

- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
    NSURL *url = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
}

Just putting it out there in case someone is stuck on this in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM