NSURLで取得出来るURL(URI)のパラメータ一覧
NSURLのどのメソッドでどの要素が取れるんだっけ?となりがちなのでメモしとく。
プログラム。
NSURL *url = [NSURL URLWithString:@"http://user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test"]; NSLog(@"absoluteString : %@", [url absoluteString]); NSLog(@"absoluteURL : %@", [url absoluteURL]); NSLog(@"baseURL : %@", [url baseURL]); NSLog(@"fragment : %@", [url fragment]); NSLog(@"host : %@", [url host]); NSLog(@"lastPathComponent : %@", [url lastPathComponent]); NSLog(@"parameterString : %@", [url parameterString]); NSLog(@"password : %@", [url password]); NSLog(@"path : %@", [url path]); NSLog(@"pathComponents : %@", [url pathComponents]); NSLog(@"pathExtension : %@", [url pathExtension]); NSLog(@"port : %@", [url port]); NSLog(@"query : %@", [url query]); NSLog(@"relativePath : %@", [url relativePath]); NSLog(@"relativeString : %@", [url relativeString]); NSLog(@"resourceSpecifier : %@", [url resourceSpecifier]); NSLog(@"scheme : %@", [url scheme]); NSLog(@"standardizedURL : %@", [url standardizedURL]); NSLog(@"user : %@", [url user]);
出力結果。
2011-04-22 01:21:05.355 Test[1737:207] absoluteString : http://user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:21:05.357 Test[1737:207] absoluteURL : http://user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:21:05.358 Test[1737:207] baseURL : (null) 2011-04-22 01:21:05.358 Test[1737:207] fragment : test 2011-04-22 01:21:05.358 Test[1737:207] host : www.example.com 2011-04-22 01:21:05.359 Test[1737:207] lastPathComponent : index.html 2011-04-22 01:21:05.451 Test[1737:207] parameterString : (null) 2011-04-22 01:21:05.473 Test[1737:207] password : password 2011-04-22 01:21:05.474 Test[1737:207] path : /hoge/fuga/index.html 2011-04-22 01:21:05.474 Test[1737:207] pathComponents : ( "/", hoge, fuga, "index.html" ) 2011-04-22 01:21:05.475 Test[1737:207] pathExtension : html 2011-04-22 01:21:05.475 Test[1737:207] port : 8080 2011-04-22 01:21:05.495 Test[1737:207] query : a=1&b=2 2011-04-22 01:21:05.496 Test[1737:207] relativePath : /hoge/fuga/index.html 2011-04-22 01:21:05.496 Test[1737:207] relativeString : http://user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:21:05.497 Test[1737:207] resourceSpecifier : //user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:21:05.498 Test[1737:207] scheme : http 2011-04-22 01:21:05.541 Test[1737:207] standardizedURL : http://user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:21:05.542 Test[1737:207] user : user
Base URLを使用した場合のプログラム。
NSURL *baseUrl = [NSURL URLWithString:@"http://user:password@www.example.com:8080"]; NSURL *url = [NSURL URLWithString:@"hoge/fuga/index.html?a=1&b=2#test" relativeToURL:baseUrl]; NSLog(@"absoluteString : %@", [url absoluteString]); NSLog(@"absoluteURL : %@", [url absoluteURL]); NSLog(@"baseURL : %@", [url baseURL]); NSLog(@"fragment : %@", [url fragment]); NSLog(@"host : %@", [url host]); NSLog(@"lastPathComponent : %@", [url lastPathComponent]); NSLog(@"parameterString : %@", [url parameterString]); NSLog(@"password : %@", [url password]); NSLog(@"path : %@", [url path]); NSLog(@"pathComponents : %@", [url pathComponents]); NSLog(@"pathExtension : %@", [url pathExtension]); NSLog(@"port : %@", [url port]); NSLog(@"query : %@", [url query]); NSLog(@"relativePath : %@", [url relativePath]); NSLog(@"relativeString : %@", [url relativeString]); NSLog(@"resourceSpecifier : %@", [url resourceSpecifier]); NSLog(@"scheme : %@", [url scheme]); NSLog(@"standardizedURL : %@", [url standardizedURL]); NSLog(@"user : %@", [url user]);
出力結果。
2011-04-22 01:26:38.274 Test[1904:207] absoluteString : http://user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:26:38.307 Test[1904:207] absoluteURL : http://user:password@www.example.com:8080/hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:26:38.309 Test[1904:207] baseURL : http://user:password@www.example.com:8080 2011-04-22 01:26:38.310 Test[1904:207] fragment : test 2011-04-22 01:26:38.312 Test[1904:207] host : www.example.com 2011-04-22 01:26:38.313 Test[1904:207] lastPathComponent : index.html 2011-04-22 01:26:38.315 Test[1904:207] parameterString : (null) 2011-04-22 01:26:38.316 Test[1904:207] password : password 2011-04-22 01:26:38.323 Test[1904:207] path : /hoge/fuga/index.html 2011-04-22 01:26:38.325 Test[1904:207] pathComponents : ( "/", hoge, fuga, "index.html" ) 2011-04-22 01:26:38.338 Test[1904:207] pathExtension : html 2011-04-22 01:26:38.340 Test[1904:207] port : 8080 2011-04-22 01:26:38.341 Test[1904:207] query : a=1&b=2 2011-04-22 01:26:38.343 Test[1904:207] relativePath : hoge/fuga/index.html 2011-04-22 01:26:38.344 Test[1904:207] relativeString : hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:26:38.345 Test[1904:207] resourceSpecifier : hoge/fuga/index.html?a=1&b=2#test 2011-04-22 01:26:38.347 Test[1904:207] scheme : http 2011-04-22 01:26:38.348 Test[1904:207] standardizedURL : hoge/fuga/index.html?a=1&b=2#test -- http://user:password@www.example.com:8080 2011-04-22 01:26:38.350 Test[1904:207] user : user
いずれの場合も parameterString の結果が (null) なのが気になって調べた所、RFC 1808によると「object parameters」と呼ばれるものみたいです。
使用する時の形式はこんな感じ。
;type=a
以下の様に複数指定する事も可能です。
;hoge=1;fuga=2
ということでこんなプログラムで確認。
NSURL *url = [NSURL URLWithString:@"http://www.example.com/index.html;xxx=333?a=1&b=2#test"]; NSLog(@"parameterString : %@", [url parameterString]);
出力結果はこちら。ちゃんと xxx=333 の部分が取得出来ました。
2011-04-22 01:29:46.366 Test[1948:207] parameterString : xxx=333