今日、奇妙なことに出くわしました。おそらく簡単な説明があります。
サブディレクトリであるベース URL に対して最初に構築された NSURL を使用して NSURLRequest を作成すると、NSURLRequest はサブディレクトリを削除します。
これは、コードで説明する方がはるかに簡単です。
// create a base URL to work with
NSURL *baseUrl = [NSURL URLWithString:@"http://www.google.com/sub"];
// create a URL relative to the base URL
NSURL *url1 = [NSURL URLWithString:@"/foo/bar" relativeToURL:baseUrl];
// create a NSURLRequest using this first URL
NSURLRequest *mangled_req = [[NSURLRequest alloc] initWithURL:url1];
NSLog(@"url1: %@", url1);
NSLog(@"mangled_req: %@", mangled_req);
// now create another URL that puts the base URL together with the relative path
NSURL *url2 = [NSURL URLWithString:@"http://www.google.com/sub/foo/bar"];
// and create a NSURLRequest, which should be the same as above
NSURLRequest *correct_req = [[NSURLRequest alloc] initWithURL:url2];
NSLog(@"url2: %@", url2);
NSLog(@"correct_req: %@", correct_req);
出力は次の点を示しています。
2013-01-25 11:55:37.386 url1: /foo/bar -- http://www.google.com/sub
2013-01-25 11:55:37.408 mangled_req: <NSURLRequest http://www.google.com/foo/bar>
2013-01-25 11:55:37.409 url2: http://www.google.com/sub/foo/bar
2013-01-25 11:55:37.409 correct_req: <NSURLRequest http://www.google.com/sub/foo/bar>
「mangled_req」では /sub が省略されていることに注意してください。
私は AFNetworking を使用していて、テストのためにローカルホスト (当然サブディレクトリに Web アプリケーションがある) とリモートサーバー (ない) を切り替えたいので、これは今私を悩ませています。
確かに回避策はありますが、これは私が何か間違ったことをしているに違いないほど奇妙に思えます。