UIWebView捕获响应头

ACP*_*ACP 11 iphone response uiwebview http-headers ios

我搜索/搜索了很多,但无法得到如何捕获HTTP响应头的答案UIWebview.假设我在App Launch上的UIWebview中重定向到用户注册网关(已经处于活动状态),当用户完成注册时,应该通过在HTTP响应标头中传回的注册时分配给用户的成功唯一ID来通知应用程序. .

有没有直接的方法来捕获/打印HTTP响应头UIWebview

Dai*_*jan 19

没有办法从中获取响应对象UIWebView(文件为苹果的bug,id说)

但两个解决方法

1)通过共享的NSURLCache

- (void)viewDidAppear:(BOOL)animated {
    NSURL *u = [NSURL URLWithString:@"http://www.google.de"];
    NSURLRequest *r = [NSURLRequest requestWithURL:u];
    [self.webView loadRequest:r];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSCachedURLResponse *resp = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
    NSLog(@"%@",[(NSHTTPURLResponse*)resp.response allHeaderFields]);
}
@end
Run Code Online (Sandbox Code Playgroud)

如果这适合你,这是理想的


其他

  1. 你可以完全使用NSURLConnection,然后只使用你下载的NSData来提供UIWebView:)

这对你来说是一个糟糕的解决方法!(正如理查德在评论中指出的那样.)它有很大的缺点,你必须看看它是否是你案例中的有效解决方案

NSURL *u = [NSURL URLWithString:@"http://www.google.de"];
NSURLRequest *r = [NSURLRequest requestWithURL:u];
[NSURLConnection sendAsynchronousRequest:r queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *d, NSError *e) {
    [self.webView loadData:d MIMEType:nil textEncodingName:nil baseURL:u];
    NSLog(@"%@", [(NSHTTPURLResponse*)resp allHeaderFields]);
}];
Run Code Online (Sandbox Code Playgroud)


Ric*_*III 8

我喜欢objective-c运行时.你有什么想做但却没有API吗?DM; HR.

好的,更严肃的说,这是解决方案.它将捕获从发起的每个 URL响应CFNetwork,这是UIWebView碰巧在幕后使用的.它还将捕获AJAX请求,图像加载等.

添加过滤器可能就像对标头内容执行正则表达式一样简单.

@implementation NSURLResponse(webViewHack)

static IMP originalImp;

static char *rot13decode(const char *input)
{
    static char output[100];

    char *result = output;

    // rot13 decode the string
    while (*input) {
        if (isalpha(*input))
        {
            int inputCase = isupper(*input) ? 'A' : 'a';

            *result = (((*input - inputCase) + 13) % 26) + inputCase;
        }
        else {
            *result = *input;
        }

        input++;
        result++;
    }

    *result = '\0';
    return output;
}

+(void) load {
    SEL oldSel = sel_getUid(rot13decode("_vavgJvguPSHEYErfcbafr:"));

    Method old = class_getInstanceMethod(self, oldSel);
    Method new = class_getInstanceMethod(self, @selector(__initWithCFURLResponse:));

    originalImp = method_getImplementation(old);
    method_exchangeImplementations(old, new);
}

-(id) __initWithCFURLResponse:(void *) cf {
    if ((self = originalImp(self, _cmd, cf))) {
        printf("-[%s %s]: %s", class_getName([self class]), sel_getName(_cmd), [[[self URL] description] UTF8String]);

        if ([self isKindOfClass:[NSHTTPURLResponse class]])
        {
            printf(" - %s", [[[(NSHTTPURLResponse *) self allHeaderFields] description] UTF8String]);
        }

        printf("\n");
    }

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 使用私人api是苹果不赞成的 (2认同)