简体   繁体   中英

Open Link in UIWebView in Safari

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM