【发布时间】:2011-05-08 23:17:10
【问题描述】:
我目前正在尝试记录在我的应用中建立的所有 URL 连接。我可以使用调试开关或网络活动监控工具来执行此操作吗?
或者我唯一的选择是在整个源代码库中包含 NSLog 语句?
谢谢, 灰
【问题讨论】:
标签: ios
我目前正在尝试记录在我的应用中建立的所有 URL 连接。我可以使用调试开关或网络活动监控工具来执行此操作吗?
或者我唯一的选择是在整个源代码库中包含 NSLog 语句?
谢谢, 灰
【问题讨论】:
标签: ios
您的应用程序的所有NSURLConnections 都默认使用共享缓存类
因此,您可以做的一件事是将默认缓存子类化,然后在 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
在您的AppDelegatedidFinishLaunchingWithOptions方法中,将共享缓存设置为您的缓存实例。
CustomNSURLCache *customCache = [[CustomNSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:51200 diskPath:nil];
[NSURLCache setSharedURLCache:customCache];
[customCache release];
(MemoryCapacity 的默认值为 0,DiskCapacity 的默认值为 512000)
现在当你创建一个新连接时
NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
[[NSURLConnection alloc] initWithRequest:request1 delegate:self];
您应该在控制台上看到类似的内容
connection 将发送对 url 的请求:
https://stackoverflow.com/>
【讨论】:
在 Instruments 的 System Instruments 下有一个网络活动监视器。虽然我没有亲自使用过,所以我不知道它是否符合您的要求。
【讨论】: