I was trying to make a share sheet action with the code below but failed.
# coding: utf-8
from objc_util import *
import appex
SFSafariViewController = ObjCClass('SFSafariViewController')
UIApplication = ObjCClass('UIApplication')
def open_in_safari_vc(url):
vc = SFSafariViewController.alloc().initWithURL_entersReaderIfAvailable_(nsurl(url), True)
root_vc = UIApplication.sharedApplication().keyWindow().rootViewController()
while root_vc.presentedViewController():
root_vc = root_vc.presentedViewController()
root_vc.presentViewController_animated_completion_(vc, True, None)
vc.release()
def main():
if not appex.is_running_extension():
print 'This script is intended to be run from the sharing extension.'
return
url = appex.get_url()
if not url:
print 'No input url'
return
open_in_safari_vc(url)
if __name__ == '__main__':
main()
It returned:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/xxx/Documents/QuickViewer.py", line 32, in
main()
File "/private/var/mobile/Containers/Shared/AppGroup/xxx/Documents/QuickViewer.py", line 29, in main
open_in_safari_vc(url)
File "/private/var/mobile/Containers/Shared/AppGroup/xxx/Documents/QuickViewer.py", line 14, in open_in_safari_vc
root_vc = UIApplication.sharedApplication().keyWindow().rootViewController()
AttributeError: 'NoneType' object has no attribute 'rootViewController'