How to track usage of data sent and received through my app?
I just want to record bytes sent and received when my app is running. If I can get separate info for Wifi and Cellular network then it would be great but its not a priority.
I know how to find total usage of device - https://stackoverflow.com/a/8014012/427969
Also, I know I can use Instruments to collect network activity data but I want to record this data in my app so need a programmatic way to do this.
I tried to search this but all I find is device's network usage and not a particular app's usage.
The following is the screenshot of Whatsapp's Settings -> Usage page, which would give a better idea of what I am trying to do:
I am using AFNetworking
for HTTP request and response as follows:
NSData* requestData = [NSJSONSerialization dataWithJSONObject:info options: NSJSONWritingPrettyPrinted error:&error];
if(error != nil) {
NSLog(@"Error: converting JSON: %@, %@", error, error.userInfo);
}
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
/*
################### ----------------- ###################
WILL [requestData length] BE THE NUMBER OF BYTES SEND ???
################### ----------------- ###################
*/
NSLog(@"data bytes: %d", [requestData length]);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL .....];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest: request
success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
} failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
[operation start];
I have updated my question.
Can somebody please answer: Is [requestData length]
the number of bytes SEND for one request?