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;
///Button Code
- (IBAction)buttonClick:(id)sender{
NSLog(@"button %@",[sender tag]);
}
The only method I know is by implementing
webView:shouldStartLoadWithRequest:navigationType
method ofUIWebViewDelegate
protocol. The required steps are as follows:1) Create a new class subclassed from NSObject named, say, MyWebViewDelegate. You can implement only that method:
2) Create instance of delegate in your code and set it as a delegate of UIWebView before loading page:
3) And then try to debug. Try to put breakpoint inside ..sholdStartLoadWithRequest:.. method and ensure that you application stops in that breakpoint. Maybe you shold do a little redesign of you HTML webpage to achieve this. After that try to distinguish different buttons by value of
request
parameter.Inside your html code use this snippet:
on native side, in uiwebview's delegate method:
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 :)
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://" + dataInStringToSend Then, 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).