跟踪通过我的应用发送和接收的数据的使用情况 [英] track usage of data sent and received through my app

查看:147
本文介绍了跟踪通过我的应用发送和接收的数据的使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何跟踪通过我的应用发送和接收的数据的使用情况?

How to track usage of data sent and received through my app?

我只想记录我的应用程序运行时发送和接收的字节数。如果我可以获得Wifi和蜂窝网络的单独信息,那么它会很棒但不是优先考虑。

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.

我知道如何找到设备的总使用量 - https://stackoverflow.com/a/8014012/427969

I know how to find total usage of device - https://stackoverflow.com/a/8014012/427969

另外,我知道我可以使用Instruments收集网络活动数据,但我想在我的应用程序中记录这些数据,因此需要以编程方式执行此操作。

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.

以下是Whatsapp的设置 - >使用情况页面的屏幕截图,它可以更好地了解我想要做的事情:

The following is the screenshot of Whatsapp's Settings -> Usage page, which would give a better idea of what I am trying to do:

我正在使用 AFNetworking 获取HTTP请求和响应,如下所示:

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.

有人可以回答: [requestData length] 一个请求的字节数SEND?

Can somebody please answer: Is [requestData length] the number of bytes SEND for one request?

推荐答案

根据您使用 AFNetworking 下载的类,有几种方法可供选择

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

AFHTTPRequestOperation 例如有以下方法:

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 AFHTTPRequestOperation 的子类,所以那些方法应该在任何一个类中工作。

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

请注意,只向网络服务器发送json请求并不意味着你没有下载。当然你必须得到内容 - json - 这将是你的数据下载。

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.

此外你还在质疑 [requestData length] 告诉你发送到服务器的正确大小。这不是确切的大小,因为在您的requestData中,您没有HTTP请求的额外标头,因此您的大小将比发送的原始字节小一点。

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.

每次执行上述某个方法时,您应该添加读取的字节结果,并将其添加到执行此方法之前读取的字节数。当然,您必须将当前的bytesRead永久保存在coredata中或任何其他持久性存储中。

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.

这篇关于跟踪通过我的应用发送和接收的数据的使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆