【发布时间】:2011-02-15 11:52:53
【问题描述】:
我正在尝试让 Twitter 在我的应用程序中运行,并且一切正常,但代码似乎无法识别来自 Twitter 的错误。如果用户名/密码无效,我会通过此函数收到错误消息:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString* strData = [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"Received data: %@", strData ) ;
return ;
}
它打印:接收到的数据: 无法验证您的身份。 .
但是,该应用程序会继续发布我拥有的推文视图并忽略该错误。显然,我没有正确的设置来检测来自 twitter 的此类错误,所以我的问题是如何让 Xcode 识别这样的错误?顺便说一句,这使用了基本的 http auth,并且没有提及任何关于 OAuth 的内容……只是想让它暂时起作用。
-(void) onLogin:(id)sender
{
[loading1 startAnimating];
NSString *postURL = @"http://twitter.com/account/verify_credentials.xml";
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:postURL ] ];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (!theConnection)
{
UIAlertView* aler = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"Failed to Connect to twitter" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
[aler show];
[aler release];
}
else
{
receivedData = [[NSMutableData data] retain];
}
[request release];
}
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0 && ![challenge proposedCredential])
{
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:txtUsername.text
password:txtPassword.text
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
else
{
isAuthFailed = YES;
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
【问题讨论】:
-
我对互联网几乎一无所知,所以请在这里帮助我
-
这与 Xcode 无关——Xcode 只是一个 IDE——你可能指的是 Objective-C 和 CocoaTouch。
标签: iphone objective-c cocoa-touch xcode twitter