Skip to content

Commit 97d00cd

Browse files
author
Vladimir Enchev
committed
Merge pull request NativeScript#1479 from NativeScript/openFile-for-ios
openFile add for iOS
2 parents 0d4d0b0 + 3bc1aa0 commit 97d00cd

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

utils/utils.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@
188188
* Gets the iOS device major version (for 8.1 will return 8).
189189
*/
190190
export var MajorVersion: number;
191+
192+
/**
193+
* Opens file with associated application.
194+
* @param filePath The file path.
195+
*/
196+
export function openFile(filePath: string): boolean
191197
}
192198
/**
193199
* An utility function that copies properties from source object to target object.

utils/utils.ios.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import common = require("./utils-common");
33
import colorModule = require("color");
44
import enums = require("ui/enums");
55
import * as typesModule from "utils/types";
6+
import * as fsModule from "file-system";
67

78
global.moduleMerge(common, exports);
89

@@ -200,6 +201,21 @@ export module ios {
200201
}
201202

202203
export var MajorVersion = NSString.stringWithString(UIDevice.currentDevice().systemVersion).intValue;
204+
205+
export function openFile(filePath: string): boolean {
206+
try {
207+
var fs: typeof fsModule = require("file-system");
208+
var path = filePath.replace("~", fs.knownFolders.currentApp().path)
209+
210+
var controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path));
211+
controller.delegate = new UIDocumentInteractionControllerDelegateImpl();
212+
return controller.presentPreviewAnimated(true);
213+
}
214+
catch (e) {
215+
console.error("Error in openFile", e);
216+
}
217+
return false;
218+
}
203219
}
204220

205221
export function GC() {
@@ -219,3 +235,24 @@ export function openUrl(location: string): boolean {
219235
}
220236
return false;
221237
}
238+
239+
class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate {
240+
public static ObjCProtocols = [UIDocumentInteractionControllerDelegate];
241+
242+
public getViewController() : UIViewController {
243+
var frame = require("ui/frame");
244+
return frame.topmost().currentPage.ios;
245+
}
246+
247+
public documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) {
248+
return this.getViewController();
249+
}
250+
251+
public documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController) {
252+
return this.getViewController().view;
253+
}
254+
255+
public documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController): CGRect {
256+
return this.getViewController().view.frame;
257+
}
258+
}

0 commit comments

Comments
 (0)