Track usage of data sent and received through my application

How to track the use of data sent and received through my application?

I just want to write the bytes sent and received when my application is running. If I can get separate information for Wi-Fi and cellular network, then it will be great, but this is not a priority.

I know how to find full use of the device - stack overflow site/questions/49051 / ...

In addition, I know that I can use tools to collect data about network activity, but I want to write this data to my application, so you need a programmatic way to do this.

I tried to search, but all I find is the use of the device’s network, not the specific use of the application.

Below is a screenshot of the Whatsapp Settings → Usage settings page, which will give a better idea of ​​what I'm trying to do:

enter image description here

I use AFNetworkingfor the 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 updated my question.

Can someone answer: is there a [requestData length]number of SEND bytes for one request?

+5
source share
2 answers

There are several methods, depending on which class you use to load with AFNetworking

AFHTTPRequestOperation, for example, has the following method:

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

For another method, there is such a method:

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

With this method, you should try to keep track of all downloaded and uploaded data.

AFJSONRequestOperationis a subclass AFHTTPRequestOperation, so this method should work in any class.

, "json-" - , . , - json - .

, [requestData length] , . , requestData HTTP-, , .

, , , , . , bytesRead coredata, , .

+1

, , :

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];
+1

All Articles