James
James

Reputation: 541

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.

Upvotes: 0

Views: 4865

Answers (1)

Greg
Greg

Reputation: 33650

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

Upvotes: 1

Related Questions