gh-114099: Additions to standard library to support iOS (GH-117052)

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Co-authored-by: Malcolm Smith <smith@chaquo.com>
Co-authored-by: Ned Deily <nad@python.org>
This commit is contained in:
Russell Keith-Magee 2024-03-28 15:59:33 +08:00 committed by GitHub
parent b44898299a
commit f006338017
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 474 additions and 48 deletions

View file

@ -478,6 +478,9 @@ def register_standard_browsers():
# OS X can use below Unix support (but we prefer using the OS X
# specific stuff)
if sys.platform == "ios":
register("iosbrowser", None, IOSBrowser(), preferred=True)
if sys.platform == "serenityos":
# SerenityOS webbrowser, simply called "Browser".
register("Browser", None, BackgroundBrowser("Browser"))
@ -599,6 +602,70 @@ if sys.platform == 'darwin':
rc = osapipe.close()
return not rc
#
# Platform support for iOS
#
if sys.platform == "ios":
from _ios_support import objc
if objc:
# If objc exists, we know ctypes is also importable.
from ctypes import c_void_p, c_char_p, c_ulong
class IOSBrowser(BaseBrowser):
def open(self, url, new=0, autoraise=True):
sys.audit("webbrowser.open", url)
# If ctypes isn't available, we can't open a browser
if objc is None:
return False
# All the messages in this call return object references.
objc.objc_msgSend.restype = c_void_p
# This is the equivalent of:
# NSString url_string =
# [NSString stringWithCString:url.encode("utf-8")
# encoding:NSUTF8StringEncoding];
NSString = objc.objc_getClass(b"NSString")
constructor = objc.sel_registerName(b"stringWithCString:encoding:")
objc.objc_msgSend.argtypes = [c_void_p, c_void_p, c_char_p, c_ulong]
url_string = objc.objc_msgSend(
NSString,
constructor,
url.encode("utf-8"),
4, # NSUTF8StringEncoding = 4
)
# Create an NSURL object representing the URL
# This is the equivalent of:
# NSURL *nsurl = [NSURL URLWithString:url];
NSURL = objc.objc_getClass(b"NSURL")
urlWithString_ = objc.sel_registerName(b"URLWithString:")
objc.objc_msgSend.argtypes = [c_void_p, c_void_p, c_void_p]
ns_url = objc.objc_msgSend(NSURL, urlWithString_, url_string)
# Get the shared UIApplication instance
# This code is the equivalent of:
# UIApplication shared_app = [UIApplication sharedApplication]
UIApplication = objc.objc_getClass(b"UIApplication")
sharedApplication = objc.sel_registerName(b"sharedApplication")
objc.objc_msgSend.argtypes = [c_void_p, c_void_p]
shared_app = objc.objc_msgSend(UIApplication, sharedApplication)
# Open the URL on the shared application
# This code is the equivalent of:
# [shared_app openURL:ns_url
# options:NIL
# completionHandler:NIL];
openURL_ = objc.sel_registerName(b"openURL:options:completionHandler:")
objc.objc_msgSend.argtypes = [
c_void_p, c_void_p, c_void_p, c_void_p, c_void_p
]
# Method returns void
objc.objc_msgSend.restype = None
objc.objc_msgSend(shared_app, openURL_, ns_url, None, None)
return True
def main():
import getopt