[英]iPhone Twitter Integration: Validating login
Update: 更新:
Using the following gets back an XML response. 使用以下代码可以返回XML响应。 Used NSXMLParser to check for "errors" element.
使用NSXMLParser检查“错误”元素。 Not the cleanest method but gets the job done.
不是最干净的方法,但是可以完成工作。 I am open for suggestions.
我愿意征求意见。
NSURLResponse *response;
NSError *error;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *resultString = [[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding];
NSLog(@"myResult: %@", resultString);
Original Post: 原始帖子:
The following code posts to twitter: 以下代码发布到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://iosdevelopertips.com/networking/post-to-a-twitter-account-from-the-iphone.html http://iosdevelopertips.com/networking/post-to-a-twitter-account-from-the-iphone.html
and 和
http://icodeblog.com/2009/07/09/integrating-twitter-into-your-applications/ http://icodeblog.com/2009/07/09/integrating-twitter-into-your-applications/
您可以通过对http://twitter.com/account/verify_credentials.xml或http://api.twitter.com/1/account/verify_credentials.xml进行身份验证的请求来检查用户名和登录名是否正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.