Merge pull request #1479 from NativeScript/openFile-for-ios

openFile add for iOS
This commit is contained in:
Vladimir Enchev
2016-02-04 10:53:35 +02:00
2 changed files with 43 additions and 0 deletions

6
utils/utils.d.ts vendored
View File

@@ -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.

View File

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