AFNetworking 2.0 Plist

两盒软妹~` 提交于 2020-01-14 06:59:31

问题


I want to update my App with the latest AFNetworking version 2. Since now they have changed some things I was wondering how to download plist files.

I found this example in the documentation:

NSURL *URL = [NSURL URLWithString:@"http://example.com/foo.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                     initWithRequest:request];
operation.responseSerializer = [AFJSONSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:nil];
[operation start];

But I need to download a plist what I did with AFNetworking 1 like this:

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/test.plist"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
    AFPropertyListRequestOperation *operation = [AFPropertyListRequestOperation propertyListRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id propertyList) {

        NSDictionary *myTempDic = (NSDictionary *)propertyList;
        myArray = [myTempDic objectForKey:@"Whatever"];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id propertyList) {
//do something with the error
}];
[operation start];

Where can I find any example about handling Plist with AFNetworking 2.0?

I have found this way to it. Is this correct?

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFPropertyListResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id propertyList) {

    NSDictionary *myTempDic = (NSDictionary *)propertyList;
    myArray = [myTempDic objectForKey:@"Whatever"];

    }failure:nil];

    [operation start];

回答1:


I was just doing the AFnetworking tutorial on http://www.raywenderlich.com/30445/, and had the same problem going from 1.0 to 2.0

I am fairly new but this is a solution i found to work:

 NSString *url = @"http://example.com/foo.plist";

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

 manager.responseSerializer = [AFPropertyListResponseSerializer serializer];

 [manager GET:url 
   parameters:nil 
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          myArray = [responseObject objectForKey:@"Whatever"]; //responseObject is a dictionary
          NSLog(@"PLIST: %@", responseObject);
      } 
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];


来源:https://stackoverflow.com/questions/19631466/afnetworking-2-0-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!