본문 바로가기

개발도구/iOS - 아이폰 개발

[아이폰] 특수문자 치환 replace - stringByReplacingOccurrencesOfString

아이폰에서 특수문자(' " .. 등등...) 이러한 문자들은 코드값
&#39, &quot.. 등등 이런 코드값을 갖게 됩니다.
그 코드값을 그대로 강용석 '구비언 발언' 이렇게 찍히게 됩니다.
페북이나, 미투데이는 같은 unicode의 값을 url 을 넘겨줘도 잘넘어가는데 twitter 에서만 유독 특수문자를 인식 못하게 되었습니다.
그래서 아래는 xcode에서 제공해주는 특수문자로 치환하여 작업하였습니다.
' , " 의 값은 xcode 뿐만아니라 맥에서 지원해주는 특수기호
command + option + T  


로 대칳소스코드는 아래와 같습니다.

    NSString *temp_title = [article.title stringByReplacingOccurrencesOfString:@"\'" withString:@""];

    temp_title = [temp_title stringByReplacingOccurrencesOfString:@"\"" withString:@""];

    temp_title = [temp_title stringByReplacingOccurrencesOfString:@""" withString:@""];

    temp_title = [temp_title stringByReplacingOccurrencesOfString:@"'" withString:@""];

    NSString *ch_title = [NSString stringWithFormat:@"%@+%@",temp_title,temp_ch];

    NSString *url = [[NSString stringWithFormat:EXPORT_TWITTER, ch_title ,article.link] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

콘솔의 찍힌 값

2012-03-09 11:23:50.718 MTPad for Orientation[210:707] 강용석 구럼비 발언김규리에 독설? 뭐라 했기에

2012-03-09 11:23:50.947 MTPad for Orientation[210:707] tw url: http://mobile.twitter.com/home?status=%EA%B0%95%EC%9A%A9%EC%84%9D%20%E2%80%B2%EA%B5%AC%EB%9F%BC%EB%B9%84%20%EB%B0%9C%EC%96%B8%E2%80%B2%EA%B9%80%EA%B7%9C%EB%A6%AC%EC%97%90%20%EB%8F%85%EC%84%A4?%20%EB%AD%90%EB%9D%BC%20%ED%96%88%EA%B8%B0%EC%97%90%E2%80%A6+%23MToday+http://news.mt.co.kr/mtview.php?no=2012030811105400217&type=1