2013-09-22 54 views
0

我想下载一些网页,但这个例子代码似乎不起作用。 它打印“开始下载”,然后退出,为什么委托方法不会被执行? 示例代码有什么问题? 感谢这个简单的代码(目标c)有什么问题?

main.m 

#import <Foundation/Foundation.h> 
#import "Test.h" 

int main(int argc, const char * argv[]) 
{ 

    @autoreleasepool { 
     Test * test = [[Test alloc]init]; 
     [test downloadData]; 

    } 
    [NSThread sleepForTimeInterval:21.0f]; 

    return 0; 
} 

Test.h 


#import <Foundation/Foundation.h> 

@interface Test : NSObject <NSURLConnectionDelegate,NSURLConnectionDataDelegate,NSURLConnectionDownloadDelegate> 
@property (retain) NSMutableData * receivedData; 
@property (retain) NSURLConnection * theConnection; 
- (void) downloadData; 
@end 

Test.m 

#import "Test.h" 

@implementation Test 
- (void) downloadData 
{ 
    NSURLRequest *theRequest= 
     [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.sf.net/"]         
         cachePolicy:NSURLRequestUseProtocolCachePolicy 
        timeoutInterval:60.0]; 

    _receivedData = [NSMutableData dataWithCapacity: 0]; 



    [NSURLConnection sendSynchronousRequest:theRequest 
          returningResponse:nil 
             error:nil]; 
    NSLog(@"begin download"); 

    if (!_theConnection) { 

     _receivedData = nil; 

     // Inform the user that the connection failed. 

    } 
} 

enter code here 

#pragma mark - 
#pragma mark NSURLConnectionDataDelegateenter code here methods 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 

{ 
    NSLog(@"1"); 
    [_receivedData setLength:0]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

{ 
    NSLog(@"2"); 
    [_receivedData appendData:data]; 

} 
- (void)connection:(NSURLConnection *)connection 

    didFailWithError:(NSError *)error 

{ 
    NSLog(@"3"); 
    _theConnection = nil; 

    _receivedData = nil; 



    // inform the user 

    NSLog(@"Connection failed! Error - %@ %@", 

      [error localizedDescription], 

      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

{ 


    NSLog(@"4"); 
    NSLog(@"Succeeded! Received %lu bytes of data",(unsigned long)[_receivedData length]); 



    _theConnection = nil; 

    _receivedData = nil; 

} 

-(void) connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL 
{ 
    NSLog(@"5"); 
} 
@end 
+1

同样的问题[如何获得NSTimer循环](http://stackoverflow.com/questions/8055629/how-to-get-nstimer-to-loop) –

+0

简而言之:您的程序需要一个运行循环。 – dreamlax

+0

您正在使用同步请求。你的委托方法不会被调用。将请求的结果分配给nsdata对象'NSData * data = [NSURLConnection sendSynchronousRequest ....];'或使用异步请求并设置委托! – Mario

回答

0

你有两种方式同步或Asynchonous:

在同步任何代表不叫,右线 https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLConnection/sendSynchronousRequest:returningResponse:error:

_receivedData = [NSURLConnection sendSynchronousRequest:theRequest 
          returningResponse:nil 
             error:nil]; 
    NSLog(@"begin download"); 

    if (!_theConnection) { 

     _receivedData = nil; 

     // Inform the user that the connection failed. 

    } 

在异步你需要使用 - initWithRequest:委托: https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURLConnection/initWithRequest:delegate:

[NSURLConnection alloc] initWithRequest:delegate:theRequest 
          delegate:self]; 

NSLog(@"begin download");