iPhone 上的基本 HTTP 身份验证
我正在尝试运行一个小型 Twitter 客户端,但在测试需要身份验证的 API 调用时遇到了问题。
我的密码中有特殊字符,因此当我尝试使用以下代码时它不起作用。
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 并将身份验证放入标头中。 我发现 Dave Dribin 的关于他的 base64 实现的帖子,这似乎是有道理的。 然而,当我尝试使用它时,编译器开始抱怨它如何找不到 openssl 库。 所以我读到我需要链接 libcrypto 库,但它似乎不存在于 iphone 中。
我还读到有人说苹果不允许应用程序使用加密库,这对我来说没有意义。
所以现在我有点困惑和困惑。 在我的应用程序中进行基本身份验证的最简单方法是什么?
干杯
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];
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.
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?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您也可以直接写下载用户名& 主网址中的密码,即 https://username: [email protected]/
首先,您需要调用 NSURLConnection 委托文件:-
(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
返回是;
;
调用
- (void)连接:(NSURLConnection *)连接 didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)挑战
You can directly aslo write download the username & password in main URL i.e https://username:[email protected]/
First of all you need to call NSURLConnection Delegate file:-
(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return YES;
}
and then call
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
两件事情。 首先,您必须使用异步方法而不是同步/类方法。
身份验证是通过在委托中实现此方法来管理的:
并且您可能还需要实现这些方法:
无论如何,使用异步方法往往会提供更好的用户体验,因此尽管有额外的复杂性,但即使没有能力,也值得这样做进行身份验证。
Two things. Firstly you have to use the async methods rather than the synchronous/class method.
The authentication is managed by implementing this method in your delegate:
And you'll probably also need to implement these methods too:
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.