在iphone/ipad 上预览阅读服务器端的 PDF not UIDocumentInteractionController

or this you require the QuickLook Framework and its class called as the QLPreviewController. With the help of this class you can view documents like

a) iWork Documents
b) Microsoft Office Documents
c) RTF documents
d) PDF Files
e) Images
f) Text Files
g) CSV (Comma separated files)

For this tutorial i have just displayed PDF files, the file could be present any where in the server or in the app bundle. But since in maximum of the application the PDF files are loaded from the server i shall do the same in this demo.

Step 1: Open Xcode and create a view which looks like the one given below

Step 2: Add the Quick Look framework into your project and import it into your view controller.h file, create the object of the QLPreviewController in the table views did select row at index method and set its dataSource and delegate methods

  1. -  ( void )tableView : ( UITableView  * )tableView didSelectRowAtIndexPath : ( NSIndexPath  * )indexPath {
  2.  
  3.      //initializing the fileURL object with the URL links to be loaded
  4.      switch  ( indexPath. row )  {
  5.          case  0 :
  6.              fileURL  =  [ NSURL  URLWithString : kStringURLViewControllerPDF ] ;
  7.              break ;
  8.  
  9.        case  1 :
  10.              fileURL  =  [ NSURL URLWithString : kStringURLQLPreviewControllerPDF ] ;
  11.              break ;
  12.              case  2 :
  13.              fileURL  =  [ NSURL URLWithString : kStringURLUIDocumentInteractionControllerPDF ] ;
  14.              break ;
  15.      }
  16.      //creating the object of the QLPreviewController
  17.      QLPreviewController  * previewController  =  [ [ QLPreviewController alloc ]init ] ;
  18.  
  19.      //settnig the datasource property to self
  20.     previewController. dataSource  =  self ;
  21.  
  22.      //pusing the QLPreviewController to the navigation stack
  23.      [ [ self navigationController ] pushViewController : previewControlleranimated : YES ] ;
  24.      [ previewController release ] ;
  25. }

Code Explanation: The maximum coding is very simple and is given in the comments i am initializing the fileURL object which is the object of NSURL class which will be returned in the QLPreviewController datasource later to load the pdf files/

Step 3: Add the QLPreviewController datasource method which will do all the hardwork for you and launch the PDF for you.

  1. #pragma mark QLPreviewControllerDataSource
  2.  
  3. // Returns the number of items that the preview controller should preview
  4. -  ( NSInteger )numberOfPreviewItemsInPreviewController : ( QLPreviewController * )previewController
  5. {
  6.      return  30 ;
  7. }
  8.  
  9. // returns the item that the preview controller should preview
  10. -  ( id )previewController : ( QLPreviewController * )previewController previewItemAtIndex : ( NSInteger )idx
  11. {
  12.      return fileURL ;
  13. }

Code Explanation:

numberOfPreviewItemsInPreviewController: Invoked when the Quick Look preview controller needs to know the number of preview items to include in the preview navigation list. (required)

previewItemAtIndex: Invoked when the Quick Look preview controller needs the preview item for a specified index position. (required)

Step 4: Add the view into the iPhone window with the help of the navigation controller, and then add the navigation controller into the iPhone window to add navigation and in case if you dont want navigation you may skip the adding the navigation controller.

Step 5: Launch the application and view the PDF's

you may also zoom in and out and even print the PDF.

But it is advised to use the UIDocumentInteractionController but its a bit complex so i came up with this easy solution.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值