打开Web视图时如何检查特定URL

时间:2014-05-17 09:07:31

标签: ios

我有一个webview,其中我正在使用:

打开网址
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:abs.com"]]];

现在在webview上打开这个网址时,可以选择播放视频,我想在点击你的网址时在导航栏中添加一个后退按钮。

我该怎么做。

这是我的全部代码:

-(void)loadwebview
{
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http:abc.com"]]];
  //  if([NSURL URLWithString:@"http://www.youtube.com/watch?v=47pwcfo9E_s"])
        //  if(youtubeurl)
      NSURL* url1;
    if ([url1.absoluteString isEqualToString:@"http://www.youtube.com/watch?c"])
    {
        NSLog(@"hello222");
    }


}

- (void)webViewDidStartLoad:(UIWebView *)webView {

    [self loadwebview];

          }



- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    // Here you can check the URL
    NSURL *url = [request URL];
    if ([url.absoluteString isEqualToString:@"http://www.youtube.com/watch?c"]) {
        NSLog(@"string");
        // Do something
        return NO;
    }
    return YES;
}

2 个答案:

答案 0 :(得分:1)

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    // Here you can check the URL
    NSURL *url = [request URL];
    if ([url.absoluteString isEqualToString:@"url"]) {
        // Do something
        return NO;
    }
    return YES;
}

答案 1 :(得分:0)

你想要一个按钮,回到浏览器中?

因此,您需要在视图中添加UITabBar,如下所示:

enter image description here

现在我把一些图像放在下面的按钮上等等。

这是header文件:

@interface WebViewController : UIViewController <UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *btBack;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *btNext;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *btRefresh;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *btStop;

- (IBAction)goBack:(id)sender;
- (IBAction)goNext:(id)sender;
- (IBAction)refresh:(id)sender;
- (IBAction)stop:(id)sender;

@end

和实施:

#pragma mark - UIWebViewDelegate

-(void)webViewDidStartLoad:(UIWebView *)webView {
    [_btRefresh setEnabled:NO];
    [_btStop setEnabled:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_btRefresh setEnabled:YES];
    [_btStop setEnabled:NO];

    [_btBack setEnabled:[webView canGoBack]];
    [_btNext setEnabled:[webView canGoForward]];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [_btRefresh setEnabled:YES];
    [_btStop setEnabled:NO];
}

- (IBAction)goBack:(id)sender {
    [_webView goBack];
}

- (IBAction)goNext:(id)sender {
    [_webView goForward];
}

- (IBAction)refresh:(id)sender {
    [_webView reload];
}

- (IBAction)stop:(id)sender {
    [_webView stopLoading];
}

显然,打开viewController时会禁用按钮。

如果您只对后退感兴趣,请选择。否则,这是一个完整的实现。