【问题标题】:Open YouTube URL in Safari instead of UIWebView在 Safari 中打开 YouTube URL 而不是 UIWebView
【发布时间】:2013-01-07 06:09:39
【问题描述】:

我正在尝试在 Safari 中加载初始加载后的任何 URL。对于任何其他网页,此代码都可以正常工作:

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

【问题讨论】:

    标签: iphone ios xcode youtube


    【解决方案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 中打开的某些 URL,例如任何以 http://www.example.com/share 开头的 URL,如下所示:

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

    【讨论】:

    • 这完全一样——点击链接后,除了 YouTube 之外的所有网站都会在 Safari 中打开。
    • 您是说 YouTube 链接会在 YouTube 应用程序内部加载吗?还是在您的应用内的 webView 内?
    • 它在 webView 中加载。
    • 您确定在链接实际出现的视图的视图控制器中有上面的代码吗?而且...您有UIWebViewNavigationTypeLinkClicked == navigationType,而应该将其翻转为navigationType == UIWebViewNavigationTypeLinkClicked
    • 没有看到更多你的代码,我真的帮不上忙——我展示的应该可以完美运行,就像在我的应用程序中一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    相关资源
    最近更新 更多