* * *

Author Topic: Use NSURLConnection with a delegate  (Read 2728 times)


  • Sr. Member
  • ****
  • Posts: 414
Use NSURLConnection with a delegate
« on: July 06, 2017, 05:06:25 pm »
I have trouble figuring out how to write Lazarus code that interfaces with delegates... Any working examples I can take a look at? Here is my attempt:

Code: Pascal  [Select]
  1. {$mode objfpc}
  2. {$modeswitch objectivec1}
  3. {$modeswitch objectivec2}
  5. interface
  7. uses
  8.   CocoaAll
  9. ;
  11. type
  12.   // need implement protocols: NSURLConnectionDelegate + NSURLConnectionDataDelegate
  13.   TmsMacRequestDelegate = objcclass(NSObject)
  14.   public
  15.     procedure connectionDidFinishLoading(ANSUC: NSURLConnection); message ':connectionDidFinishLoading';
  16.   end;
  18. implementation
  20. var
  21.   globalDataRecordsArr: Txxx
  22.   globalDataRecordsArr_CriticalSection: TCriticalSection = nil,
  24.   procedure TmsMacRequestDelegate.connectionDidFinishLoading(ANSUC: NSURLConnection);
  25.   var
  26.     I: Integer;
  27.     B: Integer;
  28.   begin
  29.     globalDataRecordsArr_CriticalSection.Acquire,
  30.     for I := Low(globalDataRecordsArr) to High(globalDataRecordsArr) do
  31.     begin
  32.       globalDataRecordsArr[I].CS.Acquire;
  33.       if globalDataRecordsArr[I].FURLConnection = ANSUC then
  34.         begin
  35.            B := True;
  36.            globalDataRecordsArr[I].Done := True;
  37.         end
  38.       ;
  39.       globalDataRecordsArr[I].CS.Release;
  40.       if B then
  41.         Break
  42.       ;
  43.     end;
  44.     globalDataRecordsArr_CriticalSection.Release;
  45.   end;
« Last Edit: February 06, 2018, 04:12:37 am by MISV »


  • Hero Member
  • *****
  • Posts: 2749
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:


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:



  • Sr. Member
  • ****
  • Posts: 414
Re: Use NSURLConnection with a delegate
« Reply #2 on: February 06, 2018, 02:27:56 am »
The problem using synchronous mode is that redirects are followed automatically. Not good in my case, so I need async mode.

I have a multi-threaded crawler where the implementation can swap between Indy, WinInet and NativeMac - all sharing the same ancestor class. I don't want each thread to be aware of if what gets called is sync or async. I will therefore wrap the async and wait for the call to return.

So after I got synchronous mode working - this is what I have in addition:

Code: Pascal  [Select]
  1.   // We need to implement support for NSURLConnectionDelegate and NSURLConnectionDataDelegate
  2.   TmsMacRequestDelegate = objcclass(NSObject)
  3.   public
  4.     // this will set flag when done
  5.     procedure connectionDidFinishLoading(ANSUC: NSURLConnection); message 'connectionDidFinishLoading:'; override;
  6.     // ... implement rest
  7.   end;

And we intend to call like this

Code: Pascal  [Select]
  1. requestDelegate := TmsMacRequestDelegate.alloc.init;
  2. urlConnection := NSURLConnection.connectionWithRequest_delegate(urlRequest, requestDelegate);
  3. // ... setup flag
  4. urlConnection.start;
  5. // ... wait here checking flag until done

Initial testing seems going-not-so-well though. We reach urlConnection.start; but connectionDidFinishLoading never gets called.

My current theory is that it may be because we do not fully implement the delegate. I will try to do so and see what happens then.
« Last Edit: February 06, 2018, 04:11:13 am by MISV »


  • Sr. Member
  • ****
  • Posts: 414
Re: Use NSURLConnection with a delegate
« Reply #3 on: February 06, 2018, 03:55:37 am »
Working on adding all required functions accordingly to documentation https://developer.apple.com/documentation/foundation/nsurlconnectiondatadelegate

Code: Pascal  [Select]
  2.     procedure connectionDidFinishLoading(ANSUC: NSURLConnection); message 'connectionDidFinishLoading:'; override;
  3.     procedure connection(ANSUC: NSURLConnection; didReceive: NSURLResponse); message 'connection::';
  4.     procedure connection(ANSUC: NSURLConnection; didReceive: NSData); message 'connection::';
  5.     procedure connection(ANSUC: NSURLConnection; didSendBodyData: Integer; totalBytesWritten: Integer; totalBytesExpectedToWrite: Integer); message 'connection::::';
  6.     procedure connection(ANSUC: NSURLConnection; willSend: NSURLRequest; redirectResponse: PNSURLResponse); message 'connection:::';
  7.     procedure connection(ANSUC: NSURLConnection; willCacheResponse: NSCachedURLResponse); message 'connection::';

  • 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: February 06, 2018, 04:13:27 am by MISV »


  • Sr. Member
  • ****
  • Posts: 414
Re: Use NSURLConnection with a delegate
« Reply #4 on: March 09, 2018, 09:20:19 am »
Bump. Am I the only one having problems with objective pascal syntax? i.e. just defining causes compiler errors so not even at runtime problems yet :)



Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus