2017-01-18 25 views
0

Mac認定のMac用Safari拡張機能(「GTMAppAuth」ポッド)を開発中です。しかし、私はSafariの拡張ターゲットに承認を処理することはできません。handleGetURLEvent:Safari拡張機能で呼び出されないMac OS

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

私は

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

をの.plistするURLを追加しました、それは拡張

には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]; 
} 

とにかく呼び出されていません。

答えて

0

数日後、解決策が見つかりました。 私はアプリケーショングループを使用して承認URLを内線番号に渡しています。 ユーザーがサインインボタンを押した後、私はNSTimerをスケジュールしています。メインアプリケーションにサインインしたユーザーがリダイレクトURLをロードして処理しているとき。その方法では、Groupの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にリダイレクトハンドラを設定します。アプリケーショングループを通じて、私はサファリにGoogleのリダイレクト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]; 
} 

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

メインアプリケーションのAppDelegateでの取り扱いリダイレクトURLを、グループにURLを保存する:あなたは、アプリケーションが起動する前にそれを設定する必要があります次の承認ステップのための拡張。誰かがそれを行う別の方法を知っているなら、私はそれを確認することがうれしいです。

関連する問題