shouldStartLoadWithRequest无法在iOS中运行

时间:2015-09-11 16:42:08

标签: ios uiwebview

我尝试使用以下代码覆盖网址:

- (void)viewDidLoad {
    [super viewDidLoad];
     // Do any additional setup after loading the view.
    NSString *urlString = @"http://www.google.com/";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [_udazzWebView loadRequest:urlRequest];
    NSLog(@"log");
}

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

    NSLog(@"log2");
    //use NSURLRequest object request , to manage the request.
    NSURL *urL=request.URL;
    NSString *urlStr=[urL absoluteString];
    NSLog(@"URLL %@",urlStr);
    if([urlStr isEqualToString:@"PostPicPopUp"]){
        NSLog(@"log3");
    }

    return YES;
}

Log2不会出现在控制台中。我猜这与替换NSURLRequest中的viewDidLoad有关,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

在我看来,你只是缺少UIWebView委托初始化

- (void)initWebViewWithRect:(CGRect)rect {
    self.webView = [[UIWebView alloc] initWithFrame:rect];
    self.webView.backgroundColor = [UIColor whiteColor];
    self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.webView.opaque=NO;
    self.webView.userInteractionEnabled=YES;
    self.webView.delegate=self;
    [self cleanSubViews];
}

您的UIWebView应该定义为

@interface MXMBaseWebView()<UIWebViewDelegate, UIScrollViewDelegate, MXMWebViewProgressDelegate> {
    CAGradientLayer *shadowLayer;
    MXMWebViewProgressView *_progressView;
    MXMWebViewProgress *_progressProxy;
    UIRefreshControl *_refreshControl;
}

@property(nonatomic, strong) UIWebView *webView;
@end

所以你会做像

这样的电话
[self.webView loadRequest: [NSURLRequest requestWithURL:url]];

此时一切都应该正常工作。