LoginSignup
12
10

More than 5 years have passed since last update.

[Objective-C] NSURLの生成でハマったのでメモ

Posted at

NSURL#URLWithString:メソッドに渡す文字列が不正だとnilが返される。
文字列自体は設定しているのに遷移しないーと思っていたら上記が問題だった。
(具体的には設定されたURLに{}が含まれていた)

対処?

最終的にはサーバでってことになったのだけど、クライアントサイドだけでなんとかならないかなーとごにょごにょしたのでメモ。
結果的には、CoreFoundationCFURLCreateStringByAddingPercentEscapes関数を使っていったん、{}をURLエンコードしてNSURLに渡すようにした。

そのときに書いた簡単な動作チェック用のソースを残しておきます。

#import <Foundation/Foundation.h>

int main() {

    NSString *urlString = @"http://example.com/web12345/{any_id}/foo?app_id={app_id}&param={param}";
    NSString *encodeString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                kCFAllocatorDefault,
                (CFStringRef)urlString,
                NULL,
                (CFStringRef)@"",
                kCFStringEncodingUTF8
            );
    NSURL *url = [NSURL URLWithString:encodeString];

    NSLog(@"URL String    : %@\n\n", urlString   );
    NSLog(@"Encoded String: %@\n\n", encodeString);
    NSLog(@"URL           : %@\n\n", url         );

    NSLog(@"absoluteString: %@\n",    [url absoluteString   ]);
    NSLog(@"absoluteURL: %@\n",       [url absoluteURL      ]);
    NSLog(@"baseURL: %@\n",           [url baseURL          ]);
    NSLog(@"fragment: %@\n",          [url fragment         ]);
    NSLog(@"host: %@\n",              [url host             ]);
    NSLog(@"lastPathComponent: %@\n", [url lastPathComponent]);
    NSLog(@"parameterString: %@\n",   [url parameterString  ]);
    NSLog(@"password: %@\n",          [url password         ]);
    NSLog(@"path: %@\n",              [url path             ]);
    NSLog(@"pathComponents: %@\n",    [url pathComponents   ]);
    NSLog(@"pathExtension: %@\n",     [url pathExtension    ]);
    NSLog(@"port: %@\n",              [url port             ]);
    NSLog(@"query: %@\n",             [url query            ]);
    NSLog(@"relativePath: %@\n",      [url relativePath     ]);
    NSLog(@"relativeString: %@\n",    [url relativeString   ]);
    NSLog(@"resourceSpecifier: %@\n", [url resourceSpecifier]);
    NSLog(@"scheme: %@\n",            [url scheme           ]);
    NSLog(@"standardizedURL: %@\n",   [url standardizedURL  ]);
    NSLog(@"user: %@\n",              [url user             ]);

    return 0;
}

出力結果

2014-05-23 14:36:11.595 sample[28713:507] URL String    : http://example.com/web12345/{any_id}/foo?app_id={app_id}&param={param}

2014-05-23 14:36:11.597 sample[28713:507] Encoded String: http://example.com/web12345/%7Bany_id%7D/foo?app_id=%7Bapp_id%7D&param=%7Bparam%7D

2014-05-23 14:36:11.598 sample[28713:507] URL           : http://example.com/web12345/%7Bany_id%7D/foo?app_id=%7Bapp_id%7D&param=%7Bparam%7D

2014-05-23 14:36:11.598 sample[28713:507] absoluteString: http://example.com/web12345/%7Bany_id%7D/foo?app_id=%7Bapp_id%7D&param=%7Bparam%7D
2014-05-23 14:36:11.598 sample[28713:507] absoluteURL: http://example.com/web12345/%7Bany_id%7D/foo?app_id=%7Bapp_id%7D&param=%7Bparam%7D
2014-05-23 14:36:11.599 sample[28713:507] baseURL: (null)
2014-05-23 14:36:11.599 sample[28713:507] fragment: (null)
2014-05-23 14:36:11.600 sample[28713:507] host: example.com
2014-05-23 14:36:11.600 sample[28713:507] lastPathComponent: foo
2014-05-23 14:36:11.600 sample[28713:507] parameterString: (null)
2014-05-23 14:36:11.601 sample[28713:507] password: (null)
2014-05-23 14:36:11.601 sample[28713:507] path: /web12345/{any_id}/foo
2014-05-23 14:36:11.601 sample[28713:507] pathComponents: (
    "/",
    web12345,
    "{any_id}",
    foo
)
2014-05-23 14:36:11.602 sample[28713:507] pathExtension:
2014-05-23 14:36:11.602 sample[28713:507] port: (null)
2014-05-23 14:36:11.602 sample[28713:507] query: app_id=%7Bapp_id%7D&param=%7Bparam%7D
2014-05-23 14:36:11.603 sample[28713:507] relativePath: /web12345/{any_id}/foo
2014-05-23 14:36:11.603 sample[28713:507] relativeString: http://example.com/web12345/%7Bany_id%7D/foo?app_id=%7Bapp_id%7D&param=%7Bparam%7D
2014-05-23 14:36:11.603 sample[28713:507] resourceSpecifier: //example.com/web12345/%7Bany_id%7D/foo?app_id=%7Bapp_id%7D&param=%7Bparam%7D
2014-05-23 14:36:11.604 sample[28713:507] scheme: http
2014-05-23 14:36:11.604 sample[28713:507] standardizedURL: http://example.com/web12345/%7Bany_id%7D/foo?app_id=%7Bapp_id%7D&param=%7Bparam%7D
2014-05-23 14:36:11.604 sample[28713:507] user: (null)
12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10