It is probably a bad idea to pass in alloc().init() into an objc method in the argument directly. objc_util does not retain arguments by default, so what happens is the ObjCInstance is created, a pointer to the class is sent to the objc side of the world, then the object is promptly destroyed in python as it falls out of scope. It sort of depends whether the reference counter gets incremented on the objc side, but basically it is a good idea to separately create a variable and pass it in.

So, just use mydelegate=BrowserDelegate.alloc().init(), and pass that into your other methods.

aBr.setDelegate_(mydelegate)

I could not get your code to crash after fixing this, but I also don't think I have a peer around to trigger the callback. There could be other issuex. If you have pythonista 3, you should install dgelessus's pythonista_startup's enable_fault_handler to tell you where the crash is occuring.

If you get this working, please do report back as others may find this useful.

@JonB I'm no Objective-C expert, but shouldn't the result of XYSomeClass.alloc().init() start out retained? I thought the Objective-C convention was that objects returned from alloc..., copy..., mutableCopy... or new... methods don't need to be retained by hand.

Also objc_util.ObjCInstance automatically retains and releases the underlying object in the Python __new__ and __del__ methods, so as long as the ObjCInstance is alive in Python, the Objective-C instance is too. And Python functions keep their arguments alive for the duration of the function call (except in some VERY specially written function code), so an ObjCInstance should never be garbage-collected during a method call.

By the way, if you only want to install the faulthandler and Objective-C exception handler, you can copy the code for that from this Gist. The rest of my pythonista_startup code can be found in the main repo.

Now it's saying that it can't find the method browser:foundPeer:withDiscoveryInfo: on your delegate. It looks like you mistyped the name in your code (you wrote withDiscoverInfo, it should be withDiscoveryInfo, note the y in Discovery).

Disclaimer... I don't have anything to test out, you need o find a good end to end example written in ObjC or swift to port over... some of these frameworks are tricky to work with just from the docs alone.

Can you paste the actual traceback (if the debugger pops up, click the button in the corner to expand it, then tap Print Traceback). I seem to remember encountering a similar problem in the past. This comes from ctypes, but we need to know which argument is causing problepms.

The error comes from ctypes callback.c. I suspect the issue is the ObjCBlock, the c code cannot convert the pointer it gets.
I know I encountered and solved this elsewhere, but can't find the solution, you might try