在iOS中,网址为nl的NSInputStream为nil

时间:2014-04-12 23:47:48

标签: ios objective-c dropbox dropbox-api nsinputstream

我试图设置一个NSInputStream,但是当我进入代码时,我的输入流输出为nil。该网址来自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];
}

2 个答案:

答案 0 :(得分:1)

嘿,不要犹豫使用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

答案 1 :(得分:0)

所以感谢rmaddy的建议,我查了一下NSURLConnection,但决定使用NSURLSession的功能。

我像这样使用了NSURLSessionDownloadTask。熟悉Dropbox选择器应该会有所帮助。

-(IBAction)didPressChooser:(id)sender {
{
    [[DBChooser defaultChooser] openChooserForLinkType:DBChooserLinkTypeDirect
                                    fromViewController:self completion:^(NSArray *results)
     {
         if ([results count]) {

             DBChooserResult *_result = results[0];
             NSString *extension = [_result.name pathExtension];
             if ([extension isEqualToString:@"m4a"]) {
                 url = _result.link; //url has already been declared elsewhere
                 DBFileName = _result.name; //DPFileName has also been declared. It's a string

                 NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
                 NSURLSession *session = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];
                NSURLSessionDownloadTask *getFile = [session downloadTaskWithURL:url];
                 [getFile resume];

             }


                   } else {
             // User canceled the action
         }


     }];
}
}

一旦你有了这个,你就会放入另一个作为完成处理程序的方法。

-(void)URLSession:(NSURLSession *)session
 downloadTask:(NSURLSessionDownloadTask *)downloadTask
didFinishDownloadingToURL:(NSURL *)location {

NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); //I put the file in a temporary folder here so it doesn't take up too much room.
NSString  *documentsDirectory = [paths objectAtIndex:0];

NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, DBFileName];
NSData *data = [NSData dataWithContentsOfURL:location];
[data writeToFile:filePath atomically:YES];
url = [NSURL fileURLWithPath:filePath];  //Yep, I needed to re-assign url for use elsewhere.

//do other stuff with your local file now that you have its url!

}

奖励是您可以通过这个非常棒的功能跟踪下载进度:

-(void)URLSession:(NSURLSession *)session
 downloadTask:(NSURLSessionDownloadTask *)downloadTask
 didWriteData:(int64_t)bytesWritten
totalBytesWritten:(int64_t)totalBytesWritten
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"%f / %f", (double)totalBytesWritten,
      (double)totalBytesExpectedToWrite);
}

无论如何,希望有人觉得这很有用。我的iPhone 4的工作速度比NSURLSessionDataTask的工作速度快得多。