Xcode WebView。如何在打开pdf文件时添加关闭/完成按钮

时间:2018-05-23 23:49:01

标签: ios objective-c xcode uiwebview

我有一个使用WebView的iOS小应用程序。

当我从WebView打开PDF文件时,我无法关闭窗口或返回。打开PDF文件时如何添加或关闭按钮。

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

1 个答案:

答案 0 :(得分:1)

你需要做三件事

  1. 确认webview委托
  2. 实施webview委托webviewDidFinishLoad方法
  3. 为按钮操作创建方法
  4. 首先确认视图中的webview委托已加载

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

    第二个实现webview委托

    -(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;
      }
    }
    

    第三个添加backButtonPressed方法

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

    修改

    如果你只想在pdf上显示按钮,那么当你在视图中做出请求时,加载创建一个全局BOOL isPdf并在加载请求时设置它,如

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

    然后在你的webview委托方法中: -

    -(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;
      }
    }