使用sendAsynchronousRequest时跟踪重定向:queue:completionHandler:?

And*_*sek 5 iphone cocoa-touch objective-c ios ios5

在使用该方法发送请求时,如何在重定向时调用自定义代码:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler
Run Code Online (Sandbox Code Playgroud)

我想要复制NSURLConnectionDelegate方法的行为:

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
Run Code Online (Sandbox Code Playgroud)

nha*_*tdh 1

我认为您无法复制该行为,因为sendAsynchronousRequest仅调用代码块中为其提供的完成处理程序。

看来你必须使用委托方法来达到这个目的。我没有看到使用sendAsynchronousRequest普通的有任何好处NSURLConnection,除了你必须保留 NSMutableData 的 ivar 和可能的 NSURLConnection (如果该类的每个实例有多个)。