URLWithString的问题:当string包含Arab chars时

时间:2011-04-04 14:24:20

标签: ios nsurl

当路径包含阿拉伯字符时,我的代码有问题。在这种情况下,URLWithString始终返回nil。

NSString *path = self.currentlySelectedBlogItem.linkUrl;
NSURL *url = [NSURL URLWithString:path];
[[UIApplication sharedApplication] openURL:url];

任何sugestions?

1 个答案:

答案 0 :(得分:2)

您必须对特殊字符使用UTF8编码:

NSString *path = self.currentlySelectedBlogItem.linkUrl;

NSString *encodedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:encodedPath];

[[UIApplication sharedApplication] openURL:url];