Search code examples
iosipadmobileuiwebviewmobile-safari

UIWebView opening external links


I have this uiwebview code which on the main works except for the part that forces any links to not open with the uiwebview (basically I would like links to open in the browser)

Can anyone point me in the right direction or see what is wrong with my code

JGViewController.h #import

@interface JGViewController : UIViewController <UIWebViewDelegate> {
    IBOutlet UIWebView *customWebView;
}
@property (strong, nonatomic) IBOutlet UIWebView *customWebView;

@end

JGViewController.m

#import "JGViewController.h"

@interface JGViewController ()

@end

@implementation JGViewController
@synthesize customWebView;

//force any links to open in browser rather than in the uiwebview
-(BOOL) webView:(UIWebView *)customWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL* url = [request URL];
    if (UIWebViewNavigationTypeLinkClicked == navigationType) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }

    return YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    NSString *httpSource = @"http://www.google.co.uk";
    NSURL *fullUrl = [NSURL URLWithString:httpSource];
    NSURLRequest *httpRequest = [NSURLRequest requestWithURL:fullUrl];
    customWebView.scalesPageToFit = YES;

    customWebView.frame=self.view.bounds;

    [customWebView.scrollView setShowsHorizontalScrollIndicator:NO];
    customWebView.scrollView.scrollEnabled = NO;
    customWebView.scrollView.bounces = NO;

    //actually call the page into the uiwebview
    [customWebView loadRequest:httpRequest];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Solution

  • fixed by adding

    self.webview.delegate = self;
    

    to viewDidLoad function