为什么 NSURLConnection 的代理方法不执行?最简单的实现都不起作用!!
我新建了控制台工程,建立了一个RequestSender类,该类的代码如下:(1)头文件:#import<Foundation/Foundation.h>@interfa...
我新建了控制台工程,建立了一个RequestSender类,该类的代码如下:
(1)头文件:
#import <Foundation/Foundation.h>
@interface RequestSender : NSObject <NSURLConnectionDelegate> {
NSMutableData* d;
}
@end
(2)实现文件
#import "RequestSender.h"
@implementation RequestSender
- (id)init {
self = [super init];
if (self) {
NSString* s = @"http://www.apple.com/";
NSURL* u = [[NSURL alloc] initWithString:s];
NSURLRequest* r = [[NSURLRequest alloc] initWithURL:u];
NSURLConnection* c = [[NSURLConnection alloc] initWithRequest:r delegate:self];
d = [NSMutableData data];
return self;
} else {
return nil;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[d setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[d appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
}
@end
(3)main 函数:
RequestSender* rs = [[RequestSender alloc] init]; // 就这么一句
我在那几个代理方法下了断电,发现一个都没被调用!这怎么解决?都是主线程啊,不存在 run loop 的问题啊。
回答得好定重谢!! 展开
(1)头文件:
#import <Foundation/Foundation.h>
@interface RequestSender : NSObject <NSURLConnectionDelegate> {
NSMutableData* d;
}
@end
(2)实现文件
#import "RequestSender.h"
@implementation RequestSender
- (id)init {
self = [super init];
if (self) {
NSString* s = @"http://www.apple.com/";
NSURL* u = [[NSURL alloc] initWithString:s];
NSURLRequest* r = [[NSURLRequest alloc] initWithURL:u];
NSURLConnection* c = [[NSURLConnection alloc] initWithRequest:r delegate:self];
d = [NSMutableData data];
return self;
} else {
return nil;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[d setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[d appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
}
@end
(3)main 函数:
RequestSender* rs = [[RequestSender alloc] init]; // 就这么一句
我在那几个代理方法下了断电,发现一个都没被调用!这怎么解决?都是主线程啊,不存在 run loop 的问题啊。
回答得好定重谢!! 展开
Storm代理
2023-05-09 广告
2023-05-09 广告
StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、账密提取,IP+端口提取形式,不限...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询