iOS11 SDK无法访问应用程序的Delegate

iOS11 SDK无法访问应用程序的Delegate,第1张

概述- (NSString *)getAuthorizationHeader{ iKMAppDelegate *delegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate; NSString *header = [NSString stringWithFormat:@"Bearer %@", deleg
- (Nsstring *)getAuthorizationheader{    iKMAppDelegate *delegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate;    Nsstring *header = [Nsstring stringWithFormat:@"Bearer %@",delegate.appDataObject.oauth2Acesstoken];    return header;}

这个方法会在XCode9中收到警告

[UIApplication delegate] must be called from main thread only

我不认为主队列中的调度将适用于我的功能.那么如何很好地解决这个警告呢?

解决方法 在主线程上无法访问UIApplication的委托,但您可以使用dispatch_sync轻松完成

- (Nsstring *)getAuthorizationheader{    __block iKMAppDelegate *delegate;    if([NSThread isMainThread]) {        delegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate;    } else {        dispatch_sync(dispatch_get_main_queue(),^{            delegate = (iKMAppDelegate *)[UIApplication sharedApplication].delegate;        });    }    Nsstring *header = [Nsstring stringWithFormat:@"Bearer %@",delegate.appDataObject.oauth2Acesstoken];    return header;}

与dispatch_async相反,dispatch_sync函数将一直等到它传递的块完成后再返回.

使用dispatch_sync,有必要检查是否未从主线程执行该函数,这会导致死锁.

总结

以上是内存溢出为你收集整理的iOS11 SDK无法访问应用程序的Delegate全部内容,希望文章能够帮你解决iOS11 SDK无法访问应用程序的Delegate所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1018162.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存