Skip to content

Commit

Permalink
iOs ShareSheet
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayaurora committed Jul 5, 2022
1 parent 2be7b31 commit 5beb62c
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
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 5beb62c

Please sign in to comment.