adamdehaven
adamdehaven

Reputation: 5920

Xcode Open target=_blank links in Safari

I'm pretty new to Xcode... I have a single page iOS app that just has a UIWebView opening a specific URL. I would like any links within the pages that have target="_blank" to open in Safari, rather than inside the app.

Can someone tell me how to accomplish this? (I've searched everywhere) and also tell me in which files and where to put the code? Thank you SOOO much!!

EDIT

I implemented the following code in my ViewController.m file:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Add line below so that external links & PDFs will open in Safari.app
    webView.delegate = self;

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/"]]];

}

// Add section below so that external links & PDFs will open in Safari.app
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return false;
    }
    return true;
}

But for the line webView.delegate = self; I am getting a yellow warning that says: Assigning to 'id'from incompatible type 'UIWebViewViewController *const_strong'

What is this error, and how can I fix it in Xcode?

Upvotes: 3

Views: 6672

Answers (2)

Dwight Mix
Dwight Mix

Reputation: 11

This is the way we solved it:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    first.delegate = (id)self;
                [first loadRequest:[NSURLRequest requestWithURL:[NSURL      URLWithString:@"http://www.website.com"]]];
}

// Add section below so that external links & PDFs will open in Safari.app
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeOther) {
        NSString *checkURL = @"http://www.linkyouwanttogotoviasafari.com";
        NSString *reqURL = request.URL.absoluteString;
        if ([reqURL isEqualToString:checkURL])
             {
                 [[UIApplication sharedApplication] openURL:request.URL];
            return false;
    }
        else {
            return true;
        }
    }
    return true;
}

Upvotes: 0

Rohit Gupta
Rohit Gupta

Reputation: 408

Perhaps following answer on SO can solve your problem or atleast give you some ideas on how to achieve what you are trying to do: UIWebView open links in Safari

Upvotes: 1

Related Questions