Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return from methods #52

Closed
almaz1213 opened this issue Jun 1, 2016 · 2 comments
Closed

Return from methods #52

almaz1213 opened this issue Jun 1, 2016 · 2 comments

Comments

@almaz1213
Copy link

almaz1213 commented Jun 1, 2016

Hi,

I included XWebView into my code and it works:
WebView!.loadPlugin(jsObj(), namespace: "injectedObj")

In the injected object I have a method:
@objc func getPlatform() -> String{
print(" PLARFORNN")
return "{"code":1}"
}
it works and prints on the iOS emulator console.

But it returns undefined in javascript code:
window.injectedObj.getPlatform(); // = undefined

could you give me a clue how can I get result from the method in javascipt in WebView?

Thanx

@xwv
Copy link
Contributor

xwv commented Jun 1, 2016

Native method can't pass the result to JavaScript by return value, because the communication between native and javascript is asynchronous. You have to pass the result back to JavaScript by callback.
Plugin:

@objc func getPlatform(callback: XWVScriptObject) {
    print(" PLARFORNN")
    callback.call(arguments: ["{\"code\":1}"], completionHandler: nil) 
}

JavaScript:

window.injectedObj.getPlatform(function(result){ /*your code */});

@almaz1213
Copy link
Author

nice. works. thanx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants