2011-02-04 11 views
2

私はのUIWebViewでクリックされたボタンを判断する方法を知りたいこの時間は.....uiwebviewでjavascriptを使用してクリックされたボタンIDを取得する方法は?

appDelegate.mystring = [[NSMutableString string]init]; 
    NSString *[email protected]"<label><input type=\"submit\" name=\"button\" id=\"1\" value=\"Delete\" /></label>"; 

    for (int i=0; i<appDelegate.lyricsData.count; i++) { 
    NSString *b= @"<br>"; 
    NSString *st1=[[appDelegate.lyricsData objectAtIndex:i] objectForKey:@"user_name"]; 
    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:st1]; 
    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:b]; 
    NSString *st2=[[appDelegate.lyricsData objectAtIndex:i] objectForKey:@"added_date"]; 
    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:st2]; 
    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:b]; 
    NSString *st3=[[appDelegate.lyricsData objectAtIndex:i] objectForKey:@"verse"]; 

    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:st3]; 
    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:buttonstring]; 
    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:b]; 
    appDelegate.mystring=[appDelegate.mystring stringByAppendingString:b]; 



    btn_back1_en.tag = i; 
    NSLog(@"%d",btn_back1_en.tag); 
    [btn_back1_en addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; 
    [buttonArray insertObject:btn_back1_en atIndex:i]; 
    [btn_back1_en release];  



    } 
    NSLog(@"My string %@",appDelegate.mystring); 

    UIWebView *mywebview = [[UIWebView alloc] initWithFrame:CGRectMake(0,70, 320, 240)]; 
    mywebview.backgroundColor=[UIColor clearColor]; 
    mywebview.opaque=NO; 
    mywebview.dataDetectorTypes= UIDataDetectorTypeNone; 



// working 
    NSString *htmlTempString = [NSString stringWithFormat:@"<html><head><meta name=\"viewport\" content=\"width=200; initial-scale=1.0; maximum-scale=0; user-scalable=0;\" /> </head>   <style> *{padding:0px;margin:0px;}.wrap{width:320px;background:#000000;color:#FFFFFF;font-family:'Myriad Pro',Arial, Helvetica, sans-serif; font-size:12px;}.head{ padding:15px 20px;background:url(images/bg.png) repeat-x bottom #383838;display:block; border:1px } .head-left{ float:left; width:54px;}.head-right{float:left; width:224px; clear:right; padding:0px 0px 8px 0px;} h1{ padding:0px; font-size:16px; color:#fff;} h2{ padding:0px; font-size:14px; color:#3a89d3;}small{ font-size:11px; color:#77c900; padding:0px;font-weight:normal;} b{ padding:0px;font-size:16px; color:#fff; }.comments{ background:url(images/bot-line.png) no-repeat bottom left; padding:10px 0px;}.comments h1{ padding:0px;font-size:16px; color:#fff; display:inline}.comments h2{ padding:0px;font-size:14px; color:#3a89d3; display:inline; margin:0px;}.comments small{ font-size:11px; color:#77c900; padding:0px; margin:0px 0px 0px 5px; font-weight:normal; display:inline;} .comments b{ padding:0px; margin:0px 0px 5px 5px; font-size:16px; color:#fff; display:inline} .status{padding:15px 20px;background:url(images/bg.png) repeat-x bottom #383838;display:block; margin:4px 0px; } </style> <body> <div class=\"wrap\"> <div class=\"head\"> <div class=\"head-left\"><img src=\"%@\" width=\"54\" height=\"54\" /></div> <div class=\"head-right\"> <h1>%@</h1> <h2>%@</h2> <small>on %@</small> </div> <br clear=\"all\" /> </div> <div class=\"status\"><b>%@</b><div class=\"comments\"><h2>%@ </h2></div><br clear=\"all\" /> </div></div> </body></html>",  
           appDelegate.profilepic,appDelegate.textfield,appDelegate.username,appDelegate.added_date,appDelegate.mytextview,appDelegate.mystring]; 




    [mywebview loadHTMLString:htmlTempString baseURL:nil]; 
    mywebview.delegate=self; 

///ボタンのコード

- (IBAction)buttonClick:(id)sender{ 
    NSLog(@"button %@",[sender tag]); 
} 

答えて

0

私が知っている唯一の方法はwebView:shouldStartLoadWithRequest:navigationTypeを実装することです方法。UIWebViewDelegateプロトコル。必要な手順は次のとおりです。

1)MyWebViewDelegateという名前のNSObjectからサブクラス化された新しいクラスを作成します。あなたはそれだけでメソッドを実装することができます

-(BOOL)webView:(UIWebView*)webView 
     sholdStartLoadWithRequest:(NSURLRequest *)request 
     navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSLog(@"%@", [[request URL] relativePath]); 
} 

2)あなたのコード内で、デリゲートのインスタンスを作成し、ロードページ前のUIWebViewのデリゲートとして設定:

MyWebViewDelegate *mydelegate = [[MyWebViewDelegate alloc] init]; 
mywebview.delegate = mydelegate; 

3)そして、デバッグしてみてください。 ..sholdStartLoadWithRequest:..メソッドの中にブレークポイントを入れて、アプリケーションがそのブレークポイントで停止していることを確認してください。たぶんあなたは、これを達成するためにあなたのHTML Webページを少し再デザインしました。その後、異なるボタンをrequestパラメータの値で区別してみてください。あなたのhtmlコードを使用インサイド

2

このスニペット:ネイティブ側の

<html> 
<head> 

    <script> 

     function iOSNativeBridge(){ 

      this.sendRawMessageToiOS = function(message){ 
//    alert(message); 
       console.log("Message string to iOS: [" + message+ "]"); 
       var iframe = document.createElement("IFRAME"); 
       iframe.setAttribute("src", "jscall://" + message); 
       document.documentElement.appendChild(iframe); 
       iframe.parentNode.removeChild(iframe); 
       iframe = null; 
      }; 

     } 

     bridge = new iOSNativeBridge(); 


    </script> 


</head> 

<body> 

    <a href="javascript:bridge.sendRawMessageToiOS('your message');">link</a> 
    <input type="button" value="button val" name="button" id="1" onClick="javascript:bridge.sendRawMessageToiOS('your message');"> 

</body> 


</html> 

、のUIWebViewのデリゲートメソッドで:ウェブ側のコードに関して

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

    NSString *jsCallPrefix = @"jscall://"; 
    NSString *fullURLRequested = request.URL.absoluteString; 

    BOOL isJavaScriptCall = ([fullURLRequested hasPrefix:jsCallPrefix]); 

    if (isJavaScriptCall) { 
     NSString *messageConveyed = [fullURLRequested substringFromIndex:jsCallPrefix.length]; 
     NSLog(@"Your message was: %@", messageConveyed); 
     return NO; 
    } 

    return YES; 

} 

- 私は両立しません。この動的に作成されたiframeには理由はありません。一見すると、webviewのページ上の位置を変更するだけで十分であるように思えます(document.location.href = "jscall:// message"を呼び出してください)。最悪のことは、実際にはうまくいくようです。残念なことに、このショートカットを使用すると、JSタイマーが大きく台無しになります。だから、私のアドバイスを取る - 使用この奇妙に見える、少し、しかし:)であるとして「sendRawMessageToiOS」任意の副作用なし作業

上記のコードで何が起こっているの

リトルより詳細な説明: でウェブコードは、私のアプリのネイティブ側にいくつかのデータをプッシュしたいときは、sendRawMessageToiOS(dataInStringToSend)を呼び出します。 iframeを作成し、それをDOMツリーに追加し、その場所を "jscall://" + dataInStringToSend に設定します。次に、要求を開始するかどうかに関わらず、uiwebviewの代理人に質問します。特定のアドレスからコンテンツをダウンロードする。私はそれが実際のアドレスであるかどうかをチェックし(そしてYESを返します)、またはこの仮面舞踏会だけに特別な "jscall://"接頭辞を付けます。この特別な呼び出しであれば、私はそれと共に運ばれたデータを読み、NOに答えます(webviewは実際の要求を開始しようとすべきではないので)。

関連する問題