Skip to content

Commit 38a6d8e

Browse files
committed
iOs Share Sheet, share_text and share_ios
1 parent 2be7b31 commit 38a6d8e

File tree

4 files changed

+154
-2
lines changed

4 files changed

+154
-2
lines changed

plyer/__init__.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'brightness', 'call', 'camera', 'compass', 'cpu', 'email', 'filechooser',
1010
'flash', 'gps', 'gravity', 'gyroscope', 'humidity', 'irblaster',
1111
'keystore', 'light', 'notification', 'orientation', 'processors',
12-
'proximity', 'screenshot', 'sms', 'spatialorientation', 'storagepath',
12+
'proximity', 'screenshot', 'sms', 'share', 'spatialorientation', 'storagepath',
1313
'stt', 'temperature', 'tts', 'uniqueid', 'vibrator', 'wifi', 'devicename'
1414
)
1515

@@ -76,6 +76,9 @@
7676
#: Sms proxy to :class:`plyer.facades.Sms`
7777
sms = Proxy('sms', facades.Sms)
7878

79+
#: Share proxy to :class:`plyer.facades.Share`
80+
sms = Proxy('share', facades.Share)
81+
7982
#: Speech proxy to :class:`plyer.facades.STT`
8083
stt = Proxy('stt', facades.STT)
8184

plyer/facades/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
__all__ = ('Accelerometer', 'Audio', 'Barometer', 'Battery', 'Call', 'Camera',
1010
'Compass', 'Email', 'FileChooser', 'GPS', 'Gravity', 'Gyroscope',
1111
'IrBlaster', 'Light', 'Orientation', 'Notification', 'Proximity',
12-
'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash', 'CPU',
12+
'Sms', 'Share', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash', 'CPU',
1313
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness',
1414
'Processors', 'StoragePath', 'Keystore', 'Bluetooth', 'Screenshot',
1515
'STT', 'DeviceName')
@@ -33,6 +33,7 @@
3333
from plyer.facades.orientation import Orientation
3434
from plyer.facades.notification import Notification
3535
from plyer.facades.sms import Sms
36+
from plyer.facades.share import Share
3637
from plyer.facades.stt import STT
3738
from plyer.facades.tts import TTS
3839
from plyer.facades.uniqueid import UniqueID

plyer/facades/share.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# coding=utf-8
2+
'''
3+
Share
4+
=====
5+
6+
The :class:`Share` provides access to public methods to share a file or text or ...
7+
8+
.. note::
9+
In android you need external storage permissions.
10+
11+
.. versionadded:: 2.1.0
12+
13+
This can be used to activate the sharesheet on supported OS.
14+
15+
Simple Examples
16+
---------------
17+
18+
Share text::
19+
20+
>>> from plyer import share
21+
>>> share.share_text(text, title)
22+
23+
Share file::
24+
25+
>>> share.share_file(data, filename, title)
26+
27+
Supported Platforms
28+
-------------------
29+
Android, iOS
30+
31+
'''
32+
from typing import Tuple
33+
34+
35+
class Share:
36+
"""
37+
Share facade.
38+
"""
39+
40+
def share_text(self, text: str, title: str,
41+
size: Tuple[int, int]=(32, 32),
42+
pos:Tuple[int, int]=(200, 200),
43+
arrow_direction:int=0):
44+
"""
45+
Share Sheet for text
46+
"""
47+
self._share_text(text, title)
48+
49+
def share_file(
50+
self, data: str, filename: str, title: str,
51+
size: Tuple[int, int]=(1, 1),
52+
pos:Tuple[int, int]=(0, 0),
53+
arrow_direction:int=0):
54+
"""
55+
Share a file
56+
"""
57+
self._share_file(data, filename, title, size, pos, arrow_direction)
58+
59+
# private
60+
61+
def _share_text(self, text:str,
62+
size: Tuple[int, int]=(32, 32),
63+
pos:Tuple[int, int]=(200, 200),
64+
arrow_direction:int=0):
65+
raise NotImplementedError()
66+
67+
def _share_file(self, data: str, filename: str, titile: str,
68+
size: Tuple[int, int]=(32, 32),
69+
pos:Tuple[int, int]=(200, 200),
70+
arrow_direction:int=0):
71+
raise NotImplementedError()

plyer/platforms/ios/share.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# coding=utf-8
2+
"""
3+
Share
4+
-----
5+
"""
6+
7+
from plyer.facades import Share
8+
from plyer import storagepath
9+
from pyobjus import autoclass
10+
from pyobjus.objc_py_types import NSSize, NSRect, NSPoint
11+
from typing import Tuple
12+
13+
NSURL = autoclass('NSURL')
14+
UIApplication = autoclass('UIApplication')
15+
UIDevice = autoclass('UIDevice')
16+
currentDevice = UIDevice.currentDevice()
17+
iPhone = currentDevice.userInterfaceIdiom == 0
18+
iPad = currentDevice.userInterfaceIdiom == 1
19+
sharedApplication = UIApplication.sharedApplication()
20+
21+
class IosShare(Share):
22+
23+
def _write_data_to_file(self, data, out_file):
24+
with open(out_file, 'w') as ofile:
25+
ofile.write(data)
26+
27+
28+
def _share_text(self, text: str, title: str,
29+
size: Tuple[int, int]=(32, 32),
30+
pos:Tuple[int, int]=(200, 200),
31+
arrow_direction:int=0):
32+
self._share_file(text, None, title,
33+
size=size, pos=pos, arrow_direction=arrow_direction)
34+
35+
def _share_file(
36+
self, data: str, filename: str, title: str,
37+
size: Tuple[int, int]=(32, 32),
38+
pos:Tuple[int, int]=(200, 200),
39+
arrow_direction:int=0):
40+
41+
if not data:
42+
return
43+
44+
if filename:
45+
out_file = storagepath.get_home_dir() + '/Documents/' + filename
46+
self._write_data_to_file(data, out_file)
47+
URL = NSURL.fileURLWithPath_(out_file)
48+
data = URL
49+
50+
import gc
51+
gc.collect()
52+
53+
UIActivityViewController = autoclass('UIActivityViewController')
54+
UIActivityViewController_instance = UIActivityViewController.alloc().init()
55+
activityViewController = UIActivityViewController_instance.initWithActivityItems_applicationActivities_([data], None)
56+
UIcontroller = sharedApplication.keyWindow.rootViewController()
57+
58+
59+
if iPad:
60+
UIView = UIcontroller.view()
61+
UIActivityViewController_instance.modalPresentationStyle = 9# 9 is popover
62+
UIActivityViewController_instance.preferredContentSize = NSSize(0,0)
63+
pc = UIActivityViewController_instance.popoverPresentationController()
64+
pc.permittedArrowDirections = arrow_direction # 0 means no direction
65+
pc.sourceView = UIView
66+
val = NSRect()
67+
val.size = NSSize(*size)# Apsect ration?
68+
val.origin = NSPoint(*pos)
69+
pc.sourceRect = val
70+
71+
UIcontroller.presentViewController_animated_completion_(activityViewController, True, None)
72+
gc.collect()
73+
74+
75+
76+
def instance():
77+
return IosShare()

0 commit comments

Comments
 (0)