diff --git a/utils/utils.d.ts b/utils/utils.d.ts index 058df7c06..adbbfa2aa 100644 --- a/utils/utils.d.ts +++ b/utils/utils.d.ts @@ -188,6 +188,12 @@ * Gets the iOS device major version (for 8.1 will return 8). */ export var MajorVersion: number; + + /** + * Opens file with associated application. + * @param filePath The file path. + */ + export function openFile(filePath: string): boolean } /** * An utility function that copies properties from source object to target object. diff --git a/utils/utils.ios.ts b/utils/utils.ios.ts index 994e8d3bd..4adee8f78 100644 --- a/utils/utils.ios.ts +++ b/utils/utils.ios.ts @@ -3,6 +3,7 @@ import common = require("./utils-common"); import colorModule = require("color"); import enums = require("ui/enums"); import * as typesModule from "utils/types"; +import * as fsModule from "file-system"; global.moduleMerge(common, exports); @@ -200,6 +201,21 @@ export module ios { } export var MajorVersion = NSString.stringWithString(UIDevice.currentDevice().systemVersion).intValue; + + export function openFile(filePath: string): boolean { + try { + var fs: typeof fsModule = require("file-system"); + var path = filePath.replace("~", fs.knownFolders.currentApp().path) + + var controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path)); + controller.delegate = new UIDocumentInteractionControllerDelegateImpl(); + return controller.presentPreviewAnimated(true); + } + catch (e) { + console.error("Error in openFile", e); + } + return false; + } } export function GC() { @@ -219,3 +235,24 @@ export function openUrl(location: string): boolean { } return false; } + +class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate { + public static ObjCProtocols = [UIDocumentInteractionControllerDelegate]; + + public getViewController() : UIViewController { + var frame = require("ui/frame"); + return frame.topmost().currentPage.ios; + } + + public documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) { + return this.getViewController(); + } + + public documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController) { + return this.getViewController().view; + } + + public documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController): CGRect { + return this.getViewController().view.frame; + } +} \ No newline at end of file