fix(ios): Utils.openFile (#8914)

closes https://github.com/NativeScript/NativeScript/issues/8913
This commit is contained in:
Nathan Walker
2020-09-29 08:18:04 -07:00
committed by GitHub
parent 65b1cdbae0
commit 647926ee28
3 changed files with 33 additions and 27 deletions

View File

@ -11,7 +11,7 @@ export function openFile(filePath: string): boolean {
let path = iOSNativeHelper.isRealDevice() ? filePath.replace('~', appPath) : filePath; let path = iOSNativeHelper.isRealDevice() ? filePath.replace('~', appPath) : filePath;
const controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path)); const controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path));
controller.delegate = <UIDocumentInteractionControllerDelegate>new iOSNativeHelper.UIDocumentInteractionControllerDelegateImpl(); controller.delegate = iOSNativeHelper.createUIDocumentInteractionControllerDelegate();
return controller.presentPreviewAnimated(true); return controller.presentPreviewAnimated(true);
} catch (e) { } catch (e) {

View File

@ -168,7 +168,10 @@ export namespace iOSNativeHelper {
*/ */
export function applyRotateTransform(transform: any /* CATransform3D*/, x: number, y: number, z: number): any; /* CATransform3D*/ 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. * Checks whether the application is running on real device and not on simulator.

View File

@ -14,7 +14,7 @@ function openFileAtRootModule(filePath: string): boolean {
let path = iOSNativeHelper.isRealDevice() ? filePath.replace('~', appPath) : filePath; let path = iOSNativeHelper.isRealDevice() ? filePath.replace('~', appPath) : filePath;
const controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path)); const controller = UIDocumentInteractionController.interactionControllerWithURL(NSURL.fileURLWithPath(path));
controller.delegate = new iOSNativeHelper.UIDocumentInteractionControllerDelegateImpl(); controller.delegate = iOSNativeHelper.createUIDocumentInteractionControllerDelegate();
return controller.presentPreviewAnimated(true); return controller.presentPreviewAnimated(true);
} catch (e) { } catch (e) {
@ -125,30 +125,33 @@ export namespace iOSNativeHelper {
} }
return transform; return transform;
} }
@NativeClass export function createUIDocumentInteractionControllerDelegate(): NSObject {
export class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate { @NativeClass
public static ObjCProtocols = [UIDocumentInteractionControllerDelegate]; class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate {
public static ObjCProtocols = [UIDocumentInteractionControllerDelegate];
public getViewController(): UIViewController { public getViewController(): UIViewController {
const app = UIApplication.sharedApplication; const app = UIApplication.sharedApplication;
return app.keyWindow.rootViewController; return app.keyWindow.rootViewController;
} }
public documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) { public documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) {
return this.getViewController(); return this.getViewController();
} }
public documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController) { public documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController) {
return this.getViewController().view; return this.getViewController().view;
} }
public documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController): CGRect { public documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController): CGRect {
return this.getViewController().view.frame; return this.getViewController().view.frame;
} }
} }
return new UIDocumentInteractionControllerDelegateImpl();
}
export function isRealDevice() { export function isRealDevice() {
try { try {