Mac OSX Mavericks处理CFBundleURLName“自定义网址”的方式是否有所改变?启动应用程序? [英] Has there been a change to the way Mac OSX Mavericks handles CFBundleURLName "Custom url" launches for applications?

查看:214
本文介绍了Mac OSX Mavericks处理CFBundleURLName“自定义网址”的方式是否有所改变?启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序,该应用程序是从任何OSX浏览器中的自定义网址启动的。通过在应用程序的plist中添加标准CFBundleURLName条目,可以很好地实现此目的。

I created an app which is launched from a custom url in any OSX browser. This worked just fine by adding a standard CFBundleURLName entry to the app's plist.

我的应用程序通过解析自定义url上的某些参数,然后对其进行响应来进行读取。

My application works by reading by parsing some of the parameters on the custom url and then reacting to them.

因此,例如,使用以下自定义网址:

So for example with a custom url of:

foobar:// param1 / param2 / param3

在浏览器中单击上述URL时,OSX将启动我的应用程序,并将实际的自定义URL本身作为第一个参数传递给该应用程序。因此,在应用程序中,我可以读取第一个arg并获取打开应用程序的网址,然后将其解析为我需要的参数。

When clicking on the above url in a browser, OSX would launch my app and pass the actual custom url itself as the first argument to the app. Therefore in the app I could read the first arg and get the url the opened the app, and parse it for params I need.

在OSX 10.5-10.8中可以正常工作,但在10.9小牛中,它的工作方式似乎略有不同。也就是说,如果该应用程序尚未运行,它仍会启动该应用程序,但不会将自定义网址作为第一个参数传递-因此该应用程序认为它只是由用户手动启动(例如从启动板中选择),而不是直接从浏览器。

This works fine in OSX 10.5-10.8, but in 10.9 Mavericks it appears to work slightly differently. Namely that if the application is not already running, it still launches the app but does not pass the custom url as first argument - so the app thinks it's just been launched manually by the user (such as selecting it from launchpad) rather than directly from a browser.

奇怪的是,如果应用程序已打开,则单击自定义URL确实会将URL字符串作为第一个参数发送到应用程序,并且应用程序中的功能会按预期发生。

Weirdly, if the application is already open, then clicking the custom url DOES send the url string over to the app as first argument and functionality within the app occurs as expected.

我已经在10.6-> 10.9的新版和旧版应用程序中对此进行了测试,并且都表现出相同的行为。第一次启动时,所有版本在10.9 Mavericks之前的版本都可以正常工作,但是在10.9中,它们没有获得作为第一个arg传递的url,但是一旦运行就可以进行第二次单击。

I've tested this across 10.6->10.9 with new and old versions of my app and all exhibit the same behaviour. All work fine on first launch with versions before 10.9 Mavericks, but in 10.9 they don't get the url passed as first arg but then work on 2nd click once already running.

如果有人可以阐明这一点,我将不胜感激。

If anyone could shed some light on this I would be very grateful.

推荐答案

您在哪里设置URL处理程序?它需要尽早发生。如果您当前在 applicationDidFinishLaunching 中拥有它,请尝试将其移至 applicationWillFinishLaunching

Where do you set up your URL handler? It needs to happen early. If you currently have it in applicationDidFinishLaunching, try to move it to applicationWillFinishLaunching.

例如,以下内容对我有用,即使在应用未运行之前,即使在我在Safari中打开该URL之前,该URL也会在启动时记录该URL。当我将WillFinishLaunching更改为DidFinishLaunching时,我确切地看到了您所描述的行为。

The following works for me and logs the URL at launch even when the app is not running before I open the URL in Safari, for example. When I change WillFinishLaunching to DidFinishLaunching, I see exactly the behavior you describe.

@implementation AppDelegate

- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
    NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
    [appleEventManager setEventHandler:self andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
}

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSAppleEventDescriptor *obj = [event descriptorForKeyword:keyDirectObject];
    DescType type = [obj descriptorType];
    if (type == typeChar) {
        NSData *data = [obj data];
        if (data) {
            NSString *urlString = [[NSString  alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:urlString];
            NSLog(@"url: %@", url);
        }
    }
}

@end

这篇关于Mac OSX Mavericks处理CFBundleURLName“自定义网址”的方式是否有所改变?启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆