我正在使用Google授权开发适用于Mac的Safari扩展程序(“GTMAppAuth”pod)。但我无法在Safari扩展目标中处理授权:
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
我已将网址添加到.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>MyURL</string>
</array>
</dict>
</array>
我在singleton中设置了事件处理程序,因为它在Extension
中没有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 :(得分:0)
几天后,我找到了一个解决方案。 我正在使用应用程序组将授权URL传递给扩展程序。 用户按下signIn按钮后,我安排了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中设置重定向处理程序。您必须在应用程序启动之前设置它:
- (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重定向网址传递到Safari扩展程序,以便进行下一步授权。如果有人知道另一种方法,我会很高兴检查它。