Fiddler是一款流行的跨平台Web调试工具,近年来,它也逐渐深入到移动端领域。在移动端,Fiddler iOS版作为一款iOS设备调试利器, 弥补了Xcode Instrument的不足,对于iOS端调试问题有着非常重要的意义。
一、安装和启动
Fiddler iOS版的安装非常简单,可以直接在App Store中搜索Fiddler进行安装。进入应用后,点击启动按钮即可调起代理服务。
这时,需要我们将手机的代理设置为Fiddler iOS版的IP地址和端口号。Fiddler默认的端口是8888,可以在设置中进行修改。设置完成后,在iOS设备上打开任意的网络应用,在Fiddler的过滤器中即可看到拦截到的网络请求。
//Fiddler iOS版中启动代理监听的代码
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "FlipsideViewController.h"
#import "FiddlerProxy.h"
#define kShowGuideAfterRunTime 3 // Run timer 3 seconds away from request API again
@implementation AppDelegate {
NSTimer *requestApiTimer; // timer for sending request API every few seconds
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[NSURLProtocol registerClass:[FiddlerProxy class]];
return YES;
}
@end
二、HTTP与HTTPS请求捕获
在iOS端的开发中,我们往往需要通过网络请求与后台进行交互,通过Fiddler iOS版,我们可以抓取HTTP和HTTPS请求,方便我们对网络请求进行监控和分析。
对于HTTPS请求的抓取,需要进行证书的安装和信任,Fiddler iOS版提供了便捷的证书导入功能。在App的设置中,可以进行证书的导入和信任操作。
同时,Fiddler iOS版还提供了WebSocket的捕获和监控,支持对网络请求的拦截以及黑白名单的设置,更加方便我们对网络请求进行调试和追踪。
//HTTPS请求捕获的示例代码
@implementation FiddlerProxy
static NSDictionary *__certificateDictionaries;
+ (NSDictionary *)certificateDictionaries {
if (__certificateDictionaries == nil) {
__certificateDictionaries = @{
@"www.baidu.com" : @"baidu.com.der",
@"www.apple.com" : @"apple.com.der"
};
}
return __certificateDictionaries;
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:@"https"]) {
NSString *filename = [self certificateDictionaries][[url host]];
if (filename) {
[self installCertificate:filename];
return YES;
}
}
return NO;
}
+ (void)installCertificate:(NSString *)filename {
NSData *certificateData = [FiddlerProxy dataWithResource:filename];
if (certificateData) {
CFDataRef cfCertificateData = (__bridge CFDataRef)certificateData;
SecCertificateRef certificate = SecCertificateCreateWithData(NULL, cfCertificateData);
if (certificate!=NULL) {
CFArrayRef certArray = CFArrayCreate(NULL, (void *)&certificate, 1, NULL);
NSDictionary *dict = @{ (__bridge id)kSecImportExportPassphrase: @"fiddler" };
SecPKCS12Import((CFDataRef)[FiddlerProxy dataWithResource:@"fiddler.pfx"],
(__bridge CFDictionaryRef)dict,
&certArray);
}
}
}
@end
三、修改请求内容
在很多场景下,我们需要修改网络请求的请求头或请求体,对于这类场景,Fiddler iOS版提供了非常便捷的方式。我们只需要在Fiddler iOS版中找到对应的请求,进行编辑,即可将请求的内容修改为我们期望的内容。
同时,Fiddler iOS版还支持对请求的链接进行修改,更加方便我们对单个请求的定制化的调试工作。
//修改请求内容的示例代码
- (void)updateRequest:(NSMutableURLRequest *)mutableRequest {
NSString *url = [[mutableRequest URL] absoluteString];
// Rewrite API URL
url = [url stringByReplacingOccurrencesOfString:@"http://example.com/"
withString:@"http://example2.com/"
options:0
range:NSMakeRange(0, url.length)];
[mutableRequest setURL:[NSURL URLWithString:url]];
// Set an Access Token header on every request
NSString *accessToken = @"..."; // Your access token here
[mutableRequest setValue:[@"Bearer " stringByAppendingString:accessToken]
forHTTPHeaderField:@"Authorization"];
}
四、性能优化与缓存管理
在开发过程中,性能优化和缓存管理是我们必须要考虑的问题。Fiddler iOS版提供了PageSpeed Insights功能,可以对移动网站的性能进行分析,并给出相应的优化建议。同时,Fiddler iOS版还提供了丰富的缓存管理功能,可以对缓存进行定制化的设置和管理。
除此之外,Fiddler iOS版还支持网络请求的cookie管理,提高了测试过程的灵活性和准确性。
//缓存管理的示例代码
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
NSCachedURLResponse *cachedResponse = [super cachedResponseForRequest:request];
if (cachedResponse != nil) {
NSMutableData *data = [cachedResponse.data mutableCopy];
NSData *additionalData = [self.additionalCache objectForKey:request.URL.absoluteString];
if (additionalData) {
[data appendData:additionalData];
}
cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response
data:data];
}
return cachedResponse;
}
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse
forRequest:(NSURLRequest *)request {
[super storeCachedResponse:cachedResponse forRequest:request];
NSURL *url = request.URL;
NSData *data = cachedResponse.data;
unsigned long long size = [url.host isEqualToString:@"static.example.com"] ? 1000000 : 50000;
while (self.currentCacheSize + data.length > size && self.cachedRequests.count > 0) {
NSURLRequest *cachedRequest = self.cachedRequests[0];
NSCachedURLResponse *cachedResponse = [self cachedResponseForRequest:cachedRequest];
self.currentCacheSize -= cachedResponse.data.length;
[self.additionalCache removeObjectForKey:cachedRequest.URL.absoluteString];
[self.cachedRequests removeObjectAtIndex:0];
}
[self.additionalCache setObject:data forKey:url.absoluteString];
self.currentCacheSize += data.length;
[self.cachedRequests addObject:request];
}
五、结语
本文简单介绍了Fiddler iOS版的安装和使用,包括HTTP与HTTPS请求捕获、修改请求内容、性能优化与缓存管理等方面。Fiddler iOS版是一款非常实用的网络调试工具,对于iOS端的开发和调试非常重要。我们希望通过本文的介绍,能够帮助读者更加深入的了解Fiddler iOS版,并能够在实际开发中加以应用。