我正在研究一个原生脚本应用程序中的一些SSO行为。我有以下正确工作的Swift代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
var webAuthSession: SFAuthenticationSession?
@IBAction func click(sender: AnyObject)
{
let authURL = URL(string: "http://localhost:5000/redirect.html");
let callbackUrlScheme = "myApp://"
self.webAuthSession = SFAuthenticationSession.init(url: authURL!, callbackURLScheme: callbackUrlScheme, completionHandler: { (callBack:URL?, error:Error?) in
// handle auth response
guard error == nil, let successURL = callBack else {
return
}
print(successURL.absoluteString)
})
self.webAuthSession?.start()
}
}由此,我得到了等效的typescript代码(在.ios.ts文件中)
function callback(p1: NSURL, p2: NSError) {
console.log('Got into the url callback');
console.log(p1);
console.log(p2);
};
public onTap() {
const session = new SFAuthenticationSession(
{
URL: NSURL.URLWithString('localhost:3500/redirect.html'),
callbackURLScheme: 'myApp://',
completionHandler: callback,
},);
console.log('session created');
console.log('the session is ', session);
console.log('the start method is ', session.start);
console.log('about to call it');
session.start();
console.log('After calling start');
}这一切都可以很好地编译和构建,但是在运行时,它会在大约一秒钟的延迟后在session.start()调用时崩溃。在此之前,我得到了输出,包括“将要调用它”方法,但在此之后没有输出,甚至没有错误消息或堆栈转储。
有没有什么明显的地方我做错了?从typescript调用原生ios共享库方法需要做什么特殊的事情吗?
发布于 2019-02-22 06:21:59
我今天发现了问题,这是一个令人尴尬的小错误。
当我进行翻译时,我设法从原始代码中删除了http://。
URL: NSURL.URLWithString('localhost:3500/redirect.html'),
当在同事的机器上运行时,我们在日志中获得了更多详细信息,结果发现仅支持http或https方案。它必须显式地添加到url中。
所以这就修复了它(除了上面Manoj的更改)
URL: NSURL.URLWithString('http://localhost:3500/redirect.html'),
发布于 2019-02-21 14:25:44
我认为您必须将session变量的引用全局存储到文件中。因为它的作用域是函数的局部作用域,所以一旦onTap函数作用域结束,它就可能被销毁。你可以试一试
function callback(p1: NSURL, p2: NSError) {
console.log('Got into the url callback');
console.log(p1);
console.log(p2);
};
let session;
public onTap() {
session = new SFAuthenticationSession(
{
URL: NSURL.URLWithString('localhost:3500/redirect.html'),
callbackURLScheme: 'myApp://',
completionHandler: callback,
},);
console.log('session created');
console.log('the session is ', session);
console.log('the start method is ', session.start);
console.log('about to call it');
session.start();
console.log('After calling start');
}https://stackoverflow.com/questions/54799406
复制相似问题