Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
''' | ||
IOS file chooser | ||
-------------------- | ||
# TODO | ||
.. versionadded:: 1.4.4 | ||
''' | ||
|
||
from plyer.facades import FileChooser | ||
from pyobjus import autoclass, protocol | ||
from pyobjus.dylib_manager import load_framework | ||
|
||
|
||
load_framework('/System/Library/Frameworks/Photos.framework') | ||
|
||
|
||
class IOSFileChooser(FileChooser): | ||
''' | ||
FileChooser implementation for IOS using | ||
the built-in file browser via UIImagePickerController. | ||
.. versionadded:: 1.4.0 | ||
''' | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(IOSFileChooser, self).__init__(*args, **kwargs) | ||
self._on_selection = None | ||
|
||
def _file_selection_dialog(self, *args, **kwargs): | ||
""" | ||
Function called when action is required, A "mode" parameter specifies | ||
which and is one of "open", "save" or "dir". | ||
""" | ||
self._on_selection = kwargs["on_selection"] | ||
if kwargs["mode"] == "open": | ||
self._open() | ||
else: | ||
raise NotImplementedError() | ||
|
||
def _get_picker(self): | ||
""" | ||
Return an instantiated and configured UIImagePickerController. | ||
""" | ||
picker = autoclass("UIImagePickerController") | ||
po = picker.alloc().init() | ||
po.sourceType = 0 | ||
po.delegate = self | ||
return po | ||
|
||
def _open(self): | ||
""" | ||
Launch the native iOS file browser. Upon selection, the | ||
`imagePickerController_didFinishPickingMediaWithInfo_` delegate is | ||
called where we close the file browser and handle the result. | ||
""" | ||
picker = self._get_picker() | ||
UIApplication = autoclass('UIApplication') | ||
vc = UIApplication.sharedApplication().keyWindow.rootViewController() | ||
vc.presentViewController_animated_completion_(picker, True, None) | ||
|
||
@protocol('UIImagePickerControllerDelegate') | ||
def imagePickerController_didFinishPickingMediaWithInfo_( | ||
self, image_picker, frozen_dict): | ||
""" | ||
Delegate which handles the result of the image seletion process. | ||
""" | ||
image_picker.dismissViewControllerAnimated_completion_(True, None) | ||
|
||
# Note: We need to call this Objective C class as there is currently | ||
# no way to call a non-class function via pyobjus. And here, | ||
# we have to use the `UIImagePNGRepresentation` to get the png | ||
# representation. | ||
native_image_picker = autoclass("NativeImagePicker").alloc().init() | ||
path = native_image_picker.writeToPNG_(frozen_dict) | ||
self._on_selection([path.UTF8String()]) | ||
|
||
|
||
def instance(): | ||
return IOSFileChooser() |