Why can't I call the external JS function Window.hoge?

Asked 2 years ago, Updated 2 years ago, 152 views

I am developing a Gawa native application using WebView at Swift.
The UIViewController cannot send the value to the function declaring the value in the external JS.
Why?

With the code below, window.a() can be called, but window.hoge() cannot be called.
Is there no choice but to create a tunnel to call window.hoge() in index.html?

HogeViewController.swift (excerpt)

let webView:UIWebView=UIWebView()

override func viewDidLoad(){
    super.viewDidLoad()

    webView.frame=view.bounds
    webView.delegate=self
    view.addSubview (webView)

    let path=NSBundle.mainBundle().pathForResource("html/index", ofType:"html")!
    let url = NSURL (string:path) !
    let urlRequest = NSURLRequest (URL: url,
        cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 60.0)
    webView.loadRequest(urlRequest)

    println("url:"+path)

}

// Abbreviated
// Send to WebView after onLoad due to some event
webView.stringByEvaluatingJavaScriptFromString("window.a('+"hoge"+"')
webView.stringByEvaluatingJavaScriptFromString("window.Hoge('+"hoge"+"")

HTML and JS Files

index.html (part)

<script type="text/javascript"src="./js/hoge.js"></script> 
<script type="text/javascript">
function a(arg){
    window.log(arg.length);
}

</script>

hoge.js

window.hoge=function(binay){
    window.log("recodedMovie");

    return true
}

window.log=function(str){
    console.log(new Date()+":"+str);

    // Implement the functionality used to send logs
    var strBase64 = encodeURIC component(str)
    var URL="nativehoge://log/"

    // Send logs natively
    variable iframe= document.createElement("IFRAME");
    iframe.setAttribute("src", URL + strBase64);
    document. documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;

};

That's all.
Thank you for your cooperation.

javascript swift xcode xcode6 webview

2022-09-30 19:29

1 Answers

Here,

webView.stringByEvaluatingJavaScriptFromString("window.Hoge('+"hoge"+"')

window.hoge('+"hoge"+") is wrong?


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.