NSURLConnectionDataDelegate只适用于App Delegate

时间:2014-05-08 17:57:23

标签: xcode ftp nsurlconnection nsurlconnectiondelegate nsconnection

(在MAC OSX-Xcode中)我尝试使用NSURLConnection类通过FTP读取文件,如果我的代码在主Application Delegate类(AppDelegate)中但是如果我在外面使用它(另一个.h .m文件)它工作正常)没有调用连接委托。

任何帮助!?

这是我的代码:

-(void)getURL {

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"ftp://.... connection string with file"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];

NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (connection) {

    // Do something

} else {
    // Inform the user that the connection failed.
    NSLog(@"connection failed");
}

}

代表:

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

{

NSLog(@"didReceiveResponse");

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

NSLog(@"connectionDidFinishLoading");

}

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

{

//Getting file data
[receivedData appendData:data];

}

1 个答案:

答案 0 :(得分:0)

您是否已将代理<NSURLConnectionDelegate>添加到您尝试使用它的视图控制器中?

例如

@interface ViewController : UIViewController <NSURLConnectionDelegate>