ios - NSInputStream 与 url 在 iOS 中出现 nil

标签 ios objective-c dropbox dropbox-api nsinputstream

我正在尝试设置一个 NSInputStream,但是当我进入代码时,我的输入流显示为 nil。该 url 来自 Dropbox 帐户。

在我通过 Dropbox Chooser 获得 url 后通过 NSData 获取文件会使我的 iPhone 4 崩溃(尽管当它通过 XCode 运行时不会)。文件太大了,所以我想试试 NSInputStream。

我从I cannot initialize a NSInputStream看到该网址应该是本地的。知道如何从 Dropbox 流式传输文件吗?

谢谢。

- (void)setUpStreamForFile {
// iStream is NSInputStream instance variable already declared
iStream = [NSInputStream inputStreamWithURL:url];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                   forMode:NSDefaultRunLoopMode];
[iStream open];
}

最佳答案

嘿,不要犹豫,使用 AFNetworking,它是一个很好的框架来操纵您的连接和下载内容。这是从 URL 下载文件的示例:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
    NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];

更多信息可以查看官方资料HERE

关于ios - NSInputStream 与 url 在 iOS 中出现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037504/

相关文章:

javascript - 了解控制台错误 "TypeError: undefined is not an object"和 "Uncaught TypeError: Cannot read property ' 0' of undefined"

ios - AVPlayer seekToTime 方法在 iOS 9 上停止工作

git - 跨计算机快速同步 git 工作区

sql - Excel 到 Azure 数据工厂到 SQL

iphone - 如何检查给定的 xml 在 Objective C 中是否有效?

javascript - 如何创建 Dropbox 托管应用程序

iphone - 针对空 UITextField 进行验证?

ipad - UINavigationController:Resize动画中标题跳转

ios - 如何在 objective-c 中获取大于 127(扩展 Ascii 字符)的整数的 ascii 值

objective-c - iOS - 通话时的通知?