Обновление:iPhone Twitter Интеграция: Проверка входа
Использование следующего возвращает ответ XML. Используется NSXMLParser для проверки элемента «errors». Не самый чистый метод, но выполняет свою работу. Я открыт для предложений.
NSURLResponse *response;
NSError *error;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *resultString = [[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding];
NSLog(@"myResult: %@", resultString);
Оригинал сообщения:
Следующие сообщения код в твиттер:
NSString *compoundLoginString = [NSString stringWithFormat:@"http://%@:%@@twitter.com/statuses/update.xml",extractedUsername, extractedPassword];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
[NSURL URLWithString:compoundLoginString]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
// The text to post
NSString *msg = tweetText;
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[[NSString stringWithFormat:@"status=%@", msg]
dataUsingEncoding:NSASCIIStringEncoding]];
NSURLResponse *response;
NSError *error;
if ([NSURLConnection sendSynchronousRequest:request
returningResponse:&response error:&error] != nil){
[self postSuccessfulAlert];
}else{
[self postNotSuccessfulAlert];
}
Я интересно, как я могу проверить, если имя пользователя и пароль правильно, прежде чем перейти к выше части кода.
В учебнике я нашел следующий код, но не уверен, как реализовать или вызвать эту функцию.
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:[self username]
password:[self password]
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
// inform the user that the user name and password
// in the preferences are incorrect
NSLog(@"Invalid Username or Password");
}
}
Любые идеи?
Обратите внимание, что я взял фрагменты кода из обоих следующих руководств.
http://iosdevelopertips.com/networking/post-to-a-twitter-account-from-the-iphone.html
и
http://icodeblog.com/2009/07/09/integrating-twitter-into-your-applications/
Не скоро ли они устареют от простой авторизации? Вместо этого вы должны использовать oauth. Googling «twitter oauth iphone» дает множество хитов. – Rup