Andy Andy - 1 month ago 33
Objective-C Question

objective c convert special URL to NSURL

I have a

URL
like this, and I can't convert it to the
NSUrl
.

NSString * mURL = @"http://cl3.webterren.com/1.gif?z=7&a=157ffb99379&b=%u5149%u660E%u7F51_%u65B0%u95FB%u89C6%u91CE%u3001%u6587%u5316%u89C6%u89D2%u3001%u601D%u60F3%u6DF1%u5EA6%u3001%u7406%u8BBA%u9AD8%u5EA6&B=UTF-8&c=http%3A//gmw.cn/%3F_wdxid%3D000000000000000000000000000000000000000000%26_wdc%3D0%26_wdt%3D012%26&d=&e=0&f=0&H=gmw.cn&E=0&r=6c4a9d9057c8bcef&s=0&t=0&u=1&i=zh-tw&j=0&k=320x568&l=32&m=&n=&o=8";
NSString *pathComponent = [mURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL* uri = [NSURL URLWithString:pathComponent];
NSLog(@"uri host = %@", uri.host);
NSLog(@"uri path = %@", uri.path);


And, the host and path are (null). How can I fix this?

Answer

Please check below code, it is working,

NSString * mURL = @"http://cl3.webterren.com/1.gif?z=7&a=157ffb99379&b=%u5149%u660E%u7F51_%u65B0%u95FB%u89C6%u91CE%u3001%u6587%u5316%u89C6%u89D2%u3001%u601D%u60F3%u6DF1%u5EA6%u3001%u7406%u8BBA%u9AD8%u5EA6&B=UTF-8&c=http%3A//gmw.cn/%3F_wdxid%3D000000000000000000000000000000000000000000%26_wdc%3D0%26_wdt%3D012%26&d=&e=0&f=0&H=gmw.cn&E=0&r=6c4a9d9057c8bcef&s=0&t=0&u=1&i=zh-tw&j=0&k=320x568&l=32&m=&n=&o=8";
NSString * webStringURL = [mURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL* url = [NSURL URLWithString:webStringURL];
NSLog(@"uri host = %@", url.host);
NSLog(@"uri path = %@", url.path);