2010-05-04 74 views
0

我想在我的应用程序中使Twitter工作,一切正常,但代码似乎无法识别来自Twitter的错误。如果用户名/密码无效,我通过这个功能得到一条错误消息:http验证在Xcode

- (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 ; 
} 

它打印:接收到的数据: 无法验证您的身份。 。

但是,应用程序继续发布我的推文视图并忽略错误。很明显,我没有设置正确的东西来检测这样的错误,所以我的问题是如何让Xcode识别这样的错误?这使用基本的HTTP身份验证btw,并没有提及有关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]; 
     } 
    } 
+0

我知道旁边没有关于互联网的东西,所以请尽量帮我在这里 – fraggleRockz 2010-05-04 14:14:57

+0

这有什么好做的Xcode - Xcode的只是一个IDE - 你可能意味着Objective-C和CocoaTouch。 – 2010-05-04 15:24:50

回答

0

好吧,现在我可以在xml中获得错误响应或身份验证响应。现在,我怎么说,使用此响应作为connectionDidFailWithError的触发因素,因为如果Twitter发送给我一个错误,我想给出一个应用内错误。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
[receivedData appendData:data]; 

NSXMLParser* parser = [[NSXMLParser alloc] initWithData:data]; 
[parser setDelegate:self]; 
[parser parse]; 

return; 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string 
{ 
NSLog(@"response: %@", string); 
} 
+0

尝试寻找...(NSURLConnection *)连接didFailWithError:(NSError *)错误 与上面相同。实现它并查看返回的错误类型。 – Staros 2010-05-04 21:36:00

+0

好吧,现在所有的工作,只是困惑如何使其失败正确。 – fraggleRockz 2010-05-05 14:46:47

0

方法- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data被称为零次或多次,具体取决于有多少数据可用。

在网络请求的情况下,它可能会被多次调用。你需要做的是创建一个NSMutableData对象并将其作为实例变量存储。然后,当调用此方法需要将新数据添加到您的NSMutableData对象,像这样:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    //myMutableData should be an instance of NSMutableData and stored as an instance variable in your class. Don't forget to initialise it. 
    [myMutableData appendData:data]; 
} 

如果你不这样做,你可能会错过响应的部分。

我不确切知道如何执行请求或返回什么样的响应,但理想情况下,您需要使用XML或JSON格式的响应,并且应该使用解析器来响应变成你可以使用的东西。

这样,你将能够提取错误代码,消息等,并相应地采取行动。最后,如果你打算从应用程序中使用Twitter,可以考虑使用像MGTwitterEngine这样的预建库。它会为你节省很多的麻烦和麻烦。另外,尽管你不提及它,但是MGTwitterEngine现在也支持OAuth,这样一旦基本认证被关闭,这将节省你不得不修补你的代码。

+0

我有类似于上面的帖子和你发布的以及它仍然从来没有previousfailurecount等于除0之外的任何东西。 – fraggleRockz 2010-05-04 15:12:27

3

你的委托需要实现...

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 

这理应得到调用,如果你试图连接到服务器要求身份验证。这里是我的程序的一些示例代码...

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
     if ([challenge previousFailureCount] == 0) { 
      NSURLCredential *newCredential; 
      newCredential=[NSURLCredential credentialWithUser:_username 
             password:_password 
             persistence:NSURLCredentialPersistenceNone]; 
      [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
     } else { 
      [[challenge sender] cancelAuthenticationChallenge:challenge]; 
      NSLog(@"Bad Username Or Password"); 
     } 
    } 
} 
+0

我有这在我的代码,它是不够的,仍然没有打其他人 – fraggleRockz 2010-05-04 15:24:10