iPhone上的基本HTTP身份验证 [英] Basic HTTP Authentication on iPhone

查看:205
本文介绍了iPhone上的基本HTTP身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行一个小型的Twitter客户端,在测试需要身份验证的API调用时遇到了问题。

I'm trying to get a small twitter client running and I ran into a problem when testing API calls that require authentication.

我的密码中有特殊字符,所以当我尝试使用以下代码时,它不起作用。

My password has special characters in it, so when I try to use the following code it doesn't work.

NSString *post = [NSString stringWithFormat:@"status=%@", [status stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%@@%@/statuses/update.json", username, password, TwitterHostname]];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

我开始研究base64并将身份验证放入标头中。我在他的base64实现中发现了 Dave Dribin 的帖子,它似乎有道理。但是,当我尝试使用它时,编译器开始抱怨它无法找到openssl库。所以我读到我需要在libcrypto库中链接,但它似乎不适用于iphone。

I started looking into base64 and putting the authentication into the headers. I found Dave Dribin's post on his base64 implementation, and it seemed to make sense. However when I tried to use it the compiler started complaining about how it couldn't find the openssl libraries. So I read that I needed to link in the libcrypto library, but it doesn't seem to exist for iphone.

我也读过有人说苹果赢了' t允许使用加密库的应用程序对我来说没有意义。

I've also read people saying that apple won't allow apps that use crypto libraries, which doesn't make sense to me.

所以现在我有点困惑和困惑。在我的应用程序中获取基本身份验证的最简单方法是什么?

So now I'm kinda stuck and confused. What's the easiest way to get basic authentication into my app?

干杯

推荐答案

两件事。首先,你必须使用异步方法而不是同步/类方法。

Two things. Firstly you have to use the async methods rather than the synchronous/class method.

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:req]
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval:30.0];

// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

通过在您的委托中实施此方法来管理身份验证:

The authentication is managed by implementing this method in your delegate:

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

您可能还需要实施这些方法:

And you'll probably also need to implement these methods too:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

使用async方法往往会提供更好的用户体验,所以尽管额外的复杂性值得做甚至无法进行身份验证。

Using the async method tends to give a better user experience anyway so despite the extra complexity is worth doing even without the ability to do authentication.

这篇关于iPhone上的基本HTTP身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆