我正在尝试实现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::';