is it possible to install AFNetworking 2.0 with Re

2019-06-15 09:08发布

问题:

I got an news app which fetches news online in XML format in stores it locally in the sqlite dbase.. which is perfect case for restkit.

However, I would like to also fetch xml data manually sometimes (ie and not want to store it in the dbase).. in other words i would like to request it directly with AFNetworking. I like how in AFNetworking 2.0 it does the parsing of xml automatically, and so I want to use that feature.

However, Restkit 2.0 is linked to AFNetworking 1.3.. and so If I add this to my podfile:

pod 'RestKit', '~> 0.21.0'
pod "AFNetworking", "~> 2.0"

and run a pod install I get the following error:

[!] Unable to satisfy the following requirements:
- `AFNetworking (~> 1.3.0)` required by `RestKit/Network (0.21.0)`- `AFNetworking (~> 2.0)` required by `Podfile`

is there anyway around this?

回答1:

In version 1.3.0 you have access to AFXMLRequestOperation which should fulfil the same goal.

It would be a lot of work to update RestKit to use version 2.0, or to rename so that you could use both versions...



回答2:

A quick and dirty solution could be to just rename the files of afnetworking to af1networking and all the classes/interfaces/constants to AF1xxxx. If we only had namespaces in Objective-C or the AFNetworking guys would have prefixed the new version with AF2...

I think what i describe could be done with a project search and replace in hours.

An Alternative would be to use MKNetworkKit for the networking - it is similiar to afnetworking/afnetworking2 but then you end up having 2 different network libraries and AFNetworking seems to have more effort put into.



回答3:

Using a forked version of XMLDictionary, I basically achieved what I wanted to achieve by asking the above question by doing the following:

NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFXMLRequestOperation *operation =
[AFXMLRequestOperation XMLParserRequestOperationWithRequest:request
 success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {

    NSDictionary *dict = [NSDictionary dictionaryWithXMLParser:XMLParser];
    // do stuff with dict        

 failure:failure:^(NSURLRequest *request, NSHTTPURLResponse *response, 
                   NSError *error, NSXMLParser *XMLParser){

    NSLog(@"somethign weng wrong in fetching news data: %@", 
                 [error localizedDescription]);
}];

[operation start];

Credit goes to Wain in his answer's comments :)