Skip to content

Commit

Permalink
iOs Share Sheet, share_text and share_ios
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayaurora committed Jul 5, 2022
1 parent 2be7b31 commit 38a6d8e
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 2 deletions.
5 changes: 4 additions & 1 deletion plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'brightness', 'call', 'camera', 'compass', 'cpu', 'email', 'filechooser',
'flash', 'gps', 'gravity', 'gyroscope', 'humidity', 'irblaster',
'keystore', 'light', 'notification', 'orientation', 'processors',
'proximity', 'screenshot', 'sms', 'spatialorientation', 'storagepath',
'proximity', 'screenshot', 'sms', 'share', 'spatialorientation', 'storagepath',
'stt', 'temperature', 'tts', 'uniqueid', 'vibrator', 'wifi', 'devicename'
)

Expand Down Expand Up @@ -76,6 +76,9 @@
#: Sms proxy to :class:`plyer.facades.Sms`
sms = Proxy('sms', facades.Sms)

#: Share proxy to :class:`plyer.facades.Share`
sms = Proxy('share', facades.Share)

#: Speech proxy to :class:`plyer.facades.STT`
stt = Proxy('stt', facades.STT)

Expand Down
3 changes: 2 additions & 1 deletion plyer/facades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
__all__ = ('Accelerometer', 'Audio', 'Barometer', 'Battery', 'Call', 'Camera',
'Compass', 'Email', 'FileChooser', 'GPS', 'Gravity', 'Gyroscope',
'IrBlaster', 'Light', 'Orientation', 'Notification', 'Proximity',
'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash', 'CPU',
'Sms', 'Share', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash', 'CPU',
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness',
'Processors', 'StoragePath', 'Keystore', 'Bluetooth', 'Screenshot',
'STT', 'DeviceName')
Expand All @@ -33,6 +33,7 @@
from plyer.facades.orientation import Orientation
from plyer.facades.notification import Notification
from plyer.facades.sms import Sms
from plyer.facades.share import Share
from plyer.facades.stt import STT
from plyer.facades.tts import TTS
from plyer.facades.uniqueid import UniqueID
Expand Down
71 changes: 71 additions & 0 deletions plyer/facades/share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# coding=utf-8
'''
Share
=====
The :class:`Share` provides access to public methods to share a file or text or ...
.. note::
In android you need external storage permissions.
.. versionadded:: 2.1.0
This can be used to activate the sharesheet on supported OS.
Simple Examples
---------------
Share text::
>>> from plyer import share
>>> share.share_text(text, title)
Share file::
>>> share.share_file(data, filename, title)
Supported Platforms
-------------------
Android, iOS
'''
from typing import Tuple


class Share:
"""
Share facade.
"""

def share_text(self, text: str, title: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
"""
Share Sheet for text
"""
self._share_text(text, title)

def share_file(
self, data: str, filename: str, title: str,
size: Tuple[int, int]=(1, 1),
pos:Tuple[int, int]=(0, 0),
arrow_direction:int=0):
"""
Share a file
"""
self._share_file(data, filename, title, size, pos, arrow_direction)

# private

def _share_text(self, text:str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
raise NotImplementedError()

def _share_file(self, data: str, filename: str, titile: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
raise NotImplementedError()
77 changes: 77 additions & 0 deletions plyer/platforms/ios/share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# coding=utf-8
"""
Share
-----
"""

from plyer.facades import Share
from plyer import storagepath
from pyobjus import autoclass
from pyobjus.objc_py_types import NSSize, NSRect, NSPoint
from typing import Tuple

NSURL = autoclass('NSURL')
UIApplication = autoclass('UIApplication')
UIDevice = autoclass('UIDevice')
currentDevice = UIDevice.currentDevice()
iPhone = currentDevice.userInterfaceIdiom == 0
iPad = currentDevice.userInterfaceIdiom == 1
sharedApplication = UIApplication.sharedApplication()

class IosShare(Share):

def _write_data_to_file(self, data, out_file):
with open(out_file, 'w') as ofile:
ofile.write(data)


def _share_text(self, text: str, title: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):
self._share_file(text, None, title,
size=size, pos=pos, arrow_direction=arrow_direction)

def _share_file(
self, data: str, filename: str, title: str,
size: Tuple[int, int]=(32, 32),
pos:Tuple[int, int]=(200, 200),
arrow_direction:int=0):

if not data:
return

if filename:
out_file = storagepath.get_home_dir() + '/Documents/' + filename
self._write_data_to_file(data, out_file)
URL = NSURL.fileURLWithPath_(out_file)
data = URL

import gc
gc.collect()

UIActivityViewController = autoclass('UIActivityViewController')
UIActivityViewController_instance = UIActivityViewController.alloc().init()
activityViewController = UIActivityViewController_instance.initWithActivityItems_applicationActivities_([data], None)
UIcontroller = sharedApplication.keyWindow.rootViewController()


if iPad:
UIView = UIcontroller.view()
UIActivityViewController_instance.modalPresentationStyle = 9# 9 is popover
UIActivityViewController_instance.preferredContentSize = NSSize(0,0)
pc = UIActivityViewController_instance.popoverPresentationController()
pc.permittedArrowDirections = arrow_direction # 0 means no direction
pc.sourceView = UIView
val = NSRect()
val.size = NSSize(*size)# Apsect ration?
val.origin = NSPoint(*pos)
pc.sourceRect = val

UIcontroller.presentViewController_animated_completion_(activityViewController, True, None)
gc.collect()



def instance():
return IosShare()

0 comments on commit 38a6d8e

Please sign in to comment.