在WebView中显示PDF文件不起作用-Titanium

时间:2019-05-23 16:33:18

标签: appcelerator appcelerator-titanium

我们正在使用Ti SDK 8.0.0 GA,并在iOS 12.2设备中运行应用程序。 尝试从文档文件夹加载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

任何建议!

1 个答案:

答案 0 :(得分:0)

基于此Appc JIRA票证 https://jira.appcelerator.org/browse/TIMOB-25680

我们将PDF移到了tmp文件夹,并尝试在网络视图中打开该tmp文件夹PDF文件,效果很好。

上面的JIRA链接中也有示例代码。在此处复制相同的代码。

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;
}