Lazarus

Programming => Operating Systems => macOS / Mac OS X => Topic started by: MISV on July 06, 2017, 05:06:25 pm

Title: Objective Pascal PasCocoa declaration problems with delegates (NSURLConnection)
Post by: MISV on July 06, 2017, 05:06:25 pm
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
Title: Re: Use NSURLConnection with a delegate
Post by: Phil 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
Title: Re: Objective Pascal PasCocoa declaration problems with delegates (NSURLConnection)
Post by: MISV on December 14, 2019, 05:20:43 pm
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