0

Hi, I have a slight issue. I have tried all types of solutions I could find, minus the outdated codes, on this topic of getting a UIWebView link to pop open Safari and load it there.

So far I can get the specific size to load in simulator, but every time I click it, it loads right there. I have to be missing a major step or I have the AppDelegate .h .m and ViewController .h .m completely messed up.

I was big into coding for devices up to 3rd Gen iPod/iPhones. I know that Xcode likes to update a lot and I have the 5.0.2 version. I am basically a No0b again, since I have been out of the game for some time.

Please let me know if you have any tips. Besides to give it up. lol. I know it can be done. Here is what I have...

#import "WIAppDelegate.h"

@implementation WIAppDelegate

- (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
   navigationType:(UIWebViewNavigationType)navigationType {
    // This practically disables web navigation from the webView.
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return FALSE;
    }
    return TRUE;
}


#import <UIKit/UIKit.h>

@interface WIViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIWebView *webview;

@end

#import "WIViewController.h"

@interface WIViewController ()

@end

@implementation WIViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *fullURL = @"http://THESITE.com";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [_webview loadRequest:requestObj];
}

@end
Unheilig
  • 16,196
  • 193
  • 68
  • 98

1 Answers1

0

You need to implement the webview:shouldStartLoadWithRequest:navigationType: method on the class that acts as the UIWebViewDelegate

This should most likely live in your WIViewController class

@implementation WIViewController

- (void)viewDidLoad
{
  [super viewDidLoad];

  NSString *fullURL = @"http://THESITE.com";
  NSURL *url = [NSURL URLWithString:fullURL];
  NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  [_webview loadRequest:requestObj];
  _webview.delegate = self;
}

- (BOOL)webview:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
  if (UIWebViewNavigationTypeLinkClicked == navigationType) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return NO;
  }
  return YES;
}

@end

You will also need to ensure that you actually set this class as the UIWebViewDelegate I've down this as the last line of the viewDidLoad but you could hook this up in the xib if you prefer

Paul.s
  • 38,494
  • 5
  • 70
  • 88