NSURL URLWithString returns 无

NSURL URLWithString returns nil

我陷入了一个非常明显的方法 NSURL class URLWithString 我正在传递一个 string 并且在创建 URL 它时 returns nil,我的 string 是完美的。当我在浏览器中使用相同的字符串时,它工作正常。我正在使用以下方法将 NSString 转换为 NSURL:

NSURL *url = [NSURL URLWithString:urlString];
//urlString is my perfect string

我也尝试使用以下方法首先对我的字符串进行编码

NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// using this line my output string when I log url is "url%10%10%10%10%10%10..."

%10 becomes the suffix of my url with around 100+ repetition。 如果有人知道 %10 是什么或可以做些什么来克服这个问题。

这是我的 urlString:

尝试以下解决方案

NSURL *url = [NSURL URLWithString:[urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

检查一下..!

是的,由于从互联网站点复制,我的字符串中有很多无法识别的字符,如果面临此问题的 reader 中的任何人可以复制并粘贴您的字符串,请参阅计数。并确认。 谢谢

使用以下代码解决您的问题。

NSString *str = msgDetail[@"thumbnail"];

NSString* webStringURL = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];

NSURL* url = [NSURL URLWithString:webStringURL];