tags:

views:

342

answers:

2

I'm writing an iPhone App that might potentially send/receive a lot of data over time, depending on polling intervals.

I would like to display the amount of sent/received bytes on runtime, and maybe alert the user when reaching a user-specified threshold. Any tips on how to track the amount of sent/received bytes within my app? (I'm only interested in the amount of data my app sends/receives while running).

+2  A: 

If you are using NSURLconnection then you can keep track of the data sent inside the - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data delegate method.

Each time this message in sent, query the size of data and add it to an instance variable holding the total.

Other frameworks such as the ASI requests have specific delegate messages to update progress.

Roger Nolan
+1  A: 

Displaying bytes, or even KB, will be somewhat misleading. I'd use Roger's technique and round your total to the nearest 0.1MB.

Strictly speaking, you can't precisely measure the data sent and received over the wire. In addition to the bytes you can measure, there'll be some TCP overhead plus any retransmits due to connection problems.

Even if Apple makes its global counters available, you won't be able to distinguish between your bytes and Apple's background applications' bytes (e.g. push mail).

Garth T Kidd