获取:在使用IOS 10中的IFRAME将数据从javascript传递到objective-c时,在shouldStartLoadWithRequest中为空

时间:2017-05-09 10:50:25

标签: objective-c iframe uiwebview ios10

我将数据从javascript传递给objective-c。因为我正在使用IFRAME 这是我的代码:
context.html

function openCustomURLinIFrame(src)
{
    alert(src);
    var rootElm = document.documentElement;
    var newFrameElm = document.createElement("IFRAME");
    newFrameElm.setAttribute("src",src);
    document.documentElement.appendChild(newFrameElm);
    //remove the frame now
    newFrameElm.parentNode.removeChild(newFrameElm);
    newFrameElm = null;
}  

Indoor.m

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"Loading: %@", [request URL]);
    NSURL *url = [request URL];
    NSString *urlStr = url.absoluteString;

    return [self processURL:urlStr];

}  

我正在

  

正在加载:about:blank

我使用的是xCode 8.2.1,它在IOS 9.3中运行良好,但在iOS 10.2中无效。

修改
我在.html文件中的警报截图。
My alert screenshot in .html file

修改
html文件中的方法,我在其中调用openCustomURLinIFrame方法。

function calliOSFunction(functionName, args, successCallback, errorCallback)
{
    var url = "js2ios://";
    var callInfo = {};
    callInfo.functionname = functionName;
    //alert("Custom menu clicked !!"+functionName);
    if (successCallback)
    {
        //alert("Success !!"+functionName);
        callInfo.success = successCallback;
    }
    if (errorCallback)
    {
        //alert("Error !!"+functionName);
        callInfo.error = errorCallback;
    }
    if (args)
    {
        //alert("args !!"+args);
        callInfo.args = args;
    } 
    url += JSON.stringify(callInfo)   
    openCustomURLinIFrame(url);
}

帮我解决此问题。

1 个答案:

答案 0 :(得分:2)

终于经过很长一段时间我得到了答案。

function calliOSFunction(functionName, args, successCallback, errorCallback)
{
    var url = "js2ios:///";   /* Added one more "/" */
    var callInfo = {};
    callInfo.functionname = functionName;
    //alert("Custom menu clicked !!"+functionName);
    if (successCallback)
    {
        //alert("Success !!"+functionName);
        callInfo.success = successCallback;
    }
    if (errorCallback)
    {
        //alert("Error !!"+functionName);
        callInfo.error = errorCallback;
    }
    if (args)
    {
        //alert("args !!"+args);
        callInfo.args = args;
    }

    url += JSON.stringify(callInfo)

    openCustomURLinIFrame(url);
}   

我再添加一个" /"在 url 变量中。