1

I have a webview in which i'm opeing a url using:

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

Now on opening this url on the webview there is some option of playing video,i want to add a back button in the navigation bar when youtube url is clicked.

how can i do this.

this is my whole code:

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

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

but my control is not going on (bool)webView
Your wc dear.. is there any problem?
i have edited my code,plz tell me where i'm going wrong,
on "youtube.com/watch?c" link i want to open a new web view with a back button in the navigation bar and on the click of back button the new view should disappear.
then you should use [_webView goBack]; method on the back button event. suppose user click on the link say youtube then on the back button write [_webView goBack]; it will go back to the back page
|
0

You want a button, to come back in the browser?

So you need to add a UITabBar in your view, like this:

enter image description here

Now i put some images on the buttons back next etc.

This is the header file:

@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

and the implementation:

#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];
}

Obviously buttons are disabled when you open your viewController.

If you are interested just to the back, take just that. Otherwise, this is a complete implementation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.