I am sending a request to UIWebView. The loaded webpage has AJAX calls. I need to analyze AJAX traffic to determine if the user is logged in or not. To do this, I installed NSURLCache in AppDelegate:
MYURLCache *cache = [[MYURLCache alloc] init]; [NSURLCache setSharedURLCache:cache];
This MYURLCache class correctly receives traffic that passes through the webview. I can only complete AJAX calls. Then I create my own AJAX call request. This returns a perfect response from the web server. Thus, the last step to be taken is to create an NSCachedURLResponse return object. I did it too, but the webview just doesn't return anything when returning objects. If I return only zero, WebView will load everything fine (nil is a message to NSURLCache that nothing is cached, so the webview should start loading it on its own).
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request { if ([[[request URL] absoluteString] rangeOfString:@"/ajax/"].location == NSNotFound) { return nil; } else { ASIHTTPRequest *asirequest = [ASIHTTPRequest requestWithURL:[request URL]]; [asirequest setValidatesSecureCertificate:NO]; [asirequest startSynchronous]; NSError *error = [asirequest error]; NSData* data = [[asirequest responseString] dataUsingEncoding:NSUTF8StringEncoding]; // Create the cacheable response NSURLResponse *urlresponse = [[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"application/json" expectedContentLength:[data length] textEncodingName:@"UTF-8"]; NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:urlresponse data:data]; NSLog(@"cachedResponse %@", cachedResponse); NSLog(@"cachedResponse data %@", [[NSString alloc] initWithData:[cachedResponse data] encoding:NSUTF8StringEncoding]); return cachedResponse; } return nil; }
ios objective-c uiwebview nsurlcache
nodepond
source share