I am trying to polish the NSURLSession framework I once created but never finished. Everything fine so far, except for Im unable to build a delegate class. The NSURLSessionDelegate (like the datatask delegates) is retained by the session, so I wanted to build a separate class to avoid memory leaks that could occur if the Session would be its own delegate.
Like usual, I set up a NSObject subclass and added ObjC Runtime method replacements. Here’s an example:
methods.Append new TargetClassMethodHelper("URLSession:dataTask:didBecomeDownloadTask:", AddressOf impl_URLSessiondataTaskdidBecomeDownloadTask, "v@:@@@")
mClassPtr = BuildTargetClass ("NSObject", "iOSLibURLSessionDel",methods)
(Code that works throughout the project)
That works, and the shared implementation looks like
Private Shared Sub impl_URLSessiondataTaskdidBecomeDownloadTask(pid as ptr, sel as ptr, session as Ptr, task as ptr, downloadtask as ptr)
// dim ego as AppleURLSession= AppleURLSession.MakeFromPtr (session)
// ego.informonURLSessiondataTaskdidBecomeDownloadTask (task, downloadtask)
break
#Pragma Unused pid
#Pragma Unused Sel
End Sub
The first two lines are commented out. They would usually call this method on Ego:
Attributes( hidden ) Sub informonURLSessiondataTaskdidBecomeDownloadTask(task as ptr, downloadtask as ptr)
RaiseEvent DataTaskdidBecomeDownloadTask (new AppleURLSessionTask(task),new AppleURLSessionDownloadTask(downloadtask))
End Sub
Once I uncomment the first lines, the whole project crashes before it shows its first view, even if I define a new SessionDelegate in a button action event. No Xojo debugger or error. In Console I find an
error which is quite funny because the content of my global text variable FoundationLibname ist Foundation.framework, not FoundationLibname.Framework. There is no rename in the upper form and the error only appears if I try to activate the code lines in the implementation, though it is not executed. Similar code is used without problems throughout the whole project, but once I decomment the lines above, the project will not start even if it does nothing with NSURLSession framework.
Any ideas what might be causing this issue? The code is not even called and yet it crashes without notice?
EDIT: Needless to say: I can create a AppleURLSession with its constructors as a separate object, and its declares use the foundationlibname constant