3

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?

adamdehaven
  • 5,890
  • 10
  • 61
  • 84

2 Answers2

1

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

Community
  • 1
  • 1
Rohit Gupta
  • 408
  • 5
  • 10
  • The code I updated above in my original question worked for me, but can you help me out with the error? – adamdehaven Jul 06 '12 at 14:17
  • 1
    Delegate property of webview `webView.delegate` is of type `id` which doesn't match with type of `self` which is ViewController (_in your case I think class name is UIWebViewViewController, this is based on exception in your comment_). You can fix this by one or 2 ways: Either typcasting `Self` in following way `webView.delegate = (id)self;` or by declaring delegate in class (.h file) `@interface ViewController : UIViewController `. Let me know if this doesnt help. – Rohit Gupta Jul 07 '12 at 07:22
0

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;
}
4444
  • 3,541
  • 10
  • 32
  • 43
Dwight Mix
  • 11
  • 1
  • 1
    This is great only if you know the exact url(s) you want to open in safari, doesn't address any generic link with `target="_blank"` set on it – Russ May 21 '15 at 18:38