Creare a custom action for an existing button in a webview - iphone

I'm displaying an URL on a webview, and on that page there is a button. How can I customize the action that occurs when that button is pressed?

You controller has to respond to <UIWebViewDelegate>.
Then, override this method:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
NSURL *URL = [request URL];
if ([[URL absoluteString] rangeOfString:#"http://give.it.a.custom.url.to.identify.this.action"].length > 0)
{
// your custom action here
}
return NO;
}
return YES;
}
Don't forget the [youWebView setDelegate:self];.

Related

how to change UIWebView Tap events

How Can I change the UIWebView tap action Controls.like when I tap and hold on some link in UIWebView ..it opens a UIActionSheet with three options open copy add to reading list ...I need to change this UIActionsheet controls like ..I need to add one more button into this ...how to do that...how to disable this and add new UIActionSheet according to my choice...
code
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [ request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
// Call your custom actionsheet and use the requestURL to do what you want :)
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:#"Contextual Menu"
delegate:self cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil otherButtonTitles:nil];
[sheet addButtonWithTitle:#"Save Page as Bookmark"];
[sheet addButtonWithTitle:#"Open Page in Safari"];
[sheet showInView:webView];
return NO;
}
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
webview.delegate=self;
}
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == actionSheet.cancelButtonIndex) { return; }
switch (buttonIndex) {
case 0:
{
NSLog(#"Item A Selected");
NSLog(#"reg%#", request);
NSURL *requestURL = [request URL];
[webview loadRequest:[NSURLRequest requestWithURL:requestURL]];
break;
}
case 1:
{
NSLog(#"Item B Selected");
break;
}
}
}
You can do like this , catch a tap on link and use your actionsheet then
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *requestURL = [ request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
// Call your custom actionsheet and use the requestURL to do what you want :)
return NO;
}
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
webview.delegate=self;
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]];
}

How to integrate Webview in iPhone?

I am working in iPhone application, Using Webview to load Addvertisement in bottom of the screen and its working fine, i want to when the user select the WebView, its automatically goes to browser and load in iPhone device, How to integrate this? please help me
Thanks in Advance
I tried this:
- (void)viewDidLoad
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
[webView setDelegate:self];
NSString *urlAddress = #"http://www.dasfafa./myadds.html";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *currentURL = self.AddvertiseWebView.request.URL.absoluteString;
NSLog(#"currentURL:%#",currentURL);
}
Here code showing you how to open a link in Safari when clicked on in your UIWebView. The method shouldStartLoadWithRequest is a delegate method that is called when a link is clicked on. You can override the method and tell it to open in Safari.
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType; {
NSURL *requestURL = [ [ request URL ] retain ];
// Check to see what protocol/scheme the requested URL is.
if ( ( [ [ requestURL scheme ] isEqualToString: #"http" ]
|| [ [ requestURL scheme ] isEqualToString: #"https" ] )
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
// Auto release
[ requestURL release ];
// If request url is something other than http or https it will open
// in UIWebView. You could also check for the other following
// protocols: tel, mailto and sms
return YES;
}
or Try this
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES; }

Get the Query Of a UIWebView URL

Hey guys I'm trying to find a way to specifically grab the Query String out of the URL that my UIWebView currently has loaded up.
So for example:
www.google.com?page=235
I want to end up with a NSString with the text "page=235".
Or even better Page at a NSData Key of Title and 235 at a Key of ID.
Im basically trying to organise the Query String into useable data.
In my project I have a button:
[HTML appendString:#"<a href='didtap://goto?Page=56'>Post Comment</a>"];
That i access with checking the absolute URL to see if the button was fired.
Is there any way to profile the information the way i want to?
I create the demo what you want get which is describe further..
Following is the viewcontroller.h file code
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
NSURL *Url;
}
#property(nonatomic,retain)IBOutlet UIWebView *webVctr;
#end
This may helping to solve your problem
Thanks
Following is the viewcontroller.m file code
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize webVctr = _webVctr;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"%#",request);
Url = [request URL];
// if ([[UrlParts objectAtIndex:(1)] isEqualToString:#"NeverGonnaFindMe"]) {
// CONVERT TO STRING AN CLEAN
NSString *urlResources = [Url resourceSpecifier];
urlResources = [urlResources stringByReplacingOccurrencesOfString:#"?" withString:#"/"];
// SEPORATE OUT THE URL ON THE /
NSArray *urlResourcesArray = [urlResources componentsSeparatedByString:#"/"];
// THE LAST OBJECT IN THE ARRAY
NSString *urlParamaters = [urlResourcesArray objectAtIndex:([urlResourcesArray count]-1)];
// SEPORATE OUT THE URL ON THE &
NSArray *urlParamatersArray = [urlParamaters componentsSeparatedByString:#"&"];
if([urlParamatersArray count] == 1) {
NSString *keyValue = [urlParamatersArray objectAtIndex:(0)];
NSArray *keyValueArray = [keyValue componentsSeparatedByString:#"="];
if([[keyValueArray objectAtIndex:(0)] isEqualToString:#"page"]) {
NSLog(#"%#",[keyValueArray objectAtIndex:1]);
}
[self dismissModalViewControllerAnimated:YES];
return NO;
}
return YES;
}
// ACTIVITY INDICATOR
- (void)webViewDidStartLoad:(UIWebView *)webView{
/// [_activityIndicator startAnimating];
NSLog(#"Resting to server");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
// [_activityIndicator stopAnimating];
NSLog(#"Finish request server");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(#"Error");
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *fullAuthUrlString =[[NSString alloc] initWithFormat:#"www.google.com?page=235"];
NSURL *authUrl = [NSURL URLWithString:fullAuthUrlString];
NSURLRequest *myRequest = [[NSURLRequest alloc] initWithURL:authUrl];
[self.webVctr loadRequest:myRequest];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
To get the query string, get the query string of a NSURL object.
NSURL *url = …;
NSString *queryString = url.query;
For more information, see Accessing the Parts of the URL in the NSURL class documentation.

Changing tel: property working in simulator but Not in Device

I have changed tel property to do some string manipulation instead of calling that number.
its working in simulator but when i do it device, its still trying to call.
i have tried with webview, textview, button still same problem :(
Any idea?
Let me rephrase
By default, when clicked on any link with tel:xxxx iPhone calls number xxxx
But i don't want calling, instead i want to grab xxxx and append that value to my String.
How to do that.
Thanks
- (void)viewDidLoad {
[super viewDidLoad];
self.htmlString = [self createHTML];
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
[webView loadHTMLString:self.htmlString baseURL:nil];
}
- (NSMutableString *) createHTML {
NSMutableString *tempString =[NSMutableString stringWithFormat:#"<div>"];
[tempString appendString:#"John Smith"];
[tempString appendFormat:#"<br> <a href='tel:201207415'>201207415</a>"];
[tempString appendString:#"</div>"];
return tempString;
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
NSURL *url = [request URL];
NSString *mytext = [url absoluteString];
NSLog(#"%#",mytext);
myLabel.text = mytext;
return YES;
}
If you do not want to call:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
return NO;
}
Simulator cannot call anyway.

XCode: How can I check the URL type on every page load in UIWebView?

I need some sample code to check if the page is 'file', 'http' or 'www' on the page load. At the moment the code is as follows;
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
//CAPTURE USER LINK-CLICK.
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"file"]) {
[webView loadRequest:request];
[homebutton setHidden:YES];
NSLog(#"Opened File");
return NO;
}
else if ([[URL scheme] isEqualToString:#"http"]) {
[webView loadRequest:request];
[homebutton setHidden:NO];
NSLog(#"Opened External Page");
return NO;
}
else if ([[URL scheme] isEqualToString:#"www"]) {
[webView loadRequest:request];
[homebutton setHidden:NO];
NSLog(#"Opened External Page");
return NO;
}
}
return YES;
}
I want it based around this but this is only when a link is clicked and I want it to apply when a form is filled out and then the form redirects to another page etc...
I would really appreciate a quick answer,
Thank you very much,
James Anderson
Probably you need to check out the various UIWebViewNavigationType listed in the "constants" section here