2013-08-27 90 views
4

我知道最有可能的答案是非常明顯的,但我在網上找遍了,我什麼也沒找到。我使用這種方法來查看用戶是否按下了WebViewUIWebView和點擊鏈接

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; { 

我可以向你保證它能正常工作。

我想要做的就是根據ID

ES

<a id="hello" href="..."><img src="..." /></a> 

一旦委託檢測「觸摸點擊」在IMG的「你好」的ID,我會做出不同的動作做一些自定義的東西,如[self callSomething];

你能告訴我如何使用示例代碼?感謝

+0

爲什麼你使用webview呢?使用uibutton並設置背景圖像和onclick按鈕調用[self callsomething] –

回答

4

更改您的代碼如下

<a id="hello" href='didTap://image><img src="..." /></a> 

並在委託方法中嘗試這樣。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{  
NSString *absoluteUrl = [[request URL] absoluteString]; 
NSString*temp=[absoluteUrl stringByReplacingOccurrencesOfString:@"@" withString:@""]; 

if ([temp isEqualToString:@"didTap://image"]) 
{ 
    [self your method]; 
} 
return YES; 
    } 
3

UIWebView不能從DOM元素接收ID,但有一兩件事你可以做的是通過價值href URL中包含一個參數hello

<a id="hello" href="//myurl?id=hello"><img src="..." /></a> 

,你可以得到的參數爲:

URLParser *parameter = [[URLParser alloc] initWithURLString:@"http://myurl/id=hello"]; 
NSString *id = [parameter valueForVariable:@"id"]; 
2

要做到這一點,你應該把的JavaScript的onClick處理程序以任何DOM元素,你需要

FE

<a onClick="callNativeSelector('doSomething');" ... > </a> 

javascript function callNativeSelector(nativeSelector) { // put native selector as param 
    window.location = "customAction://"+nativeSelector; 
} 

在一個UIWebView委託的方法忽略了像上面

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 

    if ([[request.URL scheme] isEqualToString:@"customAction"]) { 
     //Fetching image URL 
     NSLog(@"Custom selector is %@", [request.URL host]) 
     ... 
     // Always return NO not to allow `UIWebView` process such links 
     return NO; 
    } 
    .... 
} 

有聯繫從我的觀點來看是好處:

  • 不與像<a href=...>特定的DOM元素相關聯,你可以這樣處理分配到任何你需要

  • 不支持html,id屬性

  • 能力內UIWebView相關忽略裝載等環節,只是執行你的customSelector本質上是