2010-10-23 37 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