iPhone Twitter Integration: Validating login.
- by Mr. McPepperNuts
The following code posts to twitter:
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];
}
I am curious as to how I could check if the username and password is correct before proceeding to the above piece of code.
I found the following code in a tutorial, but am unsure how I would implement or call this function.
- (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");
}
}
Any ideas?
Please note, I have taken snippets of code from both of the following tutorials.
http://iphonedevelopertips.com/networking/post-to-a-twitter-account-from-the-iphone.html
and
http://icodeblog.com/2009/07/09/integrating-twitter-into-your-applications/