suale-dev / JavaScriptInterface

Calling native code from Javascript in the iOS application likes addJavascriptInterface in the Android.
36 stars 9 forks source link

Unable to call functions from Javascript in the native app #9

Closed raghibrm closed 6 years ago

raghibrm commented 6 years ago

I am using the following code:

@objc protocol JSExportDelegate : JSExport
{
    func social_finish(_ doConfirmInfo : Bool, infoJson : String, loginToken : String, state : String)
    func social_error(_ errorJson : String, state : String)
    func social_link(_ infoJson : String, state: String)
}

class SocialEntryViewController: UIViewController, JSExportDelegate {
    func social_finish(_ doConfirmInfo: Bool, infoJson: String , loginToken: String , state: String ) {
        print("here1")
    }

    func social_error(_ errorJson: String, state: String) {
        print("here2")
    }

    func social_link(_ infoJson: String, state: String) {
        print("here3")
    }

override func viewDidLoad() {
let url = URL(string: "http://localhost:8080/auth/social-accounts/" + provider + "-connect?device=ios&state=qwerfvb")
self.webView.addJavascriptInterface(self, forKey: "Native")
webView.loadRequest(URLRequest(url: url!))
}

I am getting the following script from my localhost server:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login / Sign up</title>
    <script type="text/javascript">
        var errorJson = null;
        var doConfirmInfo = true;
        var infoJson = "{\"mobileNumber\":\"\",\"countryCode\":\"\",\"fullName\":\"Christopher Albhdccdicefc Bowersman\",\"picture\":\"https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=102590770698706&height=50&width=50&ext=1540977690&hash=AeRSWR8symTdRN1-\"}";
        var loginToken = null;
        var state = "qwerfvb";
        var linkMode = false;

        (function() {
            if(errorJson) {
                Native.social_error(errorJson, state);
            } else {
                if(linkMode) {
                    Native.social_link(infoJson, state);
                } else {
                    Native.social_finish(doConfirmInfo, infoJson, loginToken, state);
                }
            }
        })();
    </script>
</head>
</html>

So according to the script above, social_finish should have run but it doesn't. What's missing here and how should I correct this?

suale-dev commented 6 years ago

@raghibrm : func social_link(infoJson : String, state: String) --> should be: func sociallink( infoJson : String, _ state: String)

raghibrm commented 6 years ago

@sua8051, tried, it doesn't work still. Please check the edited description. Also do you think there is something wrong with string values having null values (check the script)?

suale-dev commented 6 years ago

@raghibrm omit all label parameter in your functions.
func sociallink( infoJson : String, state: String) --> should be: func sociallink(** infoJson : String, _ state**: String)

raghibrm commented 6 years ago

@sua8051 , Thank you, it worked!