Xcode 网络视图。打开pdf文件时如何添加close/done按钮

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

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

当我从 WebView 打开 PDF 文件时,无法关闭 window 或返回。如何在打开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

已编辑

    //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. 确认网络视图委托
  2. 实现 webview 委托 webviewDidFinishLoad 方法
  3. 为按钮操作创建一个方法

首先确认view中的webview delegate确实加载了

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

第二次实现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;
  }
}

第三个添加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;
  }
}