0

I'm working on some SSO behavour in a nativescript application. I have the following Swift code that works correctly:

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()
    }
}

From this I've come up with the equivalent typescript code (in an .ios.ts file)

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');
}

This all compiles and builds fine, but when run it crashes at the session.start() call after a second or so delay. I get the output before that, including the 'about to call it' method, but nothing after, not even an error message or stack dump.

Is there anything obvious I'm doing wrong here? Is there anything special that needs to be done to call from typescript to native ios shared library methods?

4

2 回答 2

0

我认为您必须将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');
}
于 2019-02-21T06:25:44.330 回答
0

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

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

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

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

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

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

于 2019-02-21T22:21:59.233 回答