控制返回之前调用委托方法的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没有问题。那么最新错误?为什么委托方法没有调用?

+0

你检查,看看是否连接不为零? – rdelmar

检查u必须添加了这个:

@interface yourViewController : UIViewController <NSURLConnectionDelegate> 

现在做这在你的Ping方式:

NSURLConnection * connection = [[NSURLConnection alloc] 
          initWithRequest:request 
            delegate:self startImmediately:NO]; 

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
        forMode:NSDefaultRunLoopMode]; 
[connection start] 
+0

我相信这也能起作用,但这不是解决问题的答案。我复制并粘贴了他的代码,并且它工作正常。无论他的URL有什么问题,或者他的代码中还有别的东西,他没有显示这是导致问题的原因。 – rdelmar

+0

没有我的网址工作正常。是否有任何问题,因为我从checkConnection方法返回? –

+0

@VXtreme,你有没有检查连接是否为非零,就像我在我的评论中问的那样? – rdelmar