Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Beta suggestion: SafariViewController
-
It'd be cool if in the beta, you could replace the built-in web browser with iOS 9's new Safari View Controller. This wouldn't mean supporting fewer devices, because everything that can run iOS 8 can run iOS 9.
I'm not sure what kind of customization is possible with Safari View Controller, if you can change the color scheme, it could even be made to match the current user theme
-
I don't really want to require iOS 9 just yet, but eventually, I'll probably put this into the
webbrowser
module somehow... In the meantime, you can of course use the Safari View Controller in the current beta viaobjc_util
. Here's an example with a custom tint color (there aren't a lot of other customization options, this is pretty much all you can change):from objc_util import * SFSafariViewController = ObjCClass('SFSafariViewController') @on_main_thread def open_in_safari_vc(url, tint_color=None): vc = SFSafariViewController.alloc().initWithURL_(nsurl(url)) if tint_color is not None: vc.view().tintColor = tint_color app = UIApplication.sharedApplication() root_vc = app.keyWindow().rootViewController() root_vc.presentViewController_animated_completion_(vc, True, None) vc.release() if __name__ == '__main__': open_in_safari_vc('http://apple.com', UIColor.orangeColor())
-
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' -
@lucifr what Application did you try running it from? (It shouldn't make a difference but when I used the share sheet in Pythonista it failed but through google chrome it worked)
-
I tried Safari and Tweetbot. Just tried Chrome too. None of them worked.
-
Hmm this is probably better aswered by @omz
I am using an iPad Air 2 with iOS9 and latest pythonista beta. (Don't know if it will make a difference)
What is the output ofUIApplication.sharedApplication()
? -
Not sure the app extension works the same way in terms of UIApplication, etc.
-
@Cethric it can return a valid UIApplication, but not a valid keyWindow.
-
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()