UIWebView在iOS中执行JavaScript时显示错误
我有一个UIWebView,其中还执行了一些javascript。当我们点击第一个按钮时,它会正确地执行javascript代码并完美地工作,但是当我们点击任何其他按钮,然后导航回旧视图时,javascript不工作。下面是我的代码UIWebView在iOS中执行JavaScript时显示错误,ios,sdk,uiwebview,Ios,Sdk,Uiwebview,我有一个UIWebView,其中还执行了一些javascript。当我们点击第一个按钮时,它会正确地执行javascript代码并完美地工作,但是当我们点击任何其他按钮,然后导航回旧视图时,javascript不工作。下面是我的代码 - (void)webViewDidStartLoad:(UIWebView *)webView { NSMutableString *javascriptFunctions = [NSMutableString stringWithString:@"va
- (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;
}
}
在网页完全加载到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];
}