Recent

Author Topic: Objective Pascal PasCocoa declaration problems with delegates (NSURLConnection)  (Read 6197 times)

MISV

  • Hero Member
  • *****
  • Posts: 783
I have trouble figuring out how to write Lazarus code that interfaces with delegates...

--I have reworked this post *after* Phil responded below--

Since I need to use async mode (to grab the complete redirect chain) Phils HTTP example is not sufficient and I need to implement the delegate... And this is causing me problems


Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$modeswitch objectivec1}
  3. {$modeswitch objectivec2}
  4.  
  5. interface
  6.  
  7. uses
  8.   CocoaAll
  9. ;
  10.  
  11. type
  12. // Working on adding all required functions accordingly to documentation https://developer.apple.com/documentation/foundation/nsurlconnectiondatadelegate]https://developer.apple.com/documentation/foundation/nsurlconnectiondatadelegate
  13.   TmsMacRequestDelegate = objcclass(NSObject)
  14.   public
  15.     procedure connectionDidFinishLoading(ANSUC: NSURLConnection); message 'connectionDidFinishLoading:'; override;
  16.     procedure connection(ANSUC: NSURLConnection; didReceive: NSURLResponse); message 'connection::';
  17.     procedure connection(ANSUC: NSURLConnection; didReceive: NSData); message 'connection::';
  18.     procedure connection(ANSUC: NSURLConnection; didSendBodyData: Integer; totalBytesWritten: Integer; totalBytesExpectedToWrite: Integer); message 'connection::::';
  19.     procedure connection(ANSUC: NSURLConnection; willSend: NSURLRequest; redirectResponse: PNSURLResponse); message 'connection:::';
  20.     procedure connection(ANSUC: NSURLConnection; willCacheResponse: NSCachedURLResponse); message 'connection::';
  21.  
  22. implementation
  23.  

Questions
  • In one function I have translated NSURLResponse? as a pointer to NSURLResponse... But not sure what is correct here?
  • I am unsure why I accordingly to compiler must use "override" on only first function. As far as I can reason - none of the functions are implemented in NSObject?
  • The moment I have defined both connection(ANSUC: NSURLConnection; didReceive: NSData) and connection(ANSUC: NSURLConnection; didReceive: NSURLResponse) compiler gives Fatal compilation aborted without further error message. I suppose because they collide somehow, but not sure how to define them differently?
« Last Edit: December 14, 2019, 05:19:58 pm by MISV »

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Use NSURLConnection with a delegate
« Reply #1 on: August 02, 2017, 01:17:46 am »
Let me know if you need more information. I tried SO as well - no luck. But it should be possible in Lazarus as far as I can ttell.

Note that this has nothing to do with Lazarus. Objective Pascal is what you're trying to write.

Your message is not correct. Should be: 'connectionDidFinishLoading:'  Look at how it's declared in FPC's NSURLConnection.inc.

The rest of your code doesn't appear to have anything to do with writing a delegate per se.

Delegates are common and easy. See the Cocoa implementation for TWebBrowser and how it uses a delegate to handle when the WebView is done loading:

https://macpgmr.github.io/MacXPlatform/lclwebbrowser-src.zip

Also note that if you just want to do an HTTP request synchronously, you can just use the ns_url_request.pas unit in nsunits-src.zip here:

https://macpgmr.github.io

MISV

  • Hero Member
  • *****
  • Posts: 783
The importance of this problem has remerged as Indy/OpenSSL/notarized has become more problematic on Catalina.

One that (I atleast) needs to be solved if one wants to use native Mac API for e.g. keeping track of redirects, but could also be useful for others how ti implement delegates when these error messaegs are encountered.

 

TinyPortal © 2005-2018