track usage of data sent and received through my app

别来无恙 提交于 2019-12-06 02:12:16

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/14450656/track-usage-of-data-sent-and-received-through-my-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!