簡體   English   中英

從iOS App打開Twitter不能正常工作

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM