【问题标题】:How to determine whether the macOs app was launched via a url如何确定 macOS 应用程序是否通过 url 启动
【发布时间】:2019-12-28 21:28:33
【问题描述】:

我需要知道该应用程序是否通过DidFinishLaunching 中的URL 启动。

DidFinishLaunching 中的 notification 参数包含一个键 LaunchIsDefaultLaunchKey,根据 Apple 文档:

“如果启动应用程序以打开或打印文件、执行服务操作、应用程序已保存将恢复的状态或应用程序启动在某种其他意义上不是默认值,则值为 NO发射”

这涵盖了应用程序通过 URL 打开的情况,但也涵盖了我不需要的情况。

在 iOS 上有 DidFinishLaunchinglaunchOptions 字典,其中包含 LaunchOptionsUrlKey 键中的 URL。

在 macOS 上有类似的东西吗?

【问题讨论】:

    标签: c# swift macos xamarin xamarin.mac


    【解决方案1】:

    在 Objective-C 中,这就是你所做的。

    注册以使用类kInternetEventClass 和ID kAEGetURL 处理共享事件:

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

    回调如下所示:

    - (void)handleURLEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
    {
        // Read the URL used to launch the application
        NSString* launchUrl = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    }
    

    你可以在applicationWillFinishLaunching:注册回调,它会在applicationDidFinishLaunching:之前被调用。

    Xamarin / C# 应该看起来像这样(未经测试):

    public override void FinishedLaunching(NSObject notification)
    {
        NSAppleEventManager appleEventManager = NSAppleEventManager.SharedAppleEventManager;
    
        appleEventManager.SetEventHandler(this, new Selector("handleGetURLEvent:withReplyEvent:"), AEEventClass.Internet, AEEventID.GetUrl);
    }
    
    [Export("handleGetURLEvent:withReplyEvent:")]
    private void HandleGetURLEvent(NSAppleEventDescriptor descriptor, NSAppleEventDescriptor replyEvent)
    {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2018-12-01
      • 1970-01-01
      • 2016-06-30
      • 1970-01-01
      相关资源
      最近更新 更多