小编Pau*_*ing的帖子

使用AFNetworking和HTTP基本身份验证

以下代码成功连接到我的Ruby on Rails API并使用AFNetworking返回JSON.我需要做什么来编辑它以传递用户名和密码,以便我的API可以使用HTTP基本身份验证?

我已经阅读了他们的文档,但我是Objective-C和AFNetworking的新手,目前还没有意义.

NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:3000/tasks.json"];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                     JSONRequestOperationWithRequest:request
                                     success:^(NSURLRequest *request
                                     , NSHTTPURLResponse *response
                                     , id JSON) {

    self.tasks = [JSON objectForKey:@"results"];
    [self.activityIndicatorView stopAnimating];
    [self.tableView setHidden:NO];
    [self.tableView reloadData];

    NSLog(@"JSON");

} failure:^(NSURLRequest *request
                , NSHTTPURLResponse *response
                , NSError *error
                , id JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];

[operation start];
Run Code Online (Sandbox Code Playgroud)

iphone json objective-c nsurlrequest afnetworking

20
推荐指数
1
解决办法
2万
查看次数

标签 统计

afnetworking ×1

iphone ×1

json ×1

nsurlrequest ×1

objective-c ×1