handleGetURLEvent:未在 Safari 扩展中调用 Mac OS

handleGetURLEvent: doesn't get called in Safari Extension Mac OS

我正在使用 Google 授权("GTMAppAuth" pod)为 Mac 开发 Safari 扩展。但我无法在 Safari 扩展目标中处理授权:

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
           withReplyEvent:(NSAppleEventDescriptor *)replyEvent

我已将 URL 添加到 .plist

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>MyURL</string>
        </array>
    </dict>
</array>

我在单例中设置了事件处理程序,因为它在扩展中没有 AppDelegate

+ (Singleton *)sharedSingleton 
{
    static Singleton *singleton = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^
        {
            singleton = [[Singleton alloc] init];
            NSAppleEventManager *appleEventManager =
            [NSAppleEventManager sharedAppleEventManager];
            [appleEventManager setEventHandler:self
                                   andSelector:@selector(handleGetURLEvent:withReplyEvent:)
                                 forEventClass:kInternetEventClass
                                    andEventID:kAEGetURL];
        });

    return singleton;
}

我的处理方法:

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
           withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSString *URLString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSURL *URL = [NSURL URLWithString:URLString];
    [_currentAuthorizationFlow resumeAuthorizationFlowWithURL:URL];
}

无论如何它都没有被调用。

几天后我找到了适合我的解决方案。 我正在使用应用程序组将授权 URL 传递给分机。 用户按下登录按钮后,我正在安排 NSTimer。当用户登录主应用程序时正在加载和处理重定向 URL。在该方法中,它保存到组的 UserDefaults。

- (void)p_signInToGoogleWithSuccessBlock:(void(^)(NSString *email, NSString *token, NSError *error))successBlock
{
    //This method will call in Extension 
    OIDAuthorizationRequest *request = [self createGoogleAuthorizationRequest];

    _currentAuthorizationFlow =
    [OIDAuthState authStateByPresentingAuthorizationRequest:request
                                                   callback:^(OIDAuthState *_Nullable authState,
                                                          NSError *_Nullable error)
     {
         if (authState)
         {
             //User authorized
         }
         else
         {
             //User dismissed authorization
         }
     }];
         //Start timer to check group for authorization URL
    [NSTimer scheduledTimerWithTimeInterval:1
                                repeats:YES
                                  block:^(NSTimer * _Nonnull timer)
                                    {
                                       //Check group for authorization URL
                                        NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kGroupID];
                                        NSString *authorizationString = [defaults objectForKey:kAuthorizationURL];

                                        if (authorizationString != nil)
                                        {
                                            NSURL *authorizationURL = [NSURL URLWithString:authorizationString];
                                            [self.currentAuthorizationFlow resumeAuthorizationFlowWithURL:authorizationURL];
                                            [timer invalidate];
                                        }
                                    }];
}

在主应用程序的 AppDelegate 中设置重定向处理程序。您必须在应用程序启动之前设置它:

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

在主应用程序的 AppDelegate 中处理重定向 URL 并将 URL 保存到组:

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
       withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
    NSString *URLString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kGroupID];
    [defaults setObject:URLString forKey:kAuthorizationURL];
}

因此,通过应用程序组,我将 Google 重定向 URL 到 Safari 扩展以进行后续授权步骤。如果有人知道另一种方法,我会很乐意检查。