在Safari中打开YouTube网址而不是UIWebView

时间:2013-01-23 17:01:13

标签: iphone ios xcode youtube

我正在尝试在初始加载后加载任何URL,在Safari中。使用任何其他网页,此代码工作正常:

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

但是当我转到m.youtube.com时,代码不再执行任何操作,并且YouTube继续在UIWebView中加载。我在网上找不到任何相关信息。有什么帮助吗?

这是我的整个.m文件:

#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

这是视图控制器的.h文件:

#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

1 个答案:

答案 0 :(得分:1)

这是我使用的代码:

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

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

    return YES;
}

这是你的问题:

[[UIApplication sharedApplication] openURL:url];

更改为:

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

您也可以只指定要在Safari中打开的特定网址,例如以http://www.example.com/share开头的任何网址,如下所示:

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