のドキュメントを読むと、読み込み中に発生した可能性のあるリダイレクトNSURLConnection
がcurrentRequest
メソッドに反映されているはずです。
接続がロードを実行すると、プロトコルの正規化の結果として、または次のリダイレクトにより、リクエストが変更される場合があります。このメソッドは、現在の値を取得するために使用されます。
ただし、を調べると、応答データを調べるとリダイレクトが正常に完了したことが示されていても、常に元の要求の URL が含まcurrentRequest
れています。connectionDidFinishLoading:
(以下の不自然な例を参照)
currentRequest
私の質問はこれです:実際の現在のリクエストを返さないのはなぜですか? 私は何か間違ったことをしていますか、それともこれは予想される動作ですか? これが予想される動作である場合、何currentRequest
に役立つと思われますか?
@interface AppDelegate : UIResponder <UIApplicationDelegate, NSURLConnectionDataDelegate>
@property (strong, nonatomic) NSURLConnection *connection;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *url = [[NSURL alloc] initWithString:@"http://jigsaw.w3.org/HTTP/300/301.html"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
return YES;
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
NSLog(@"willSendRequest // request: %@ // currentRequest: %@", request, [connection currentRequest]);
return request;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"didFinishLoading // originalRequest: %@ // currentRequest: %@", [connection originalRequest], [connection currentRequest]);
}
@end
この例を実行すると、次の出力が得られます。
2012-12-18 15:18:44.949 ConnectionTest[12534:c07] willSendRequest // request: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html> // currentRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html>
2012-12-18 15:18:44.954 ConnectionTest[12534:c07] willSendRequest // request: <NSURLRequest http://jigsaw.w3.org/HTTP/300/Overview.html> // currentRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html>
2012-12-18 15:18:44.955 ConnectionTest[12534:c07] didFinishLoading // originalRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html> // currentRequest: <NSURLRequest http://jigsaw.w3.org/HTTP/300/301.html>
への最後の呼び出しでは、 でcurrentRequest
終わるページ URL が表示されると予想されますOverview.html
が、代わりに301.html
.