Skip to content

Commit 4383280

Browse files
committed
iOs Sharesheet
1 parent 2be7b31 commit 4383280

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed

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)