uiwebview中使用javascript来获取点击的按钮ID

uiwebview中使用javascript来获取点击的按钮ID

本文介绍了如何在uiwebview中使用javascript来获取点击的按钮ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这次我想知道如何确定在UIWebView中点击哪个按钮.....

This Time i want to know how to determine which button is click in UIWebView.....

    appDelegate.mystring = [[NSMutableString string]init];
    NSString *buttonstring=@"<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]);
}


推荐答案

在您的html代码中使用此代码段:

Inside your html code use this snippet:

<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的委托方法中:

on native side, in uiwebview's delegate method:

- (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,因为:)

With regards to the code on web side - I don't juggle with this dynamically created iframe without a reason. At first glance, it would seem sufficient just to change the location on webview's page (call document.location.href = "jscall://message"). The worst thing is, that is actually seems to work great. Unfortunately, if you take this shortcut, JS timers are messed up greatly. So take my advice - use this little, weird-looking, but working without any side-effects "sendRawMessageToiOS" as is :)

小更深入地描述上面代码中发生的事情:
在网络代码中,每当我想将一些数据推送到我的应用程序的本机端时,我都会调用sendRawMessageToiOS(dataInStringToSend)。它创建一个iframe,将其添加到DOM树并将其位置设置为
jscall://+ dataInStringToSend
然后,正如预期的那样,uiwebview的委托被询问,不管我是不是愿意开始从给定地址下载内容的请求。我检查它是否是实际地址(然后返回YES)或只有具有特殊jscall://前缀的伪装。如果是这个特殊的电话,我会读取用它传达的数据,并回答否(因为webview不应该尝试启动任何实际请求)。

Little more in-depth description of what's going on in the above code:In the web-code, whenever I want to push some data to the native-side of my app, I call sendRawMessageToiOS(dataInStringToSend). It creates an iframe, adds it to the DOM tree and sets it's location to be"jscall://" + dataInStringToSendThen, as expected, the uiwebview's delegate is asked, whether or not I'm willing to start the request to download a content from a given address. I check if it's actual address (and return YES then) or only this masquerade with special "jscall://" prefix. If it is this special call, I read data that's conveyed with it, and respond NO (because webview should not try to start any real request).

这篇关于如何在uiwebview中使用javascript来获取点击的按钮ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 19:12