我想在webview的shouldStartLoadWithRequest委託方法中運行下面的代碼,但它不作任何更改。shouldStartLoadWithRequest不運行stringByEvaluatingJavaScriptFromString方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"webView shouldStartLoadingWithRequest");
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('bold', false,null)"]];
return TRUE;
}
沒有錯誤,它打印的NSLog和除「stringByEvaluatingJavaScriptFromString法」 一切都在方法的偉大工程。
但是,如果我嘗試在另一個函數中使文本變爲粗體,例如一個IBAction方法,可以正常工作。
-(IBAction)boldClick:(id)sender
{
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('bold', false,null)"]];
}
其實,這是我公司的特殊應用,這UIWebView中不會顯示網頁。我用它來顯示一些自定義的HTML頁面。 我需要在「shouldStartLoadWithRequest」中做所有事情,因爲我試圖從javascript運行一個Objective-C方法。
UPDATE
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
// Break apart request URL
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
// Check for your protocol
if ([components count]==3)
{
[self makeBoldText];
return NO;
}
else
{
return TRUE;
}
}
-(void)makeBoldText
{
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.execCommand('bold', false,null)"]];
}