【问题标题】:Displaying PDF file in WebView is not working - Titanium在 WebView 中显示 PDF 文件不起作用 - Titanium
【发布时间】:2019-05-23 16:33:17
【问题描述】:

我们正在使用 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

任何建议!

【问题讨论】:

  • 移动到 tmp 文件夹并在 WebView 中打开它。但在 SDK 或 iOS 中似乎有问题。

标签: appcelerator appcelerator-titanium


【解决方案1】:

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

我们将 PDF 移至 tmp 文件夹并尝试在 webview 中打开该 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;
}

【讨论】:

  • 我的文件无法使用 Document viewer 尝试打开 temp 和 data 目录。
猜你喜欢
  • 1970-01-01
  • 2017-11-26
  • 2018-01-31
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多