【问题标题】:URLWithString: returns nilURLWithString:返回零
【发布时间】:2010-12-31 04:24:57
【问题描述】:

这可能很容易,但我似乎不明白为什么 URLWithString: 在这里返回 nil。

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false&key=", webName];
NSURL* url = [NSURL URLWithString:stringURL];

【问题讨论】:

  • 在您致电stringWithFormat: 之前,webName 的值是多少?那么,在您调用URLWithString: 之前,stringURL 的值是多少?使用NSLog() 逐步打印出来,或者设置断点并检查设置的值。

标签: iphone objective-c url nsstring nsurl


【解决方案1】:

您还需要转义硬编码 URL 中的非 ASCII 字符:

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

您可能可以删除 localisationName 的转义,因为它将通过整个字符串的转义来处理。

【讨论】:

  • 试图理解为什么 URLWithString 在这里给出 nil。 Apple 文档说“必须是符合 RFC 2396 的 URL。此方法根据 RFC 1738 和 1808 解析 URLString。”。正在阅读...
  • 大声笑,我从电子邮件中复制了一个 URL,这发生了!谢谢。
  • 如果 URL 中有 # 则中断。 stringByAddingPercentEscapesUsingEncoding 将 # 转换为 %2
  • 以上 API 已弃用。您可以使用以下 API webStringURL = [stringURL stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
【解决方案2】:

如果您处理保存在文件管理器上的文件,请使用此功能。

NSURL *_url = [NSURL fileURLWithPath:path];

【讨论】:

  • 谢谢!我得到了一个“中间修剪”的网址
【解决方案3】:

我猜你需要使用-[NSString stringByAddingPercentEscapesUsingEncoding:]。见Apple doc

另一个评论是,作为一个老前辈,我觉得将非 ASCII 字符放在源文件中有点不方便。 也就是说,Apple doc 表示,从 10.4 开始,@"..." 内的 UTF-16 字符串是可以的。 不知何故,GCC 似乎正确地将 Latin-1 中的源文件转换为二进制中的 UTF-16,但我认为使用它是最安全的 7 位 ASCII 字符仅在源代码内部,并使用NSLocalizedString

【讨论】:

    【解决方案4】:

    我认为你的重音字符正在扔东西;它们不会被 -stringByAddingPercentEscapesUsingEncoding: 处理。

    【讨论】:

      【解决方案5】:

      如果 URL 不符合 RFC 2396 并且必须转义,则 NSURL URLWithString:@"" 将返回 nil

      如果您阅读链接中的rfc2396,您将获得大量详细信息

      我发现了一个很棒的网站,用于检查违规字符的位置,请选择 URL 的路径选项

      http://www.websitedev.de/temp/rfc2396-check.html.gz

      【讨论】:

        【解决方案6】:

        如果传递给它的字符串格式错误,则 URLWithString: 调用将返回 nil。 由于 NSURL 会为格式错误的 url 返回 nil,因此 NSLog 您的字符串并设置断点以准确查看传递给您的 NSURL 创建方法的内容。如果您的 URLWithString 使用硬编码值,则进一步证明您传递的任何内容都是错误的。see

        【讨论】:

          【解决方案7】:

          更新: 由于stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding 现在已弃用,您应该使用stringByAddingPercentEncodingWithAllowedCharacters

          [stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
          

          以下是根据您的情况设置的允许字符列表。

          + (NSCharacterSet *)URLUserAllowedCharacterSet;
          + (NSCharacterSet *)URLPasswordAllowedCharacterSet;
          + (NSCharacterSet *)URLHostAllowedCharacterSet;
          + (NSCharacterSet *)URLPathAllowedCharacterSet;
          + (NSCharacterSet *)URLQueryAllowedCharacterSet;
          + (NSCharacterSet *)URLFragmentAllowedCharacterSet;
          

          参考:https://developer.apple.com/reference/foundation/nsstring/1411946-stringbyaddingpercentencodingwit

          【讨论】:

            【解决方案8】:

            你可以不使用 NSString 直接使用 NSURL。

            //.h 文件

            @interface NewsBrowser : UIViewController {
            
                UIWebView *webView;
                NSURL *NewsUrl;
            
            }
            
            @property (nonatomic, retain) IBOutlet UIWebView *webView;
            
            @property(nonatomic,assign)NSURL *NewsUrl;
            
            @end
            

            //.m 文件

            [webView loadRequest:[NSURLRequest requestWithURL:NewsUrl]];
            

            我将 URL 从另一个视图(使用 NewsUrl 变量)传递到这个视图。

            试试看。

            【讨论】:

            • 这个答案根本没有解决这个问题,这是在问为什么 URLWithString: 返回 nil。 (此外,您甚至没有展示将字符串更改为 URL 的方法。)
            猜你喜欢
            • 2013-12-04
            • 1970-01-01
            • 1970-01-01
            • 2015-07-30
            • 2012-03-12
            • 2011-01-07
            • 2012-06-03
            • 2021-09-23
            • 2016-11-24
            相关资源
            最近更新 更多