iPhone 3.2 Base64 编码

发布于 2024-10-11 03:40:55 字数 1540 浏览 2 评论 0原文

我将基本 HTTP 授权标头添加到请求中,但需要将 authString 编码为 Base64。有关信息,由于产生过多的 401 错误,我无法使用 didReceiveAuthenticationChallenge。

下面的代码在 iOS 4.2 中工作正常,但在 iOS 3.2 上不起作用(我想支持这一点)。

<代码> NSString *authString = [[[NSString stringWithFormat:@"%@:%@", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];

authString = [NSString stringWithFormat: @"Basic %@", authString];

NSMutableURLRequest* request =
[[NSMutableURLRequest alloc] initWithURL: url 
                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                         timeoutInterval: 30];

[request setValue: authString forHTTPHeaderField: @"Authorization"];

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

在上面代码的第一行中我收到 NSData 不会响应“base64Encoding”的警告。

所以我从这里下载了自定义类 NSData+Base64: http://cocoawithlove.com/2009/06/base64 -encoding-options-on-mac-and.html

但是......我不知道如何使用这个类来转换我的NSString(authString)。请帮忙?!

我认为以下代码行应该修复: <代码> NSString *authString = [[[NSString stringWithFormat:@"%@:%@", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];

但我收到以下消息:

* 正在终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因:“-[NSConcreteMutableData base64EncodedString]:无法识别的选择器发送到实例

我是否错过了导入或其他内容?

PS这是我在这里提出的第一个问题,所以请放轻松!

I'm adding a Basic HTTP Authorisation Header into a request but need to encode the authString to Base64. For info, I can't use didReceiveAuthenticationChallenge due to excessive 401 errors being produced.

The code below works fine in iOS 4.2 but doesn't work on iOS 3.2 (and I want to support this).


NSString *authString = [[[NSString stringWithFormat:@"%@:%@", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64Encoding];

authString = [NSString stringWithFormat: @"Basic %@", authString];

NSMutableURLRequest* request =
[[NSMutableURLRequest alloc] initWithURL: url 
                             cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                         timeoutInterval: 30];

[request setValue: authString forHTTPHeaderField: @"Authorization"];

connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

In the first line of my code above I get the warning that NSData will not respond to 'base64Encoding'.

So I've downloaded the custom class NSData+Base64 from here:
http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

But......I don't know how to use this class to convert my NSString (authString). Please help?!

I think the following line of code should fix:

NSString *authString = [[[NSString stringWithFormat:@"%@:%@", user, password] dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];

but I get following message:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMutableData base64EncodedString]: unrecognized selector sent to instance

Have I missed an Import or something?

P.S. This is my first question on here, so go easy on me!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め可乐爱微笑 2024-10-18 03:40:55

NSString 有一个 -dataUsingEncoding: 方法,您可以使用该方法将 NSString 实例转换为 NSData 实例。之后就可以使用MG的Base64类别了。

NSString has a -dataUsingEncoding: method which you can use to convert NSString instances to NSData instances. After that, you can use MG's Base64 category.

扎心 2024-10-18 03:40:55

您是否创建了类别以在 NSData 类中使用 BASE64Encoding 方法?在这里,我制作了一个轻松创建类别的教程,这些类别基本上是对现有类的修改,在本例中为 NSData: http://www.donttouchmycode.com/objective-c-class/extending-an-existing-class-with-categories 我希望这可以帮助你。

Have you created the category to use the BASE64Encoding method in your NSData class?? here i made a tutorial to easy create categories, which basically are modifications of an existing class, in this case NSData: http://www.donttouchmycode.com/objective-c-class/extending-an-existing-class-with-categories i hope this can help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文