iOS中url为零的NSInputStream
我试图设置一个NSInputStream,但当我进入代码时,我的输入流显示为nil。url来自Dropbox帐户 在我通过Dropbox Chooser获得url后,通过NSData获取文件会使我的iPhone 4崩溃(尽管在通过XCode运行时不会)。文件太大了,所以我想试试NSInputStream 我从中看到url应该是本地的。知道如何从Dropbox流式传输文件吗 谢谢iOS中url为零的NSInputStream,ios,objective-c,dropbox,dropbox-api,nsinputstream,Ios,Objective C,Dropbox,Dropbox Api,Nsinputstream,我试图设置一个NSInputStream,但当我进入代码时,我的输入流显示为nil。url来自Dropbox帐户 在我通过Dropbox Chooser获得url后,通过NSData获取文件会使我的iPhone 4崩溃(尽管在通过XCode运行时不会)。文件太大了,所以我想试试NSInputStream 我从中看到url应该是本地的。知道如何从Dropbox流式传输文件吗 谢谢 - (void)setUpStreamForFile { // iStream is NSInputStream in
- (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];
有关更多信息,您可以查看官方信息,因此感谢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快得多,NSURLSessionDataTask的工作方式类似。使用
NSURLConnection
。当您获得每个数据块时,将数据附加到一个文件中。嘿,谢谢@rmaddy,我最终使用了NSURLSession,它现在工作得很好。我在下面列出了更完整的答案。