FreePascal / Lazarus并实施nsurlconnectiondatadelegate

时间:2018-05-27 10:18:20

标签: pascal lazarus freepascal nsurlconnectiondelegate

我正在尝试实现nsurlconnectiondatadelegate,因为我需要支持异步模式 - 在同步模式下,自动跟踪重定向,这是我不想要的。

作为参考,我将代码与urlRequest等同步工作。

代码段

{$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"

...

有了上述内容,初步测试似乎不太好。我们到达urlConnection.start;但是ConnectionDidFinishLoading永远不会被调用。

我的理论是,这可能是因为我们没有完全实现代表。但是,这样做会给我一些问题 - 下面的代码片段:

  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::';
  • 在一个函数中,我翻译了NSURLResponse?作为指针 NSURLResponse ......但不确定这里有什么问题?
  • 我不确定为什么我对编译器必须只使用“覆盖” 第一个功能。据我所知 - 没有任何功能 在NSObject中实现?
  • 我定义两个连接的那一刻(ANSUC:NSURLConnection; didReceive:NSData)和连接(ANSUC:NSURLConnection; didReceive:NSURLResponse)编译器使Fatal编译中止 没有进一步的错误消息我想是因为他们碰撞了 不知何故,但不确定如何以不同的方式定义它们?

0 个答案:

没有答案