【问题标题】:Open Twitter from iOS App Not Working Correctly从 iOS 应用程序打开 Twitter 无法正常工作
【发布时间】: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


【解决方案1】:

您可以使用以下指令将空格转换为 %20:

NSString *string1 = [NSString stringWithFormat:@"twitter://post?message=hello world"];
string1 = [string1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

您也可以使用其他 NSString 方法删除此编码:

– stringByReplacingPercentEscapesUsingEncoding

【讨论】:

    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 2012-01-02
    相关资源
    最近更新 更多