2013-12-09 26 views
6

Ho caricato la pagina html tramite UIWebView. Se l'utente seleziona link che assomiglia a:UIWebView: ottieni attributi dal collegamento ipertestuale cliccato

<a webview="2" href="#!/accounts-cards/<%= item.acctno %>"></a> 

posso ottenere il valore href cliccato nel metodo UIWebViewDelegate da NSURLRequest:

webView:shouldStartLoadWithRequest:navigationType: 

Ma come posso ottenere attribuire valore da questo collegamento ipertestuale (WebView =" ") assumendo che il nome dell'attributo" webview "sia determinato?

+0

Scegli questa ** http: //stackoverflow.com/questions/5775679/how-can-i-get-name-from-link** –

risposta

0

È possibile ottenere l'attributo "webview", con l'aiuto di JavaScript e successivamente è possibile inviare tale attributo e il suo valore al codice Objective C nativo.

Aggiungere questo codice JavaScript nella tua pagina HTML tag script all'interno:

function reportBackToObjectiveC(string) 
{ 
    var iframe = document.createElement("iframe"); 
    iframe.setAttribute("src", "callback://" + string); 
    document.documentElement.appendChild(iframe); 
    iframe.parentNode.removeChild(iframe); 
    iframe = null; 
} 

var links = document.getElementsByTagName("a"); 
for (var i=0; i<links.length; i++) { 
links[i].addEventListener("click", function() { 
var attributeValue=links[i].webview; //this will give you your attribute(webview) value. 
    reportBackToObjectiveC(attributeValue); 
}, true); 
} 

dopo questo tuo metodo webViewDelegate chiamerà:

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

{ 
    if (navigationType == UIWebViewNavigationTypeLinkClicked) 
    { 
     NSURL *URL = [request URL]; 
     if ([[URL scheme] isEqualToString:@"callback"]) 
     { 
      //You can get here your attribute's value. 
     } 
} 
0

È necessario modificare href dei tuoi link. Inietta in primo luogo lo script javascript, che patch i tuoi collegamenti.

- (void)webViewDidFinishLoad:(UIWebView *)webView 
{ 

    NSString *js = @"var allElements = document.getElementsByTagName('a');" 
        "for (var i = 0; i < allElements.length; i++) {" 
        " attribute = allElements[i].getAttribute('webview');" 
        " if (attribute) {" 
        "  allElements[i].href = allElements[i].href + '&' + attribute;" 
        " }" 
        "}"; 
    [webView stringByEvaluatingJavaScriptFromString:js]; 

} 

link inseriti verranno convertiti in formato (nota 2 & in attributo href): <a webview="2" href="#!/accounts-cards/<%= item.acctno %>&2"></a> Poi si può ottenere il vostro richiamata e analizzare WebView valore param:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSArray *array = [request.URL.absoluteString componentsSeparatedByString:@"&"]; 
    if (array.count > 2) { 
     NSLog(@"webview value = %@", array[1]); 
    } 
    return YES; 
} 
Problemi correlati