【发布时间】:2015-02-20 04:44:36
【问题描述】:
我一直在使用以下代码在我的应用程序中打开 Twitter。虽然它有效,但我似乎无法在 NSString 中嵌入空格。
NSString *string1 = [NSString stringWithFormat:@"twitter://post?message=hello+world"];
NSString *string2 = [NSString stringWithFormat:@"http://twitter.com/intent/tweet?text=hello%20world"];
NSURL *twitterURL = [NSURL URLWithString:string1];
NSURL *mobileURL = [NSURL URLWithString:string2];
if ([[UIApplication sharedApplication] canOpenURL:twitterURL])
[[UIApplication sharedApplication] openURL:twitterURL];
else
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mobileURL]];
这两个部分的代码都可以在预期的情况下工作,但是如何在 NSURL 中嵌入空格?第二段代码打印“hello2orld”而不是“hello world”。我也尝试了 + 分隔符无济于事。
【问题讨论】:
-
使用
+或%20是编码空格的两种方式。 -
你应该试试这个stringByAddingPercentEscapesUsingEncoding
标签: ios objective-c iphone twitter