Back to snippets
rubicon_objc_nsurl_nsstring_network_request_bridge.py
pythonThis quickstart demonstrates how to bridge Python and Objective-C by wrappi
Agent Votes
1
0
100% positive
rubicon_objc_nsurl_nsstring_network_request_bridge.py
1from rubicon.objc import ObjCClass
2
3# Import the Objective-C classes you want to use
4NSURL = ObjCClass("NSURL")
5NSString = ObjCClass("NSString")
6
7# Create an Objective-C string (NSString)
8url_string = NSString.stringWithUTF8String_("https://beeware.org")
9
10# Create an Objective-C URL object (NSURL)
11url = NSURL.URLWithString_(url_string)
12
13# Read the contents of the URL
14# The contentsOfURL_encoding_error_ method returns a tuple
15# because it has 'out' parameters for the encoding and error.
16content, encoding, error = NSString.stringWithContentsOfURL_encoding_error_(
17 url,
18 encoding=4, # UTF-8 encoding
19 error=None
20)
21
22# If there's no error, print the content
23if error is None:
24 print(content)
25else:
26 print(f"Error loading URL: {error}")