Stickerbox
Stickerbox

Reputation: 141

Open YouTube URL in Safari instead of UIWebView

I'm trying to load any URL, after the initial load, in Safari. With any other webpage, this code works fine:

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

    return YES;
}

But as soon as I head over to m.youtube.com, the code no longer does anything, and YouTube continues to load inside the UIWebView. I can't find anything about this online. Any help?

Here is my entire .m file:

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize topLayer = _topLayer;
@synthesize layerPosition = _layerPosition;
@synthesize webView = webView;

#define VIEW_HIDDEN 260

- (void)viewDidLoad
{
    [super viewDidLoad];

    webView.delegate = self;

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.youtube.com"]]];
    [webView setBackgroundColor:[UIColor clearColor]];
    [webView setOpaque:NO];
    webView.scrollView.bounces = NO;
    webView.scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;


    [self performSelector:@selector(makeTick)];

    self->promoteImages.contentSize = CGSizeMake(1920, 101);

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Scroll3.png"]];

    [self->promoteImages addSubview:imageView];

    [promoteImages setShowsHorizontalScrollIndicator:NO];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {

        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }

    return YES;
}

@end

And here's my .h file for the View Controller:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import <Social/Social.h>

@interface SecondViewController : UIViewController <UIWebViewDelegate>
{
    IBOutlet UIScrollView *promoteImages;
    NSTimer *time;

    IBOutlet UIWebView *webView;
}

@property (nonatomic, nonatomic) UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIView *topLayer;
@property (nonatomic) CGFloat layerPosition;
@property(nonatomic,readonly,getter=isDragging)    BOOL dragging;

@end

Upvotes: 2

Views: 1138

Answers (1)

adamdehaven
adamdehaven

Reputation: 5920

Here's the code that I use:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {

        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }

    return YES;
}

Here's your problem:

[[UIApplication sharedApplication] openURL:url];

Change to:

[[UIApplication sharedApplication] openURL:request.URL];

You can also specify only certain URLs to open in Safari, for example any URLS beginning with http://www.example.com/share as shown here:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        if ((navigationType == UIWebViewNavigationTypeLinkClicked) && ([currentURL hasPrefix:@"http://www.example.com/share"])) {

            [[UIApplication sharedApplication] openURL:request.URL];
            return NO;
        }
    }

Upvotes: 1

Related Questions