ios - objective c 将特殊 URL 转换为 NSURL

标签 ios objective-c url nsstring nsurl

我有一个像这样的 URL,我无法将它转换为 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);

并且,主机和路径是 (null)。我该如何解决这个问题?

最佳答案

请检查下面的代码,它正在工作,

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

关于ios - objective c 将特殊 URL 转换为 NSURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40256036/

相关文章:

ios - swift didSelectRowAtIndexPath 可以多次调用

ios - OpenGL ES 2.0 渲染到纹理

perl - 在 Perl 中如何找到所有重定向后的最终 URL?

iOS 控制台用户输入

ios - 协议(protocol)实现中的 IBOutlet

iphone - 为什么 GDB 调试器会不断卡住 Xcode 4?

ios - 文本少于 10 个字符的 UITextView 挂起 iOS 9

ios - 自定义 UITableViewCell 重新加载行不正确

http - 我可以在 URL 中使用 at 符号 (@) 吗?

.htaccess - 如何使用 .htaccess 重写此 url