Zilant
Zilant

Reputation: 57

Xcode WebView. How to add close/done button when pdf file is opened

I have a small app for iOS that uses WebView.

When I open a PDF file from WebView, I can't close the window or go back. How to add the button back or close, when the PDF file is opened.

URL to PDF file | PDF file is opened

#import "HomeController.h"

@interface HomeController ()

@end

@implementation HomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSURL *myURL = [NSURL URLWithString:@"http://test.mysite.com"];
     NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
     [myWebView loadRequest:myRequest];
}

@end

EDITED

    //I create global BOOL isPdf in header file

    @property (nonatomic, assign) BOOL isPdf;

    //I make request in view did load

myWebView.delegate=self;

    NSURL *myURL = [NSURL URLWithString:@"http://test.mysite.com"];
_isPdf = [myURL.lastPathComponent isEqualToString:@".pdf"];
 NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
 [myWebView loadRequest:myRequest];

 NSLog(@"Run WebView with the URL");

    //And then in my webview delegate method

    - (void)webViewDidFinishLoad:(UIWebView *)webView{
        if (_isPdf) {
            UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(backButtonPressed)];
            self.navigationItem.rightBarButtonItem=backButton;
            NSLog(@"Show back button %i", _isPdf);
        }else{
            self.navigationItem.rightBarButtonItem=nil;
            NSLog(@"Not show back button %i", _isPdf);
        }
    }

// Add backButtonPressed method

-(void)backButtonPressed{
    //update the method according to your need
    if (myWebView.canGoBack) {
        [myWebView goBack];
    }else{
        [self.navigationController popViewControllerAnimated:true];
    }
}

Upvotes: 2

Views: 2402

Answers (1)

Kuldeep Tanwar
Kuldeep Tanwar

Reputation: 3526

You need to do three things

  1. Confirm the webview delegates
  2. Implement webview delegate webviewDidFinishLoad methods
  3. Create a methods for the button action

First confirm the webview delegate in view did load

webview.delegate=self //(don't forgot to add the webview protocol <UIWebviewDelegate>)

Second implement webview delegate

-(void)webViewDidFinishLoad:(UIWebView *)webView{
  if (webView.canGoBack) {
      UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(backButtonPressed)];
      self.navigationItem.rightBarButtonItem=backButton;
  }else{
      self.navigationItem.rightBarButtonItem=nil;
  }
}

Third add backButtonPressed method

-(void)backButtonPressed{
   //update the method according to your need
   if (webview.canGoBack) {
       [webview goBack]
   }else{
       [self.navigationController popViewControllerAnimated:true];
   }
}

Edit

if you want to show button only on pdf then when you make request like your doing in view did load create a global BOOL isPdf and set it when you load request like

 NSURL *myURL = [NSURL URLWithString:@"http://test.mysite.com"];
 isPdf=[myURL.lastPathComponent isEqualToString:@".pdf"];

and then in your webview delegate method :-

-(void)webViewDidFinishLoad:(UIWebView *)webView{
  if (isPdf) {
      UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(backButtonPressed)];
      self.navigationItem.rightBarButtonItem=backButton;
  }else{
      self.navigationItem.rightBarButtonItem=nil;
  }
}

Upvotes: 1

Related Questions