Chr*_*way 8 pdf ios uidocumentinteraction swift
我正在尝试使用UIDocumentInteractionController presentPreviewAnimated方法在iOS设备上显示pdf,但它会一直显示一个空白文档.我认为它可能与字符编码有关,但我不确定.如果我使用UIWebView,我可以显示pdf,而不是文档交互控制器.
//更新9/18/14现在正在使用Xcode 6的GM版本.
//更新2014年8月22日
奇怪的是,从DocumentInteractionController,如果我点击右上角的"打开方式"图标并选择类似iBooks的内容,则pdf会正确显示.似乎只是预览不想在屏幕上显示它.
这是我的代码(在Swift中):
// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIDocumentInteractionController(data: NSData) {
let fileName = NSTemporaryDirectory().stringByAppendingPathComponent("myFile.pdf")
let url: NSURL! = NSURL(fileURLWithPath: fileName)
// this does not seem to make a difference
// let pdfString = NSString(data: data, encoding: NSISOLatin1StringEncoding)
// pdfString.writeToURL(url!, atomically: true, encoding: NSISOLatin1StringEncoding, error: nil)
data.writeToURL(url, atomically: true)
if url != nil {
let docController = UIDocumentInteractionController(URL: url)
docController.UTI = "com.adobe.pdf"
docController.delegate = self
docController.presentPreviewAnimated(true)
}
}
Run Code Online (Sandbox Code Playgroud)
此代码确实正确显示了pdf:
// data is coming in as NSISOLatin1StringEncoding
func displayPdfInUIWebView(data: NSData) {
let rect = UIScreen.mainScreen().bounds
let screenSize = rect.size
let webView = UIWebView(frame: CGRectMake(0,0,screenSize.width,screenSize.height))
webView.autoresizesSubviews = true
webView.autoresizingMask = (UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth)
webView.loadData(data, MIMETYype: "application/pdf", textEncodingName: "ISO-8859-1", baseUrl: nil)
self.view.addSubview(webView)
}
Run Code Online (Sandbox Code Playgroud)
有没有理由第一个功能不起作用?它不会出错,只显示一个空白页面.