본문 바로가기

iOS Programming

아이폰 sdk 로 간단한 트윗 날리기

- (BOOL)sendTwit
{
NSString *unpw = [[NSString stringWithFormat:@"%@:%@", 아이디, 비밀번호] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *tweet = [NSString stringWithFormat:@"%@", 트윗내용];
tweet = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)tweet, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 );
NSString *body = [NSString stringWithFormat:@"source=LeadingWatch&status=%@", tweet];
// Establish the Twitter API request
id baseurl = [NSString stringWithFormat:@"http://%@@twitter.com/statuses/update.xml", unpw];
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
if (!urlRequest)
return NO;
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[urlRequest setValue:@"LeadingWatch" forHTTPHeaderField:@"X-Twitter-Client"];
NSError *error;
NSURLResponse *response;
NSData* result = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
if (!result)
return NO;
NSString *outstring = [[[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding] autorelease];
if ([outstring rangeOfString:@"uthentica"].location != NSNotFound)
return NO;
return YES;
}