I have to implement a version checker in my app. The app will check the version (which was manually set) from the server. Thing is, the server return the response in neither JSON
or XML
. It returns in plain text/html
, which I'm not familiar with.
eg. If the app need to be updated, it will return response as below.
<checkversion>
<update>TRUE</update>
<status>MINOR</status>
<alert></alert>
<version>1.0.0</version>
<ituneslink>0</ituneslink>
</checkversion>
Else, it will return response as below.
<checkversion>
<update>FALSE</update>
</checkversion>
After looking at some similar SO posts such as this and this, I manage to get the response in format of string
. Below is the snippets of my code.
- (void)slurpVersionChecker
{
NSString *versionURL = @"api";
NSDictionary *params = @{@"api_key":@"key"};
AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
[requestManager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
[requestManager POST:versionURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"version: %@", string);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"failed to check version: %@", error);
}];
}
Question is, how do I parse each of the responses? and is it there any way where I can parse the response through responseObject
directly without converting it to String
?