From 647926ee281712a82238d5153522d9a07be5870c Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Tue, 29 Sep 2020 08:18:04 -0700 Subject: [PATCH] fix(ios): Utils.openFile (#8914) closes https://github.com/NativeScript/NativeScript/issues/8913 --- packages/core/utils/index.ios.ts | 2 +- packages/core/utils/native-helper.d.ts | 5 ++- packages/core/utils/native-helper.ios.ts | 53 +++++++++++++----------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/packages/core/utils/index.ios.ts b/packages/core/utils/index.ios.ts index 90bc24dc2..1cce1b2a6 100644 --- a/packages/core/utils/index.ios.ts +++ b/packages/core/utils/index.ios.ts @@ -11,7 +11,7 @@ export function openFile(filePath: string): boolean { let path = iOSNativeHelper.isRealDevice() ? filePath.replace('~', appPath) : filePath; const controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path)); - controller.delegate = new iOSNativeHelper.UIDocumentInteractionControllerDelegateImpl(); + controller.delegate = iOSNativeHelper.createUIDocumentInteractionControllerDelegate(); return controller.presentPreviewAnimated(true); } catch (e) { diff --git a/packages/core/utils/native-helper.d.ts b/packages/core/utils/native-helper.d.ts index 19986a7cf..d402c33a2 100644 --- a/packages/core/utils/native-helper.d.ts +++ b/packages/core/utils/native-helper.d.ts @@ -168,7 +168,10 @@ export namespace iOSNativeHelper { */ export function applyRotateTransform(transform: any /* CATransform3D*/, x: number, y: number, z: number): any; /* CATransform3D*/ - export class UIDocumentInteractionControllerDelegateImpl {} + /** + * Create a UIDocumentInteractionControllerDelegate implementation for use with UIDocumentInteractionController + */ + export function createUIDocumentInteractionControllerDelegate(): any; /** * Checks whether the application is running on real device and not on simulator. diff --git a/packages/core/utils/native-helper.ios.ts b/packages/core/utils/native-helper.ios.ts index 89d24b29b..b757a33b1 100644 --- a/packages/core/utils/native-helper.ios.ts +++ b/packages/core/utils/native-helper.ios.ts @@ -14,7 +14,7 @@ function openFileAtRootModule(filePath: string): boolean { let path = iOSNativeHelper.isRealDevice() ? filePath.replace('~', appPath) : filePath; const controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path)); - controller.delegate = new iOSNativeHelper.UIDocumentInteractionControllerDelegateImpl(); + controller.delegate = iOSNativeHelper.createUIDocumentInteractionControllerDelegate(); return controller.presentPreviewAnimated(true); } catch (e) { @@ -125,30 +125,33 @@ export namespace iOSNativeHelper { } return transform; - } - - @NativeClass - export class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate { - public static ObjCProtocols = [UIDocumentInteractionControllerDelegate]; - - public getViewController(): UIViewController { - const app = UIApplication.sharedApplication; - - return app.keyWindow.rootViewController; - } - - 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; - } - } + } + + export function createUIDocumentInteractionControllerDelegate(): NSObject { + @NativeClass + class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate { + public static ObjCProtocols = [UIDocumentInteractionControllerDelegate]; + + public getViewController(): UIViewController { + const app = UIApplication.sharedApplication; + + return app.keyWindow.rootViewController; + } + + 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; + } + } + return new UIDocumentInteractionControllerDelegateImpl(); + } export function isRealDevice() { try {