Skip to content
Titanium SDK
Version13.2.0

You are reading 13.2.0. See this page in 13.4.1.

Titanium.UI.iOS.DocumentViewer

proxyiPhoneiPadmacOSsince iPhone 2.1.1, iPad 2.1.1, macOS 9.2.0

Extends Titanium.Proxy

A DocumentViewer provides in-app support for managing user interactions with files on the local system.

The DocumentViewer is created by the Titanium.UI.iOS.createDocumentViewer method.

Use this class to present an user interface for previewing files, such as an e-mail program that previews attachments.

The DocumentViewer displays previews for the following document types:

  • Microsoft Office (DOC, XLS, PPT) (Office 97-2004), the XML versions are not supported
  • Portable Document Format files (PDF)
  • Images (JPEG, GIF, PNG)
  • Rich Text Format files (RTF)
  • HTML and XML files
  • Plain text files
  • Comma-separated value files (CSV)

Note: Using HTML content in the DocumentViewer is not recommended, since it does not support all HTML-capabilities. Please use Titanium.UI.WebView to display complex HTML files.

You can launch the document either in the document viewer or with another application with the options menu. To use the options menu, you need to specify a view with the show method to anchor the options menu to.

In the document viewer, click the Done button to dismiss the document viewer or click the right navigation button to open the options menu to perform another action with the document, such as printing the document or opening the document in another application.

Note for iOS 11 and later When using the DocumentViewer in iOS 11 and later, documents need to be placed in the app-bundle in order to be shared to third-party apps. This is a privacy restriction and can be solved by using either a document from the Resources directory (classic Titanium) or app/assets (Alloy). Alternatively, documents can also be stored in the Titanium.Filesystem.applicationCacheDirectory or Titanium.Filesystem.applicationDataDirectory.

Note for iOS 11.2 and later Apple introduced a regression in iOS 11.2 that forces all files to be in the application data directory. We handle this workaround for you in Titanium SDK 7.0.2+, but you can also work around it without an SDK update. Here is an example:

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 = fileInApplicationDataDirectory(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 fileInApplicationDataDirectory(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.applicationDataDirectory, fileName);
  newFile.createFile();

  if (!newFile.exists()) {
    alert('New file could not be created in application data directory!');
    return;
  }

  newFile.write(file);

  return newFile.nativePath;
}

Read more about the issue in TIMOB-25680.

Examples

Document Viewer Example

In the example below, the navigation bar's right button opens the options menu, while the window button immediately launches the document in the document viewer.

var navButton = Ti.UI.createButton({
    title: 'Launch'
});

var win = Ti.UI.createWindow({
    rightNavButton: navButton
});

var navWin = Ti.UI.createNavigationWindow({
    window: win
});

var winButton = Ti.UI.createButton({
    title: 'Launch',
    height: 40,
    width: 200,
    top: 270
});

win.add(winButton);

// Create a document viewer to preview a PDF file ("Example.pdf")
docViewer = Ti.UI.iOS.createDocumentViewer({
    url: 'Example.pdf'
});

// Opens the options menu and when the user clicks on 'Quick Look'
// the document viewer launches with an animated transition
navButton.addEventListener('click', function() {
    docViewer.show({
        view: navButton,
        animated: true
    });
});

// The document viewer immediately launches without an animation
winButton.addEventListener('click', function(){
    docViewer.show()
});

navWin.open();

Properties 6

annotation

Dictionary | Array | String | Number | Date

Custom property list information for the target file.

Use this property to pass information about the document type to the app responsible for opening it.

apiName

Stringsince Android 3.2.0, iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0read-only

Inherited from Titanium.Proxy

The name of the API that this proxy corresponds to.

bubbleParent

Booleansince Android 3.0.0, iPhone 3.0.0, iPad 3.0.0, macOS 9.2.0

Inherited from Titanium.Proxy

Indicates if the proxy will bubble an event to its parent.

Default: true

name

Stringread-only

Name of the file (without the path).

Only returns a value after the document has been previewed.

title

Stringsince 12.7.0

Title of the file.

Use this property to specify the document title that is displayed in the navigation bar. If not set, it uses the URL path of the loaded document.

url

String

URL of the document being previewed.

Important note: For iOS 11.2 and later, you need to reference this URL from either the application data directory or temporary directory. Read more about this change in the "Note for iOS 11.2 and later" paragraph of the API summary.

Methods 6

Inherited from Titanium.Proxy

Adds the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to invoke when the event is fired.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Inherited from Titanium.Proxy

Fires a synthesized event to any registered listeners.

Parameters
ParametersTypeDescription
nameString

Name of the event.

event?Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Dismisses the document viewer.

Parameters
ParametersTypeDescription
options?DocumentViewerOptions

Animation options.

2 fields
animated Boolean

Indicates whether to animate the transition.

view Titanium.UI.View

Anchors the options menu to the specified view.

Inherited from Titanium.Proxy

Removes the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Displays the document viewer over the current view.

To show the options menu, set the view key of the options object.

Parameters
ParametersTypeDescription
options?DocumentViewerOptions

Display options.

2 fields
animated Boolean

Indicates whether to animate the transition.

view Titanium.UI.View

Anchors the options menu to the specified view.

Events 3

Fires when the document is previewed.

Fires when the document is dismissed.