2011-03-21 77 views
3

我正在尝试记录在我的应用程序中所做的所有URL连接。是否有调试开关或网络活动监视器工具可用于执行此操作?iOS - 跟踪URL连接

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

感谢, 灰

回答

0

在仪器有下的系统工具网络活动监控。我没有亲自使用它,所以我不知道它是否做到了你想要的。

1

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

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

所以,有一两件事你可以做的是继承默认缓存,然后在cachedResponseForRequestNSURLCache方法,你可以跟踪你的请求。

@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]; 

(即0 MemoryCapacity,默认值为512000为DiskCapacity)

现在,当你创建你的控制台一个新的连接

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

你应该看到这样的事情

连接将发送请求url:< NSURLRequest https://stackoverflow.com/>