iOS – 跟踪URL连接
我目前正在尝试logging在我的应用程序中所做的所有URL连接。 有没有一个debugging开关,或我可以使用networking活动监测工具来做到这一点?
或者,我唯一的select是在源代码中包含NSLog语句?
谢谢,Ash
您的应用程序的所有NSURLConnections
默认使用共享caching类
所以,你可以做的一件事是子类的默认caching,然后在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
方法中,将共享caching设置为caching的实例。
CustomNSURLCache *customCache = [[CustomNSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:51200 diskPath:nil]; [NSURLCache setSharedURLCache:customCache]; [customCache release];
(作为MemoryCapacity的默认值和DiskCapacity的512000)
现在当你创build一个新的连接
NSURLRequest *request1 = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]]; [[NSURLConnection alloc] initWithRequest:request1 delegate:self];
你应该在你的控制台看到这样的东西
连接将发送请求的URL:<NSURLRequest https://stackoverflow.com/ >
在仪器中,系统仪器下有一个networking活动监视器。 我没有亲自使用它,所以我不知道它是否做到了你想要的。