UIWebView在iOS中执行JavaScript时显示错误

时间:2013-10-21 22:55:19

标签: ios sdk uiwebview

我有一个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;
    }
}

1 个答案:

答案 0 :(得分: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];



   }