Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/delegate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
This example simplifies the code from the URL Loading System Programming Guide
(http://goo.gl/JJ2Q8T). It uses NSURLConnection to request an invalid connection
and get the connection:didFailWithError: delegate method triggered.
"""
from kivy.app import App
from kivy.uix.widget import Widget
from pyobjus import autoclass, objc_delegate, objc_str
from pyobjus.dylib_manager import load_framework, INCLUDE

load_framework(INCLUDE.AppKit)
load_framework(INCLUDE.Foundation)

NSURL = autoclass('NSURL')
NSURLConnection = autoclass('NSURLConnection')
NSURLRequest = autoclass('NSURLRequest')


class MyObjcDelegate:
"""A delegate class implemented in Python."""

def connection_didFailWithError_(self, connection, error):
print("Protocol method got called!!", connection, error)


def request_connection():
# This method request connection to an invalid URL so the
# connection_didFailWithError_ protocol method will be triggered.
url = NSURL.URLWithString_(objc_str('abc'))
request = NSURLRequest.requestWithURL_(url)
# Converts the Python delegate object to Objective C delegate instance
# simply by calling the objc_delegate() function.
delegate = objc_delegate(MyObjcDelegate(), ['NSURLConnectionDelegate'])
connection = NSURLConnection.connectionWithRequest_delegate_(request,
delegate)


class DelegateApp(App):
def build(self):
request_connection()
return Widget()

if __name__ == "__main__":
DelegateApp().run()
Loading