使用 Obj C 和 Xcode 的基本 HTTP 身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2470189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:12:19  来源:igfitidea点击:

Basic HTTP Authentication using Obj C and Xcode

xcodehttpauthentication

提问by Kristiaan

I am hoping someone can help me with a question i have relating to Basic HTTP Authentication in an Xcode 2.3 application.

我希望有人可以帮助我解决与 Xcode 2.3 应用程序中的基本 HTTP 身份验证相关的问题。

Basically i have a PHP page that is on our server and when called with certain parameters returns data relating to the servers condition etc.

基本上我在我们的服务器上有一个 PHP 页面,当使用某些参数调用时,返回与服务器条件等相关的数据。

i have managed to get a small Xcode application working where by it reads the data from the page and populates an NSTextView with the data.

我设法让一个小的 Xcode 应用程序工作,它从页面读取数据并用数据填充 NSTextView。

however to make sure this information stays safe i have setup basic HTTP Authentication on the page, but i cannot work out how to provide the username and password in XCode.

但是为了确保这些信息保持安全,我在页面上设置了基本的 HTTP 身份验证,但我无法弄清楚如何在 XCode 中提供用户名和密码。

here is my code to retrieve the page.

这是我检索页面的代码。

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSString *content = [NSString stringWIthContentsOfUrl:url];
[myTextView setString:content];

i am still at a very basic beginner level regarding xcode and obj c, so nice simple examples of how i go about this would really be appreciated.

我仍然处于关于 xcode 和 obj c 的非常基本的初学者级别,因此非常感谢我如何进行此操作的简单示例。

Thanks Kris

谢谢克里斯

ok following on from the intial comment i got about looking at a website regarding this issue here is whats happened, ive had a look through the webpage supplied and have struggled to get code working in my program.

好的,从最初的评论开始,我看到了关于这个问题的网站,这是发生了什么,我浏览了提供的网页,并且一直在努力让代码在我的程序中工作。

now when i run my program and click the button to-do the login the debug window pops up on this line

现在,当我运行我的程序并单击按钮进行登录时,调试窗口会在这一行弹出

NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@",   
    [NSString stringWithCString:encodedLoginData length:strlen(encodedLoginData)]]; 

this is in the debug log at this point

这在调试日志中

Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/kristiaandavies/getWebData/build/Debug/getWebData.app/Contents/MacOS/getWebData', process 982.
Pending breakpoint 1 - ""AppController.m:10" resolved

i click continue to attempt to get it going further and then get this from the debug log

我点击继续尝试让它更进一步,然后从调试日志中获取它

(gdb) continue
Current language:  auto; currently objective-c
Program received signal:  "EXC_BAD_ACCESS".
(gdb) 

and the program freezes???

程序冻结了???

采纳答案by Tony Eichelberger

Here is a link that may get you started in the right direction.

这是一个链接,可以帮助您朝着正确的方向开始。

http://www.chrisumbel.com/article/basic_authentication_iphone_cocoa_touch

http://www.chrisumbel.com/article/basic_authentication_iphone_cocoa_touch