• lucifr

    Modified the code and it finally worked.

    # coding: utf-8
    
    from objc_util import *
    import appex
    
    SFSafariViewController = ObjCClass('SFSafariViewController')
    
    def open_in_safari_vc(url):
        vc = SFSafariViewController.alloc().initWithURL_entersReaderIfAvailable_(nsurl(url), True)
        
        app = UIApplication.sharedApplication()
        
        if app.keyWindow():
            window = app.keyWindow()
        else:
                window = app.windows().firstObject()
        
        root_vc = window.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()
    

    posted in Pythonista read more
  • lucifr

    @Cethric it can return a valid UIApplication, but not a valid keyWindow.

    posted in Pythonista read more
  • lucifr

    I tried Safari and Tweetbot. Just tried Chrome too. None of them worked.

    posted in Pythonista read more
  • lucifr

    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 <module>
    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'

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!