Skip to content

Commit

Permalink
Merge 9487f4b into 0077d36
Browse files Browse the repository at this point in the history
  • Loading branch information
Zen-CODE committed Feb 28, 2020
2 parents 0077d36 + 9487f4b commit 7f5c252
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions plyer/platforms/ios/filechooser.py
@@ -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()

0 comments on commit 7f5c252

Please sign in to comment.