0

我有一个 UIWebView,其中一些 javascript 也在执行。当我们单击第一个按钮时,它会正确执行 javascript 代码并完美运行,但是当我们单击任何其他按钮然后导航回旧视图时,javascript 不起作用。下面是我的代码。

- (void)webViewDidStartLoad:(UIWebView *)webView { 
    NSMutableString *javascriptFunctions = [NSMutableString stringWithString:@"var t3 = new function() { this.setTitle = function(text) { window.location.href = 'about:title:' + encodeURIComponent(text); }; "];
    [javascriptFunctions appendString:@"this.setAppointment = function(beginTime, endTime, title, description, isUTC) { window.location.href = 't3://web-command/calendar?beginTime=' + encodeURIComponent(beginTime) + '&endTime=' + encodeURIComponent(endTime) + '&title=' + encodeURIComponent(title) + '&description=' + encodeURIComponent(description) + '&isUTC=' + encodeURIComponent(isUTC); }; "];
    [javascriptFunctions appendString:@"this.setBackButtonVisibility = function(isVisible) { window.location.href = 'about:back:' + encodeURIComponent(isVisible); }; "];
    [javascriptFunctions appendString:@"this.getBack = function() { window.history.back(); }; "];
    [javascriptFunctions appendString:@"this.close = function() { window.location.href = 'about:close'; }; }"];

    [self.webView stringByEvaluatingJavaScriptFromString:javascriptFunctions];
}


- (BOOL)webView:(UIWebView *)currentWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[request.URL.scheme lowercaseString] isEqualToString:@"tel"]) {
        return YES;
    }

    NSString* url = [request.URL.absoluteString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    if (url == nil) {
        [self backToMainScreen];
    return NO;
    }
}
4

1 回答 1

0

在 UIWebView 上完全加载网页之前,不会调用 Javascript 函数。所以在委托方法 webViewDidFinishLoad 中调用了你的所有 javascript 函数。

 - (void)webViewDidFinishLoad:(UIWebView *)webView {

    NSMutableString *javascriptFunctions = [NSMutableString stringWithString:@"var t3 = new function() { this.setTitle = function(text) { window.location.href = 'about:title:' + encodeURIComponent(text); }; "];
    [javascriptFunctions appendString:@"this.setAppointment = function(beginTime, endTime, title, description, isUTC) { window.location.href = 't3://web-command/calendar?beginTime=' + encodeURIComponent(beginTime) + '&endTime=' + encodeURIComponent(endTime) + '&title=' + encodeURIComponent(title) + '&description=' + encodeURIComponent(description) + '&isUTC=' + encodeURIComponent(isUTC); }; "];
    [javascriptFunctions appendString:@"this.setBackButtonVisibility = function(isVisible) { window.location.href = 'about:back:' + encodeURIComponent(isVisible); }; "];
    [javascriptFunctions appendString:@"this.getBack = function() { window.history.back(); }; "];
    [javascriptFunctions appendString:@"this.close = function() { window.location.href = 'about:close'; }; }"];

    [self.webView stringByEvaluatingJavaScriptFromString:javascriptFunctions];



   }
于 2013-10-22T00:19:29.360 回答