首先,在xcode 11中,引入了SceneDelegate概念
原因
在支付宝(极简版)授权登录的官方demo里,在ViewController里面调用方法
#pragma mark ==============点击模拟授权行为==============
- (void)demoAuth
{
NSString *url = @""; //登陆授权或别的需要跳转到支付宝完成操作的Url
NSDictionary *params = @{kAFServiceOptionBizParams: @{
@"url": url//@""
},
kAFServiceOptionCallbackScheme: @"apsdkdemo",
};
[AFServiceCenter callService:AFServiceAuth withParams:params andCompletion:^(AFServiceResponse *response) {
NSLog(@"授权结果:%@", response.result);
}];
}
在Appdelegate中的代理方法
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([url.host isEqualToString:@"apmqpdispatch"]) {
[AFServiceCenter handleResponseURL:url withCompletion:^(AFServiceResponse *response) {
if (AFResSuccess == response.responseCode) {
NSLog(@"%@", response.result);
}
}];
}
return YES;
}
// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
if ([url.host isEqualToString:@"apmqpdispatch"]) {
[AFServiceCenter handleResponseURL:url withCompletion:^(AFServiceResponse *response) {
if (AFResSuccess == response.responseCode) {
NSLog(@"%@", response.result);
}
}];
}
return YES;
}
由于SceneDelegate的引入,app的生命周期发生了变化,Appdelegate里的openURL代理方法被弃用, 导致支付宝登录授权回调andCompletion无响应
解决方法
在SceneDelegate.m写入如下方法:
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts {
NSEnumerator *enumerator = [URLContexts objectEnumerator];
UIOpenURLContext *context;
while (context = [enumerator nextObject]) {
NSLog(@"context.URL =====%@",context.URL);
NSLog(@"context.options.sourceApplication ===== %@",context.options.sourceApplication);
NSString *url = [NSString stringWithFormat:@"%@",context.URL];
if ([url containsString:@"apmqpdispatch"]) {
[AFServiceCenter handleResponseURL:context.URL withCompletion:^(AFServiceResponse *response) {
if (AFResSuccess == response.responseCode) {
NSLog(@"%@", response.result);
}
}];
}
}
}
这时ViewController的回调正常执行.代码已由阿里技术人员确认,并且他们说后期会更新相应文档.
网友评论