[英]Open Twitter from iOS App Not Working Correctly
我一直在使用以下代码在我的应用程序中打开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:
NSString *string1 = [NSString stringWithFormat:@"twitter://post?message=hello world"];
string1 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
您也可以使用其他NSString方法删除此编码:
– stringByReplacingPercentEscapesUsingEncoding
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.