如何从包含国际字符的 NSString 准备 NSURL?

发布于 2024-09-13 10:11:48 字数 249 浏览 6 评论 0原文

我必须使用带有国际字符的 GET 来访问网络服务器(在我的例子中是希伯来语,但可以是任何东西)。 所以我制作了一个 NSString 就很好,但

[NSURL URLWithString:urlString]; // returns nil.

我意识到我可能必须将国际字符转换为百分比代码。

Objective-c 中有内置方法可以做到这一点吗?

I have to access a web server using a GET with international characters (Hebrew in my case but could be anything).
So I make an NSString just fine but

[NSURL URLWithString:urlString]; // returns nil.

I realize I probably have to convert the international characters to percent codes.

Is there a built in method in Objective-c to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗失的美好 2024-09-20 10:11:48

是的,您需要 -stringByAddingPercentEscapesUsingEncoding: 方法:

[NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Yes there is, you need -stringByAddingPercentEscapesUsingEncoding: method:

[NSURL URLWithString:[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
好听的两个字的网名 2024-09-20 10:11:48

这将确保 NSURL *url 不会因为 urlString 中的 "|" 而返回 nil

NSString *urlString = [[NSString stringWithFormat:@"http://api.service.com/Service.svc?sort=name|ascending"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlString];

This would ensure NSURL *url does not return nil because of the "|" in the urlString.

NSString *urlString = [[NSString stringWithFormat:@"http://api.service.com/Service.svc?sort=name|ascending"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:urlString];
ぃ弥猫深巷。 2024-09-20 10:11:48

您可以直接使用 NSURL 而无需 NSString

//.h file

@interface NewsBrowser : UIViewController {
    UIWebView *webView;
    NSURL *NewsUrl;
}

@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, assign) NSURL *NewsUrl;

@end

//.m file

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

只需将 NSURL 从另一个视图(使用 NewsUrl 变量)传递给此视图看法。

You can use NSURL directly without NSString:

//.h file

@interface NewsBrowser : UIViewController {
    UIWebView *webView;
    NSURL *NewsUrl;
}

@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, assign) NSURL *NewsUrl;

@end

//.m file

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

Just pass a NSURL from another view (using NewsUrl variable) to this view.

故事灯 2024-09-20 10:11:48
+ (NSURL*) URLWithStringWithSpecialChars:(NSString *)sURL
{
    NSURL *url = [NSURL URLWithString:sURL];
    if(url == nil)
        return [NSURL URLWithString:[sURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    else
        return url;
}

调用此函数而不是 URLWithString:应确保始终返回有效的 NSURL(而不是在需要时返回 nil)

+ (NSURL*) URLWithStringWithSpecialChars:(NSString *)sURL
{
    NSURL *url = [NSURL URLWithString:sURL];
    if(url == nil)
        return [NSURL URLWithString:[sURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    else
        return url;
}

calling this function instead of URLWithString: should ensure that a valid NSURL is always returned (instead of nil when needed)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文