iOS - 跟踪URL连接 [英] iOS - Track URL connections

查看:84
本文介绍了iOS - 跟踪URL连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

或者是我在整个源基础中包含NSLog语句的唯一替代方法?

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

谢谢,
Ash

Thanks, Ash

推荐答案

全部 NSURLConnections 使用默认的共享缓存类

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

所以,有一件事你可以做的是将默认缓存子类化,然后在 cachedResponseForRequest NSURLCache 方法中,您可以跟踪您的请求。 / p>

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

在你的 AppDelegate didFinishLaunchingWithOptions 方法,将共享缓存设置为缓存实例。

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

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

(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


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

这篇关于iOS - 跟踪URL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆