0

I am working on a browser application and i want to track how much internet data is consumed when a web page is load. I am using this method to calculate data received by iOS application.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"Did Receive Data %i", [resourceData length]);
length += [resourceData length];
NSLog(@"Data in MB: %.3f",(length/(1024*1024)));
}

I am opening http://www.google.com in iOS simulator and same browser application for android. When page load completes iOS application show 20KB data used but android application shows 280KB data used. In android there is a class to get data used in an active session, that class name is TrafficStats. I search a lot but in iOS, i don't find any class or method that have similar.

I also try this code when webViewDidFinishLoading called to get actual data consumed by webView to load that page.

-(void)webViewDidFinishLoad:(UIWebView *)webView{

NSString* script = @"document.documentElement.outerHTML.length";
length += [[webBrowser stringByEvaluatingJavaScriptFromString:script] floatValue];
NSLog(@"Data in MB: %.3f",(length/(1024*1024)));

//[self performSelectorOnMainThread:@selector(dataUsedByApp) withObject:nil waitUntilDone:5.0];
}

But this is also not accurate because it doesn't calculate the size of images and size is calculated with @"document.documentElement.outerHTML.length".

Please help me on this. :)

Deepak
  • 1,421
  • 1
  • 16
  • 20

2 Answers2

0

Use dataWithContentsOfURL method:

NSData *resultData = [NSData dataWithContentsOfURL:@"https://www.google.co.in/logos/doodles/2014/world-cup-2014-57-5105522332139520.2-hp.gif"];
NSUInteger dataLength = resultData.length;

But here to get the URL for the image, you require to parse the entire HTML which is getting in response by https://www.google.co.in.

Hope this is what you are looking for.

Update:

  • Get html page size :

NSData *resultData = [NSData dataWithContentsOfURL:@"https://www.google.co.in"];

  • Parse all the resource are there in the page. For that you require to do XML parsing of the entire data response. (by converting in to NSString) For that you might require to look into internet.
  • Once you get all the resource URLs like I got for logo.gif, get its NSData length individualy
  • At the end summation of all these is your solution:

PageSize + AllResourcesSize = Entire Page Load Size

Mrunal
  • 13,982
  • 6
  • 52
  • 96
  • This doest work for me because it does not calculate the image size that will load automatically. – Deepak Jul 09 '14 at 11:27
  • suppose you are opening www.google.com In this case NSData *resultData = [NSData dataWithContentsOfURL:@"http://www.google.com"]; resultData does not contains the image size that will show on google page. The above line return only html length. – Deepak Jul 09 '14 at 12:21
  • please try to run you code you will get around 20654 byte which is only 20KB in size. but the actual size of google page is 330KB (as shown in android application). – Deepak Jul 09 '14 at 12:24
  • that is fine. But i want to open http://www.google.com and want to get it's data size in byte. when i used above code it gives only 20KB but same url in android shows page load size 330KB. I am sure this doesn't give me actual size that android application return. NSData *resultData = [NSData dataWithContentsOfURL:@"https://www.google.co.in"]; – Deepak Jul 09 '14 at 12:40
  • be clearer, page size is different then the load size. There might be other resources which are not in page, but coming from somewhere else. So when you get page data (as NSData) it doesn't know about those images used inside it, because those are located at some server location. – Mrunal Jul 09 '14 at 12:46
  • ok, my concern regarding load size. Do you have any help on this? – Deepak Jul 09 '14 at 12:51
0

You can intercept the requests with the opensource WebViewProxy (https://github.com/marcuswestin/WebViewProxy).

I can imagine that this will work if you want to track all http traffic, or you can change the predicate to fit your case:

[WebViewProxy handleRequestsMatching:[NSPredicate predicateWithFormat:@"absoluteString MATCHES[cd] '^http:'"] handler:^(NSURLRequest* req, WVPResponse *res) {
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:req.URL] queue:queue completionHandler:^(NSURLResponse *netRes, NSData *data, NSError *netErr) {
        //do your transfer logging here
        ....
        [res respondWithData:data mimeType:netRes.MIMEType];
    }];
}];
Daniel Alexandrov
  • 1,299
  • 8
  • 13