【问题标题】:how do I use SFAuthenticationSession from nativescript?如何从 nativescript 使用 SFAuthenticationSession?
【发布时间】:2019-07-14 21:56:18
【问题描述】:

我正在处理原生脚本应用程序中的一些 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()
    }
}

从这里我想出了等效的打字稿代码(在 .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 共享库方法需要做些什么特别的事情吗?

【问题讨论】:

    标签: nativescript sfauthenticationsession safariservices


    【解决方案1】:

    我认为您必须将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');
    }
    

    【讨论】:

    • 你是对的,我们现在使用全局变量来存储会话,以便保留它。事实证明,这并不是根本原因。
    【解决方案2】:

    我今天发现了问题,这是一个令人尴尬的小错误。

    当我进行翻译时,我设法从原始代码中删除了 http://。

    URL: NSURL.URLWithString('localhost:3500/redirect.html'),

    当在同事的机器上运行时,我们在日志中获得了更多详细信息,事实证明唯一支持的方案是 http 或 https。它必须显式添加到 url。

    所以这修复了它(除了上面 Manoj 的更改)

    URL: NSURL.URLWithString('http://localhost:3500/redirect.html'),

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 2020-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 2018-03-06
      • 1970-01-01
      相关资源
      最近更新 更多