首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在原生脚本中使用SFAuthenticationSession?

如何在原生脚本中使用SFAuthenticationSession?
EN

Stack Overflow用户
提问于 2019-02-21 12:45:13
回答 2查看 161关注 0票数 0

我正在研究一个原生脚本应用程序中的一些SSO行为。我有以下正确工作的Swift代码:

代码语言:javascript
复制
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文件中)

代码语言:javascript
复制
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共享库方法需要做什么特殊的事情吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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'),

票数 0
EN

Stack Overflow用户

发布于 2019-02-21 14:25:44

我认为您必须将session变量的引用全局存储到文件中。因为它的作用域是函数的局部作用域,所以一旦onTap函数作用域结束,它就可能被销毁。你可以试一试

代码语言:javascript
复制
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');
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54799406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档