Ios 创建类别,以便一个对象可以成为另一个通常不会';我不能
我有一个Ios 创建类别,以便一个对象可以成为另一个通常不会';我不能,ios,objective-c,uiwebview,Ios,Objective C,Uiwebview,我有一个UIWebView,如果它能成为NSURLConnection的代理,我的生活会简单得多。我做了一个这样的分类 @interface UIWebView (NSURLConnectionDelegate) <NSURLConnectionDelegate> //these methods are used by the NSURLConnection, and are implemented in the .m - (void)connection:(NSURLConne
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认为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;
}
您确定执行该语句时
webview
是非空的(并且是正确的对象)?是的,非常确定,因为该连接位于-(BOOL)webview:(UIWebView*)webview shouldStartLoadWithRequest:(NSURLRequest*)请求导航类型:(UIWebViewNavigationType)navigationType
我很确定我已经使用了类别来实现这样的委托,所以它确实有效。我会尝试再处理一些,可能犯了一个愚蠢的错误,你在哪里保留了对连接的引用以确保它不会被释放?看起来您并没有将其分配给任何对象,所以ARC可能是在调用堆栈中的某个位置取消分配它。
- (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;
}