2010-10-23 97 views
1

您好我有一个uiwebview,我已经准备好要在webview中加载的htmlstring格式的数据。来自UIWebview的本地函数调用

我正在使用此行在uiwebview中加载htmlstring。

 [webView loadHTMLString:htmlstring baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",imagePath]]]; 

在HTML格式化字符串我们有一个形象button.When我点击图像按钮,我想打电话给在ViewController.m文件中的一个局部功能。

可能吗?请帮帮我 。

我使用

<input type=""image"" onclick=""func()"" src=""icon_blog.png"" name=""image"" width=""30"" height=""30""> 

    <script language=""javascript""> function func(){alert(""hee"");} 

但警报在iPhone未显示。任何人帮助我?在此先感谢.......

回答

3

没有简单的方法来从UIWebView中加载的页面内调用objective-c代码。

唯一的方法是拦截UIWebView代理中的虚拟页面加载(– webView:shouldStartLoadWithRequest:navigationType:)。在那里你可以根据request.URL做出决定。

- (BOOL)webView:(UIWebView*)_webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSArray *components = [[[request URL] absoluteString] componentsSeparatedByString:@":"]; 
    if ([components count] > 1 && [[components objectAtIndex:0] isEqualToString:@"objc"]) { 
     if ([[components objectAtIndex:1] isEqualToString:@"someFunctionality"]) { 
      // Do something 
     } 
     return NO; 
    } 
    return YES; 
} 

您可以通过点击一个链接,提交一个表单触发这些请求,或者从JavaScript通过设置窗口的位置是这样的:window.location = "objc:someFunctionality";

+0

关于使用的任何实例? – Jirapong 2011-02-03 17:56:17