How to integrate Webview in iPhone? - 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; }

Related

How to customise the string value in uiweb view ios

In my application I want to show the URL link value as "Click here to see the link" in UIWebView?
For ex: link: http://www.youtube.com/watch?v=pii4G8FkCA4
I want to show it as in UIWebView as "click here to see the link"
Can any one please help me to how to do that?
Try this,
NSString *html = #"click here to see the link";
[webview loadHTMLString:html baseURL:nil];
This will help you to load URL in webView.
try like this ,you will get,
UIWebView *webview=[[UIWebView alloc]init];
webview.frame=CGRectMake(0, 0, 320, 480);
NSString *html = #"click here to see the link";
[webview loadHTMLString:html baseURL:nil];
[self.view addSubview:webview];
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
TRy this,
viewcontroller.h
IBOutlet UIWebView *webView;
viewController.m
NSString *strHtml = #"click here to see the link";
[webview loadHTMLString:strHtml baseURL:nil];

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"]]];
}

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

Creare a custom action for an existing button in a webview

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];.

Stopping network activity indicator

I used the below code to load a webpage. Everything works fine, but I want to stop the
network activity indicator after completing loading the webpage. How can we know that
the webpage is loaded completely.
Anyone please help.
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES; // to stop it, set this to NO
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
The simplest way to do this is to add this line after you instantiate your UIWebView.
[webView setDelegate:self];
Now you will call webViewDidFinishLoad: and the entire method should look like this.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
I'll explain this further since you will absolutely need to understand delegation in the future.
UIWebView is merely an object that displays web data. It doesn't really like handling all this other stuff, because it really just loves web data. Now in your case you wanted to find out when UIWebView was done doing its favorite little task. To do this, your UIWebView gives a shoutout to it's delegate like "Yo holmes, I'm done loading this data now. So go do whatever it is you do now." and your UIWebView continues on its merry way.
So what we did is we told the UIWebView that its delegate, in this case, was our current class by setting the delgate property on webView to self.
From there you can call any of the methods available to the UIWebView delegate (its all in the documentation) and have them perform tasks secondary to the main purpose of the UIWebView.
Make sense?
You just need to set webView.delegate to point to some object, and have that object implement webViewDidFinishLoad:.
To show network Indicator while UIWebView load request use below code..
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
return YES;
}
//For Showing NetWork Indicator in Webview
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Note: UIViewController<UIWebViewDelegate> and [webView setDelegate:self];
here's what I used to stop the activity indicator:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = [NSString stringWithFormat:#"http://www.wikipedia.com/wiki/%#", place.name];
NSURL *url= [NSURL URLWithString: [urlAddress stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[self webView] setDelegate:self];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
}
- (void) webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}