控制返回之前调用委托方法的NSURLConnection
问题描述:
我必须A和B类。我的B类具有与服务器连接的方法。我从类A调用此方法。我已经实现了具有检查连接函数的NSURLConnection.class A的委托,该函数将返回类型设置为BOOL。我从这个方法调用类B的平方法如下: A类:控制返回之前调用委托方法的NSURLConnection
-(BOOL)checkConnectivity:(Server *)newServer
{
[b ping];
return FALSE;
}
B类:
-(void)ping
{
NSURL *url=[[NSURL alloc]initWithString:@"my url"];
request=[NSMutableURLRequest requestWithURL:url];
connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
if(connection)
{
webData=[NSMutableData data];
}
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data
{
[webData appendData:data];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Finished loading");
}
没有与URL没有问题。那么最新错误?为什么委托方法没有调用?
答
检查u必须添加了这个:
@interface yourViewController : UIViewController <NSURLConnectionDelegate>
现在做这在你的Ping方式:
NSURLConnection * connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[connection start]
你检查,看看是否连接不为零? – rdelmar