UIWebView打开外部链接

时间:2014-05-23 09:40:30

标签: ios ipad mobile uiwebview mobile-safari

我有这个uiwebview代码,主要工作除了强制任何链接不能用uiwebview打开的部分(基本上我希望链接在浏览器中打开)

任何人都可以指出我正确的方向或看到我的代码有什么问题

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

1 个答案:

答案 0 :(得分:0)

通过添加

修复
self.webview.delegate = self;

to viewDidLoad function