2012-08-17 157 views
0

我必須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

回答

0

檢查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

相關問題