在Safari中的UIWebView中打开链接 [英] Open Link in UIWebView in Safari
问题描述
我正在开发一个应用程序,它接收我在UIWebView中显示的提要。 Feed中嵌入了我想要在Safari中打开的链接而不是WebView。我查看过这里发布的其他几个问题。我不确定我错过了什么,但我认为这很简单。这是我的.h和.m文件
I am working on an app that receives a feed that I display in a UIWebView. The feed has embedded links that I want to open in Safari instead of thew WebView. I have looked over several other questions that are posted here. I am not sure what I am missing, but I think it is something simple. Here are my .h and .m files
#import
@class BlogRss;
@interface EnduranceDailyWorkoutViewController : UIViewController {
IBOutlet UIWebView * descriptionTextView;
BlogRss * currentlySelectedBlogItem;
}
@property (nonatomic, retain) UIWebView * descriptionTextView;
@property (readwrite, retain) BlogRss * currentlySelectedBlogItem;
@end
#import "EnduranceDailyWorkoutViewController.h"
#import "BlogRss.h"
@implementation EnduranceDailyWorkoutViewController
@synthesize descriptionTextView = descriptionTextView;
@synthesize currentlySelectedBlogItem = currentlySelectedBlogItem;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *html = [NSString stringWithFormat:@"%@ %@",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}
-(BOOL)webView:(UIWebView *)descriptionTextView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (UIWebViewNavigationTypeLinkClicked == navigationType) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
使用Interface Builder我已经链接了IBOutlet和UIWebView。请让我知道我错过了什么。我已经在webView部分放置了断点,但是代码永远不会出现在那里,所以它几乎就像在IB中没有正确链接。
Using Interface Builder I have linked the IBOutlet and the UIWebView. Please let me know what I am missing. I have put break points in the webView section but the code never hits there so it is almost like something is not linked correctly in IB.
推荐答案
您需要确保将UIWebView的委托设置为您的控制器。您可以在界面构建器中执行此操作,也可以修改viewDidLoad方法:
You need to ensure that the UIWebView's delegate is set to your controller. You can do this in interface builder, or you can modify your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// add this line to set the delegate of the UIWebView
descriptionTextView.delegate = self;
NSString *html = [NSString stringWithFormat:@"%@ %@",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}
这篇关于在Safari中的UIWebView中打开链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!