iOS - 跟踪URL连接

时间:2011-03-21 18:04:44

标签: ios

我目前正在尝试记录在我的应用中创建的所有网址连接。是否有可用于执行此操作的调试开关或网络活动监视工具?

或者是我在整个源代码库中包含NSLog语句的唯一选择吗?

谢谢, 灰

2 个答案:

答案 0 :(得分:1)

您的应用程序的所有NSURLConnections都使用默认的共享缓存类

  

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

因此,您可以做的一件事是对默认缓存进行子类化,然后在cachedResponseForRequest NSURLCache方法中,您可以跟踪您的请求。

@interface CustomNSURLCache : NSURLCache {

}

@end


@implementation CustomNSURLCache


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

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

    return [super cachedResponseForRequest:request];

}

@end

使用AppDelegate didFinishLaunchingWithOptions方法,将共享缓存设置为缓存实例。

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

(0表示MemoryCapacity的默认值,512000表示DiskCapacity)

现在,当您创建新连接时

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

你应该在你的控制台上看到类似的东西

  

连接会发送网址请求:< NSURLRequest https://stackoverflow.com/>

答案 1 :(得分:0)

在仪器中,系统仪器下有一个网络活动监视器。我没有亲自使用它,所以我不知道它是否符合你的要求。