Andy
Andy

Reputation: 407

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?

Upvotes: 0

Views: 2827

Answers (3)

alicanbatur
alicanbatur

Reputation: 2200

In swift 3:

let 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";
    if let webStringURL = mURL.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlFragmentAllowed),
        let url = NSURL(string: webStringURL) {
        debugPrint("uri host = \(url.host!)")
        debugPrint("uri path = \(url.path!)")
    }

Upvotes: 0

kb920
kb920

Reputation: 3089

Check this

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";
NSURL *pathComponent = [NSURL URLWithString:[mURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"uri host = %@", pathComponent.host);
NSLog(@"uri path = %@", pathComponent.path);

Upvotes: 1

Rajat
Rajat

Reputation: 11127

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);

Upvotes: 4

Related Questions