Cairo
Cairo

Reputation: 341

iOS - Track URL connections

I'm currently trying to log all URL connections made in my app. Is there a debug switch, or network activity monitor tool I can use to do this?

Or is my only alternative to include NSLog statements throughout the source base?

Thanks, Ash

Upvotes: 3

Views: 2011

Answers (2)

Felipe Sabino
Felipe Sabino

Reputation: 18215

All NSURLConnections of your application use a shared cache class for default

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLCache_Class/Reference/Reference.html

So, one thing you could do is subclass the default cache, and then at the cachedResponseForRequest NSURLCache method, you can to track you requests.

@interface CustomNSURLCache : NSURLCache {

}

@end


@implementation CustomNSURLCache


-(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {

    NSLog(@"connection will send request for url: %@", request);

    return [super cachedResponseForRequest:request];

}

@end

At your AppDelegate didFinishLaunchingWithOptionsmethod, set the shared cache to an instance of your cache.

CustomNSURLCache *customCache = [[CustomNSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:51200 diskPath:nil];
[NSURLCache setSharedURLCache:customCache];
[customCache release];

(being 0 the default value for MemoryCapacity and 512000 for DiskCapacity)

Now when you create a new connection

NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
[[NSURLConnection alloc] initWithRequest:request1 delegate:self];

you should see something like this at your console

connection will send request for url: <NSURLRequest https://stackoverflow.com/>

Upvotes: 1

Patrick
Patrick

Reputation: 109

In Instruments there is a Network Activity Monitor under the System Instruments. I have not personally used it though so I don't know if it does what you want.

Upvotes: 0

Related Questions