将数据从 javascript 传递到 objective c 时未调用 shouldStartLoadWithRequest

shouldStartLoadWithRequest not being called while passing data from javascript to objective c

我查看了与此问题相关的大部分 Whosebug 答案,但没有得到我的解决方案,所以我决定在 Whosebug 中提问。
问题: shouldStartLoadWithRequest 未被调用。我在 calliOSFunction 中添加了警报,它正在运行。我需要在 showToastPassValues.
中获得的所有值 这是我的代码:

index.html

    function showToastPassValues(phrase,thisAddress,thisPrivKey,thisPubKey){
    var phrase = phrase;
    var thisAddress = thisAddress;
    var thisPrivKey = thisPrivKey;
    var thisPubKey = thisPubKey;
    //app.makeToastPassValues(phrase,thisAddress,thisPrivKey,thisPubKey);
    //var message = {"key1":"value1", "key2":"value2", "dictionary": {"name": "foo"}};
    //window.webkit.messageHandlers.fontsReady.postMessage(message);
    calliOSFunction("sayHello", phrase, "onSuccess", "onError");
    return false;
}
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);
    execute(url);
}
function openCustomURLinIFrame(src)
{
    var rootElm = document.documentElement;
    var newFrameElm = document.createElement("IFRAME");
    newFrameElm.setAttribute("src",src);
    rootElm.appendChild(newFrameElm);
    //remove the frame now
    newFrameElm.parentNode.removeChild(newFrameElm);
    newFrameElm = null;
}
function execute(url)
{
    var iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", url);
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;
}
function onSuccess (ret)
{
    if (ret)
    {
        var obj = JSON.parse(ret);
        document.write(obj.result);
    }
}
function onError (ret)
{
    if (ret)
    {
        var obj = JSON.parse(ret);
        document.write(obj.error);
    }
}  

Viewcontroller.m

    - (void)viewDidLoad {
        [super viewDidLoad];
        _webView.delegate = self;
        NSString *filePath;
        NSBundle *thisBundle;
        NSMutableString *html;
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 1024,768)];
        thisBundle = [NSBundle mainBundle];
        filePath = [thisBundle pathForResource:@"index" ofType:@"html"];
        html = [NSMutableString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        //[_webView loadHTMLString:[NSString stringWithFormat:@"%@",html] baseURL: [[NSBundle mainBundle] bundleURL]];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

        [self.view addSubview:_webView];
    }  
#pragma mark WKWebView Delegaet Methods
-(void)webViewDidStartLoad:(UIWebView *)webView1 {
    //NSLog(@"start");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView1 {
    //NSLog(@"finish");
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Error for WEBVIEW: %@", [error description]);
}
#pragma mark -
#pragma mark Html to Objective-C Methods

- (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];
}
- (BOOL) processURL:(NSString *) url {
    NSString *urlStr = [NSString stringWithString:url];
    NSString *protocolPrefix = @"js2ios:///";
    //process only our custom protocol
    if ([[urlStr lowercaseString] hasPrefix:protocolPrefix]) {
        //strip protocol from the URL. We will get input to call a native method
        urlStr = [urlStr substringFromIndex:protocolPrefix.length];
        //Decode the url string
        //urlStr = [urlStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        urlStr = [urlStr stringByRemovingPercentEncoding];
        NSError *jsonError;
        //parse JSON input in the URL
        NSDictionary *callInfo = [NSJSONSerialization JSONObjectWithData:[urlStr dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];
        //check if there was error in parsing JSON input
        if (jsonError != nil) {
            NSLog(@"Error parsing JSON for the url %@",url);
            return NO;
        }
        //Get function name. It is a required input
        NSString *functionName = [callInfo objectForKey:@"functionname"];
        if (functionName == nil) {
            NSLog(@"Missing function name");
            return NO;
        }
        NSString *successCallback = [callInfo objectForKey:@"success"];
        NSString *errorCallback = [callInfo objectForKey:@"error"];
        NSArray *argsArray = [callInfo objectForKey:@"args"];
        [self callNativeFunction:functionName withArgs:argsArray onSuccess:successCallback onError:errorCallback];
        return NO;
    }
    return YES;
}
- (void) callNativeFunction:(NSString *) name withArgs:(NSArray *) args onSuccess:(NSString *) successCallback onError:(NSString *) errorCallback {
    if ([name compare:@"sayHello" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
        if (args.count > 0) {
            NSLog(@"args : %@",args);

        } else {
            NSString *resultStr = [NSString stringWithFormat:@"Error calling function %@. Error : Missing argument", name];
            [self callErrorCallback:errorCallback withMessage:resultStr];
        }
    } else {
        //Unknown function called from JavaScript
        NSString *resultStr = [NSString stringWithFormat:@"Cannot process function %@. Function not found", name];
        [self callErrorCallback:errorCallback withMessage:resultStr];
    }
}
-(void) callErrorCallback:(NSString *) name withMessage:(NSString *) msg {
    if (name != nil) {
        //call error handler
        NSMutableDictionary *resultDict = [[NSMutableDictionary alloc] init];
        [resultDict setObject:msg forKey:@"error"];
        [self callJSFunction:name withArgs:resultDict];
    } else {
        //NSLog(@"%@",msg);
    }
}

-(void) callSuccessCallback:(NSString *) name withRetValue:(id) retValue forFunction:(NSString *) funcName {
    if (name != nil) {
        //call succes handler
        NSMutableDictionary *resultDict = [[NSMutableDictionary alloc] init];
        [resultDict setObject:retValue forKey:@"result"];
        [self callJSFunction:name withArgs:resultDict];
    } else {
        //NSLog(@"Result of function %@ = %@", funcName,retValue);
    }
}
-(void) callJSFunction:(NSString *) name withArgs:(NSMutableDictionary *) args {
    NSError *jsonError;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:args options:0 error:&jsonError];
    if (jsonError != nil) {
        //call error callback function here
        NSLog(@"Error creating JSON from the response  : %@",[jsonError localizedDescription]);
        return;
    }
    NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    if (jsonStr == nil) {
        NSLog(@"jsonStr is null. count = %lu", (unsigned long)[args count]);
    }
}  

提前致谢。

得到了答案我尝试使用 loadHTMLString 加载我的 html 文件并且它有效。