2014-12-22 4 views
0

다음 코드를 사용하여 내 앱에서 Twitter를 열었습니다. 작동하는 동안 NSString에 공백을 포함 할 수 없습니다.iOS 앱에서 트위터를 열지 못합니다.

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에 공백을 포함시킬 수 있습니까? 두 번째 코드는 "hello world"대신 "hello2orld"를 인쇄합니다. 또한 + 구분 기호를 사용하지 않으려 고 시도했습니다.

+0

을'+'또는'% 20'이 있습니다 공백을 인코딩하는 두 가지 방법. – rmaddy

+0

이 문자열을 시도해야합니다. stringByAddingPercentEscapesUsingEncoding –

답변

1

이 명령으로 20 %로 공간을 변환 할 수 있습니다 : 다른는 NSString 방법을 사용하여이 인코딩을 제거 할 수 있습니다

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

: 사용

– stringByReplacingPercentEscapesUsingEncoding 
+0

감사합니다. 이것은 완벽하게 작동합니다! –

관련 문제