Skip to content

Commit

Permalink
bugfix: 13 ShareSheet do not open on iPad when openWithDefault: false
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaa committed Oct 28, 2022
1 parent 0be0dec commit 7a192d8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ Method to open a file.

file open method options

| Prop | Type | Description | Since |
| --------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`filePath`** | <code>string</code> | file path | 1.0.0 |
| **`contentType`** | <code>string</code> | MIME type (optional) | 1.0.0 |
| **`openWithDefault`** | <code>boolean</code> | Use the default platform chooser, if true, otherwise: On Android: it will show "Open File in.." title of the chooser dialog, the system will always present the chooser dialog even if the user has chosen a default one and if no activity is found to handle the file, the system will still present a dialog with the specified title and an error message No application can perform this action On iOS: it will presents a menu restricted to a list of apps capable of opening the current document. This determination is made based on the document type and on the document types supported by the installed apps. To support one or more document types, an app must register those types in its Info.plist file using the CFBundleDocumentTypes key. (optional) default value is true | 1.0.0 |
| Prop | Type | Description | Since |
| --------------------- | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----- |
| **`filePath`** | <code>string</code> | file path | 1.0.0 |
| **`contentType`** | <code>string</code> | MIME type (optional) | 1.0.0 |
| **`openWithDefault`** | <code>boolean</code> | Use the default platform chooser, if true, otherwise: On Android: it will show "Open File in.." title of the chooser dialog, the system will always present the chooser dialog even if the user has chosen a default one and if no activity is found to handle the file, the system will still present a dialog with the specified title and an error message No application can perform this action On iOS: it will presents a menu restricted to a list of apps capable of opening the current document. This determination is made based on the document type and on the document types supported by the installed apps. To support one or more document types, an app must register those types in its Info.plist file using the CFBundleDocumentTypes key. (optional) default value is true | 1.0.0 |
| **`chooserPosition`** | <code>{ x: number; y: number; }</code> | (iOS only) Position to anchor that chooser (ShareSheet) menu in the view (optional) Please note that this is applicable only when the application runs on iPad and when openWithDefault is true otherwise this is ignored | 1.0.3 |

</docgen-api>

Expand Down
25 changes: 22 additions & 3 deletions ios/Plugin/FileOpenerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class FileOpenerPlugin: CAPPlugin, UIDocumentInteractionControllerDelegat
return
}


self.documentInteractionController = UIDocumentInteractionController.init(url: fileURL)
self.documentInteractionController.uti = uti
self.documentInteractionController.delegate = self
Expand All @@ -71,8 +72,26 @@ public class FileOpenerPlugin: CAPPlugin, UIDocumentInteractionControllerDelegat
call.reject("Internal error. View not found!", "1")
return
}
let rect = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height);
wasOpened = self.documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true)
if UIDevice.current.userInterfaceIdiom == .pad {
if
let chooserPosition = call.options["chooserPosition"] as? JSObject,
let x = chooserPosition["x"] as? Float,
let y = chooserPosition["y"] as? Float
{
let rect = CGRect(x: 0, y: 0, width: CGFloat(x), height: CGFloat(y));
wasOpened = self.documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true)
} else {
let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
activityViewController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
activityViewController.popoverPresentationController?.sourceView = view
activityViewController.popoverPresentationController?.sourceRect = CGRect(x: view.frame.midX, y: view.frame.midY, width: 0, height: 0)
self.bridge?.viewController?.present(activityViewController, animated: true, completion: nil)
wasOpened = true
}
} else {
let rect = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height);
wasOpened = self.documentInteractionController.presentOpenInMenu(from: rect, in: view, animated: true)
}
}

if (wasOpened == false) {
Expand All @@ -85,7 +104,7 @@ public class FileOpenerPlugin: CAPPlugin, UIDocumentInteractionControllerDelegat
}
})
}

public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
var presentingViewController = bridge?.viewController;
while (presentingViewController?.presentedViewController != nil && ((presentingViewController?.presentedViewController!.isBeingDismissed) != nil)) {
Expand Down
11 changes: 11 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ export interface FileOpenerOptions {
* @since 1.0.0
*/
openWithDefault?: boolean;
/**
* (iOS only; iPad only) Position to anchor the chooser (ShareSheet) menu in the view (optional)
* Please note that this is applicable only when the application runs on iPad and when
* openWithDefault is false, otherwise this is ignored
*
* @since 1.0.3
*/
chooserPosition?: {
x: number;
y: number;
};
}

export interface FileOpenerPlugin {
Expand Down

0 comments on commit 7a192d8

Please sign in to comment.