我正在开发一个包含auth进程的react本机库,所以我选择了ASWebAuthenticationSession来完成它。对于这个RN库,我的第一步是第一步(在Swift中)进行本地开发。当我启动这个新的库时,它同时使用了objective桥和swift,我假设这两个文件都可以这样做。
但是我无法正确地从ASWebAuthenticationSession文件中运行目标c,并且我更喜欢使用swift (如果我错了,告诉我)。
问题是,当我从ASWebAuthenticationSession运行代码时,弹出窗口会在任何用户输入之前关闭,而不是从objective。这是我的密码,如果你有主意的话,提前谢谢你。
Swift版本
//MyRnModule.m
@interface RCT_EXTERN_MODULE(MyRNModule, NSObject)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
RCT_EXTERN_METHOD(startSecuredView:(NSURL *)uri)//MyRnModule.swift
@objc(MyRNModule)
class MyRNModule: NSObject {
@objc func startSecuredView(_ url: URL?) {
if let url = url {
if #available(iOS 12.0, *) {
let session = ASWebAuthenticationSession(url: url, callbackURLScheme: "", completionHandler: { (callbackURL, error) in
print("completed")
if let error = error {
print("erorr \(error)")
return
}
if let callbackURL = callbackURL {
print("should handle callback \(callbackURL)")
}
})
if #available(iOS 13.0, *) {
session.presentationContextProvider = self
}
session.start()
}
} else {
print("you must specify url")
}
}
}
extension MyRNModule: ASWebAuthenticationPresentationContextProviding {
@available(iOS 13, *)
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor{
if let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first {
return keyWindow
} else {
return ASPresentationAnchor()
}
}
}Objective-C
@interface RCT_EXTERN_MODULE(MyRNModule, NSObject)
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
RCT_EXPORT_METHOD(startSecuredView:(NSURL *)url)
{
if (!url) {
RCTLogError(@"You must specify a url.");
return;
}
if (@available(iOS 12.0, *)) {
ASWebAuthenticationSession* session =
[[ASWebAuthenticationSession alloc] initWithURL:url
callbackURLScheme: @""
completionHandler:^(NSURL * _Nullable callbackURL,
NSError * _Nullable error) {
_authenticationVCC = nil;
if (callbackURL) {
[RCTSharedApplication() openURL:callbackURL];
}
}];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
if (@available(iOS 13.0, *)) {
session.presentationContextProvider = self;
}
#endif
_authenticationVCC = session;
[session start];
}
}
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
#pragma mark - ASWebAuthenticationPresentationContextProviding
- (ASPresentationAnchor)presentationAnchorForWebAuthenticationSession:(ASWebAuthenticationSession *)session API_AVAILABLE(ios(13.0)){
return UIApplication.sharedApplication.keyWindow;
}
#endif代码似乎反映了相同的过程,刚刚翻译,我不知道我错过了什么,因为调用的MyRNModule. startSecuredView("https://some.url")不一样
发布于 2022-08-02 14:05:25
在您的目标-C代码中,您对会话进行了“强”引用。
_authenticationVCC = session;在你的斯威夫特密码里,你没有。
文档指出,对于iOS < 13.0,一个强引用是强制性的,否则它将立即被删除,因为在您的方法结束时没有更多的对会话的主动引用。这会导致窗口关闭。
要解决这个问题,您可以在类MyRNModule中添加一个属性,并在启动之前将会话分配给这个属性,而不是一个本地常量。
class MyRNModule: NSObject {
private var session: ASWebAuthenticationSession?
}稍后在你的方法中:
self.session = ASWebAuthenticationSession(...)医生们引文
对于部署目标早于iOS 13的iOS应用程序,您的应用程序必须保持对会话的强烈引用,以防止系统在等待身份验证完成时释放会话。
https://stackoverflow.com/questions/72255746
复制相似问题