FreePascal / Lazarus and implementing nsurlconnectiondatadelegate
Asked Answered
R

1

8

I am trying to implement nsurlconnectiondatadelegate as I need to support async mode - in synchronous mode redirects are followed automatically which I do not want.

For reference I have the code working in synchronous mode with urlRequest etc.

The problem is I can not get FPC/Lazarus to compile my code.

...

Code snippets

{$mode objfpc}
{$modeswitch objectivec1}
{$modeswitch objectivec2}

...

// We need to implement support for NSURLConnectionDelegate and NSURLConnectionDataDelegate
TmsMacRequestDelegate = objcclass(NSObject)
public
  // this will set flag when done
  procedure connectionDidFinishLoading(ANSUC: NSURLConnection); message onnectionDidFinishLoading:'; override;
  // ... implement rest?
end;

...

requestDelegate := TmsMacRequestDelegate.alloc.init;
urlConnection := NSURLConnection.connectionWithRequest_delegate(urlRequest, requestDelegate);
// ... setup flag
urlConnection.start;
// ... wait here in loop checking flag set by "finish loading"

...

With the above, initial testing seems going-not-so-well. We reach urlConnection.start; but connectionDidFinishLoading never gets called. My theory is that it may be because we do not fully implement the delegate. However, doing so gives me other problems - here is declaration:

TmsMacRequestDelegate = objcclass(NSObject)
public
procedure connectionDidFinishLoading(ANSUC: NSURLConnection); message 'connectionDidFinishLoading:'; override;
procedure connection(ANSUC: NSURLConnection; didReceive: NSURLResponse); message 'connection::';
procedure connection(ANSUC: NSURLConnection; didReceive: NSData); message 'connection::';
procedure connection(ANSUC: NSURLConnection; didSendBodyData: Integer; totalBytesWritten: Integer; totalBytesExpectedToWrite: Integer); message 'connection::::';
procedure connection(ANSUC: NSURLConnection; willSend: NSURLRequest; redirectResponse: PNSURLResponse); message 'connection:::';
procedure connection(ANSUC: NSURLConnection; willCacheResponse: NSCachedURLResponse); message 'connection::';
end;
  • In one function I have translated NSURLResponse? as a pointer to NSURLResponse... But not sure what is correct here?
  • Compiler complains that I have to add "override" on my first function (although none of the functions are implemented in NSObject?) with this message:

Error: Inherited methods can only be overridden in Objective-C and Java, add "override" (inherited method defined in NSURLConnectionDelegateCategory

  • If I add "override" like suggested I get:

Error: :219:1: error: invalid symbol redefinition

Error: "-TmsMacRequestDeletegate connection::]":

Error: ^

Replevin answered 27/5, 2018 at 10:18 Comment(0)
M
0

Sorry for the late reply. I don't actively follow SO, but someone just pointed me to this post.

The declaration of the NSURLConnectionDataDelegateProtocol is available in the CocoaAll unit that ships with FPC. You can declare your delegate as objcclass(NSObject, NSURLConnectionDataDelegateProtocol) so

  1. you don't need to specify the message name for each method/message (the compiler will take them from the protocol/interface)
  2. the compiler can point out any errors in terms of missing methods/clashing message names

The main issue is that your message names are incomplete. E.g. for your first "connection" method, it has to be 'connection:didReceiveResponse:'. That's why the runtime is not finding them.

Manuscript answered 11/1, 2023 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.