【问题标题】:NSURLConnectionDataDelegate just work in App DelegateNSURLConnectionDataDelegate 只在 App Delegate 中工作
【发布时间】:2014-06-26 06:47:57
【问题描述】:

(在 MAC OSX-Xcode 中)我尝试使用 NSURLConnection 类通过 FTP 读取文件,只要我的代码在主应用程序委托类 (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];

}

【问题讨论】:

    标签: xcode ftp nsurlconnection nsurlconnectiondelegate nsconnection


    【解决方案1】:

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

    例如

    @interface ViewController : UIViewController <NSURLConnectionDelegate>
    

    【讨论】:

    • 是的,正如我所说,它在 AppDelegate 类中正常工作
    • 评论:我在 MAC OSX 中使用它而不是在 iOS 上
    • @user2713544 很抱歉,我没有意识到它适用于 Mac。
    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    相关资源
    最近更新 更多