Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

track usage of data sent and received through my app

Tags:

ios

ios6

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:

enter image description here

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?

like image 882
user427969 Avatar asked Nov 03 '22 06:11

user427969


2 Answers

There are several Methods depending on which class you are using to Download with AFNetworking

AFHTTPRequestOperation for example has the following method:

setDownloadProgressBlock:^(NSInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead)

For the other way around, there is a method like this:

setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite)

With this to methods you should try to keep track of all your uploaded and downloaded data.

AFJSONRequestOperation is a subclass of AFHTTPRequestOperation, so those method should work in either class.

And please be aware that only sending a "json request" to a webserver doesn't mean that you are not downloading. For sure you have to get the content - the json - which would be your data downloaded.

Further you qre questioning if [requestData length]is telling you the proper size sent to the server. That's not the exact size, because within your requestData you do not have the additional Headers for the HTTP request, therefore your size will be a little bit smaller than the original bytes sent.

Every time one of the methods above is executed you should add the result of bytes read and add it to the bytes you read before this method was executed. For sure you have to save the current bytesRead permanently in coredata for example or any other persistence store.

like image 148
Alexander Avatar answered Nov 15 '22 05:11

Alexander


I used the methods mentioned by Alexander as follows:

AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest: request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // failure
}];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    // bytes received - saved bytesRead variable to NSUserDefaults
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    // bytes sent - saved bytesWritten variable to NSUserDefaults
}];
[operation start];
like image 25
user427969 Avatar answered Nov 15 '22 06:11

user427969