我有一个UIWebView
,如果它可以成为一个NSURLConnection
. 我做了一个这样的类别
@interface UIWebView (NSURLConnectionDelegate) <NSURLConnectionDelegate>
//these methods are used by the NSURLConnection, and are implemented in the .m
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
@end
实现中的方法确实有效,因为我之前有另一个视图是委托,但现在由于某些原因我需要更改它。
我像这样给NSURLConnection
一个代表
#import "UIWebView+NSURLConnectionDelegate.h"
[[NSURLConnection alloc] initWithRequest:request delegate:webview]; //used to be self but now i need the webview to know about its own connection because there are multiple webviews
但是当它是这样的类别时,没有一个委托方法被调用。
以前有没有人做过这样的事情,或者这不起作用,因为 NSURLConnection 认为这webview
实际上不是委托或其他什么?
编辑以显示更多代码:
- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"shouldStartLoadWithRequest %@ %d", request.URL.absoluteURL.description, navigationType);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if (!authed) {
authed = NO; //gets set to yes when delegate methods work (also are some print outs in the delegate methods which are not printing at all)
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:webview];
return NO;
}
return YES;
}