UIWebView 打开外部链接
Posted
技术标签:
【中文标题】UIWebView 打开外部链接【英文标题】:UIWebView opening external links 【发布时间】:2014-05-23 09:40:30 【问题描述】:我有这个 uiwebview 代码,除了强制任何链接不使用 uiwebview 打开的部分(基本上我希望在浏览器中打开链接)之外,它在主要工作上有效。
谁能指出我正确的方向或看看我的代码有什么问题
JGViewController.h #导入
@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】:通过添加修复
self.webview.delegate = self;
查看DidLoad函数
【讨论】:
我相信它是通过 UI 设置的,因为你有 IBOutlet以上是关于UIWebView 打开外部链接的主要内容,如果未能解决你的问题,请参考以下文章
ios 8 webview在Safari中打开外部链接,除了一个