2

在初始加载后,我正在尝试在 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
4

1 回答 1

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;
        }
    }
于 2013-01-23T17:07:21.027 回答