在 WebView 中显示 PDF 文件不起作用 - Titanium
Displaying PDF file in WebView is not working - Titanium
我们在 iOS 12.2 设备中使用 Ti SDK 8.0.0 GA 和 运行 应用程序。
尝试从文档文件夹加载 PDF。它无法加载。它在模拟器中运行良好。
并且在 Ti SDK 7.5.1 GA 中也能正常工作。
Ti.UI.createWebView({
url : factSheetPath,
scalesPageToFit : true,
backgroundColor : "transparent",
disableBounce : true,
willHandleTouches : true
});
factSheetPath
是这样的
file:///var/mobile/Containers/Data/Application/8F59DFDF-E475-4383-96DC-2DCC5FDDC2DB/Documents/download/pdf_factsheets/1.pdf
任何建议!
基于此 Appc JIRA 票证
https://jira.appcelerator.org/browse/TIMOB-25680
我们将 PDF 移动到 tmp 文件夹并尝试在 webview 中打开那个 tmp 文件夹 PDF 文件,它运行良好。
上面的 JIRA link 中也有示例代码。在此处复制了相同的代码。
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var btn = Ti.UI.createButton({
title: 'Open PDF'
});
btn.addEventListener('click', openPDF);
win.add(btn);
win.open();
// Open the PDF file
function openPDF() {
var fileName = 'example.pdf';
// For iOS 11.2, workaround the Apple issue by creating a temporary file and
// reference it. It will be removed from filesystem once the app closes.
// Read more here: http://nshipster.com/nstemporarydirectory/
if (isiOS11_2()) {
fileName = fileInTemporaryDirectory(fileName);
}
var docViewer = Ti.UI.iOS.createDocumentViewer({
url: fileName
});
docViewer.show();
}
// Check if the current device runs iOS 11.2+
function isiOS11_2() {
var version = Ti.Platform.version.split(".");
return (parseInt(version[0]) >= 11 && parseInt(version[1]) >= 2);
}
// Create a temporary file with the contents of the old file
// Expects the file to be in the resources directory. If you receive the file
// from an API-call, receive pass the Ti.Blob/Ti.File/text to "write" directly.
function fileInTemporaryDirectory(fileName) {
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName);
if (!file.exists()) {
alert('File does not exist in resources!');
return;
}
var newFile = Titanium.Filesystem.getFile(Ti.Filesystem.tempDirectory, fileName);
newFile.createFile();
if (!newFile.exists()) {
alert('New file could not be created in temporary directory!');
return;
}
newFile.write(file);
return newFile.nativePath;
}
我们在 iOS 12.2 设备中使用 Ti SDK 8.0.0 GA 和 运行 应用程序。 尝试从文档文件夹加载 PDF。它无法加载。它在模拟器中运行良好。
并且在 Ti SDK 7.5.1 GA 中也能正常工作。
Ti.UI.createWebView({
url : factSheetPath,
scalesPageToFit : true,
backgroundColor : "transparent",
disableBounce : true,
willHandleTouches : true
});
factSheetPath
是这样的
file:///var/mobile/Containers/Data/Application/8F59DFDF-E475-4383-96DC-2DCC5FDDC2DB/Documents/download/pdf_factsheets/1.pdf
任何建议!
基于此 Appc JIRA 票证 https://jira.appcelerator.org/browse/TIMOB-25680
我们将 PDF 移动到 tmp 文件夹并尝试在 webview 中打开那个 tmp 文件夹 PDF 文件,它运行良好。
上面的 JIRA link 中也有示例代码。在此处复制了相同的代码。
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var btn = Ti.UI.createButton({
title: 'Open PDF'
});
btn.addEventListener('click', openPDF);
win.add(btn);
win.open();
// Open the PDF file
function openPDF() {
var fileName = 'example.pdf';
// For iOS 11.2, workaround the Apple issue by creating a temporary file and
// reference it. It will be removed from filesystem once the app closes.
// Read more here: http://nshipster.com/nstemporarydirectory/
if (isiOS11_2()) {
fileName = fileInTemporaryDirectory(fileName);
}
var docViewer = Ti.UI.iOS.createDocumentViewer({
url: fileName
});
docViewer.show();
}
// Check if the current device runs iOS 11.2+
function isiOS11_2() {
var version = Ti.Platform.version.split(".");
return (parseInt(version[0]) >= 11 && parseInt(version[1]) >= 2);
}
// Create a temporary file with the contents of the old file
// Expects the file to be in the resources directory. If you receive the file
// from an API-call, receive pass the Ti.Blob/Ti.File/text to "write" directly.
function fileInTemporaryDirectory(fileName) {
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName);
if (!file.exists()) {
alert('File does not exist in resources!');
return;
}
var newFile = Titanium.Filesystem.getFile(Ti.Filesystem.tempDirectory, fileName);
newFile.createFile();
if (!newFile.exists()) {
alert('New file could not be created in temporary directory!');
return;
}
newFile.write(file);
return newFile.nativePath;
}