【问题标题】:http authenitcation in XcodeXcode 中的 http 身份验证
【发布时间】: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


【解决方案1】:

您的委托需要实现...

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

【讨论】:

  • 我的代码中有这个,但还不够,仍然没有点击 else
【解决方案2】:

方法 - (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,一旦关闭基本身份验证,您就无需修补代码。

【讨论】:

  • 我有类似上述帖子的内容以及您发布的内容,但它的 previousfailurecount 仍然不等于 0。
【解决方案3】:

好的,所以我现在可以在 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);
}

【讨论】:

  • 尝试查看... - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 同上。实现它,看看你得到了什么类型的错误。
  • 好的,现在一切正常,只是对如何使其正确失败感到困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 2012-10-27
相关资源
最近更新 更多