iOS 11 支持 pdf 查看。
在 GitHub 上面找到一个比较好的示例,可惜没有做成动态库,那就我来做好了。
https://github.com/kishikawakatsumi/BookReader
BookReader
Sample code for PDFKit on iOS 11, clone of iBooks.app built on top of PDFKit.
CocoaPods
https://guides.cocoapods.org/making/using-pod-lib-create.html
注册 pod trunk register
$ pod trunk register iosdevlog@iosdevlog.com 'iosdevlog' --description='iMac'
[!] Please verify the session by clicking the link in the verification email that has been sent to iosdevlog@iosdevlog.com
iosdeiMac:BookReader iosdevlog$ pod trunk me
- Name: iosdevlog
- Email: iosdevlog@iosdevlog.com
- Since: August 23rd, 2017 00:48
- Pods:
- ijkplayer
- Sessions:
- August 23rd, 2017 00:48 - March 15th, 01:11. IP: 119.137.52.190
- July 20th, 03:20 - November 25th, 03:21. IP: 119.137.52.91 Description: iMac
$ pod lib create BookReader
一路 enter
,测试库什么的先放上,以后有时间一再加测试用例上去。
新建一个空的 GitHub 地址https://github.com/iOSDevLog/BookReader。
把 swift
代码放入 BookReader/BookReader/Classes/
目录下,资源文件 *.png
,*.xib
,*.storyboard
放入 BookReader/BookReader/Assets/
ios.deployment_target 改成 iOS 11 ,再加入 PDFKit。添加 swift
版本信息。
#
# Be sure to run `pod lib lint BookReader.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#
Pod::Spec.new do |s|
s.name = 'BookReader'
s.version = '0.1.0'
s.summary = 'Sample code for PDFKit on iOS 11, clone of iBooks.app built on top of PDFKit.'
# This description is used to generate tags and improve search results.
# * Think: What does it do? Why did you write it? What is the focus?
# * Try to keep it short, snappy and to the point.
# * Write the description between the DESC delimiters below.
# * Finally, don't worry about the indent, CocoaPods strips it!
s.description = <<-DESC
https://github.com/kishikawakatsumi/BookReader
Usage
Import Your Own PDFs
The easiest way to import your PDFs is to email your PDF file to your iOS device. Navigate to the email and ensure that the attachment is there. Tap and hold the document attachment icon. This should open a popover on the iPad, or an action sheet on the iPhone, that shows all of the apps that open your document type. BookReader app should show up in the list. Tap BookReader app icon and BookReader app should launch and receive the document from the email.
DESC
s.homepage = 'https://github.com/iOSDevLog/BookReader'
s.screenshots = 'https://raw.githubusercontent.com/kishikawakatsumi/BookReader/master/Resources/01_Reader.png', 'https://raw.githubusercontent.com/kishikawakatsumi/BookReader/master/Resources/02_Reader.png', 'https://raw.githubusercontent.com/kishikawakatsumi/BookReader/master/Resources/03_Thumbnail.png', 'https://raw.githubusercontent.com/kishikawakatsumi/BookReader/master/Resources/04_TOC.png', 'https://raw.githubusercontent.com/kishikawakatsumi/BookReader/master/Resources/05_OpenIn.png', 'https://raw.githubusercontent.com/kishikawakatsumi/BookReader/master/Resources/06_Search.png', 'https://raw.githubusercontent.com/kishikawakatsumi/BookReader/master/Resources/07_Search.png'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'jiaxianhua' => 'iosdevlog@iosdevlog.com' }
s.source = { :git => 'https://github.com/iOSDevLog/BookReader.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/iosdevlog'
s.ios.deployment_target = '11.0'
s.source_files = 'BookReader/Classes/**/*'
s.resource_bundles = {
'BookReader' => ['BookReader/Assets/*.png', 'BookReader/Assets/*.xib', 'BookReader/Assets/*.storyboard']
}
# s.public_header_files = 'Pod/Classes/**/*.h'
s.frameworks = 'UIKit', 'PDFKit'
# s.dependency 'AFNetworking', '~> 2.3'
s.swift_version = '4.1'
end
重新生成项目。
$ cd Example
$ pod install --verbose
"org.cocoapods.BookReader" 是默认生成的 PRODUCT_BUNDLE_IDENTIFIER,可以修改,不过执行 pod install
后又变回来了。
Bundle(identifier: "org.cocoapods.BookReader")
可以定位到引入的 framework,想要访问 storyboard,还需要找到 bundle 位置。�
示例中展示了打开 * documentDirectory* pdf 列表和打开一个本地 pdf 文档。
if let path = Bundle(identifier: "org.cocoapods.BookReader")?.path(forResource: "BookReader", ofType: "bundle") {
let bundle = Bundle(path: path)
let storyboard = UIStoryboard.init(name: "BookReader", bundle: bundle)
switch indexPath.row {
case 0:
let bookshelfViewController: BookshelfViewController! = storyboard.instantiateViewController(withIdentifier: "BookshelfViewController") as! BookshelfViewController
self.show(bookshelfViewController, sender: nil)
break
default:
let bookViewController: BookViewController! = storyboard.instantiateViewController(withIdentifier: "BookViewController") as! BookViewController
let fileManager = FileManager.default
let documentDirectory = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let contents = try! fileManager.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
let documents = contents.compactMap { PDFDocument(url: $0) }
if documents.count > 0 {
let document = documents.last!
bookViewController.pdfDocument = document
}
self.show(bookViewController, sender: nil)
break
}
}
验证
$ pod lib lint
-> BookReader (0.1.0)
- NOTE | xcodebuild: libpng warning: Input PNG is already optimized for iPhone OS. Copying source file to destination...
BookReader passed validation.
………………………………