ASWebAuthenticationSession in MacOS 10.15 (Catalina)

爷,独闯天下 提交于 2021-02-07 07:57:32

问题


I am trying to implement the new ASWebAuthenticationSession in MacOS 10.15 and I'm getting a bit confused about the callbackURLScheme.

The header file for ASWebAuthenticationSession says:

The callback URL usually has a custom URL scheme. For the app to receive the callback URL, it needs to either register the custom URL scheme in its Info.plist, or set the scheme to callbackURLScheme argument in the initializer.

So I initialize like this:

self.webAuthSession = [[ASWebAuthenticationSession alloc] initWithURL:self.authURL
                                                    callbackURLScheme:@"myurlscheme://"
                                                    completionHandler:^(NSURL * _Nullable callbackURL, NSError * _Nullable error) {

}];

The oauth provider I am trying to access, does not support custom URL schemes, so I have it set to redirect to a web address which in turn redirects to my URL Scheme.

So, after clicking Login in my MacOS app, Safari presents a window that allows me to login to the service. After successfully logging in the redirect happens but the web page just goes to a screen saying

Safari can't open the specified address.

I can see that the address is myurlscheme://?code=1234567890abcdefgetc... which is correct but it's obviously not calling the ASWebAuthenticationSession callback.

If I add the URL Scheme to the plist, then the safari window shows a prompt asking if you want to open it in the my app.

I know I can then fetch the code from the App Delegate's application:openURLs: method, but this is fiddly, has an unnecessary prompt for the user to click, and also leaves the ASWebAuth... safari window open. This should all be automatic through the callback.

The only way I get the callback to fire, is by closing the safari window in which case it calls back with error.


回答1:


I have just returned to this to try again and it seems to be working fine on MacOS 10.15.1 and 10.15.2 beta.

I have noticed that the callbackURLScheme parameter must not include ://

I'm not able to go back and test it again on 10.15 but i'm happy to use this now assuming everyone on 10.15 would have updated to 10.15.1




回答2:


Darren is right in MacOS you must remove the :// from the callbackURLScheme. iOS works fine no matter if you :// or not. However, in MacOS Catalyst there seems to be a bug. My app is getting focused once I login, but isn't getting the callback. Haven't found a way to fix it yet.



来源:https://stackoverflow.com/questions/58223030/aswebauthenticationsession-in-macos-10-15-catalina

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!