2016-02-28 138 views
-2

如何检查文件是否下载并保存在设备上?请帮帮我。如何检查文件是否下载?

- (void) song{ 
if (_index1 == 0) { 
NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B0EJbcHq3ZALWUo0a05LMWNzeDg"; 
NSURL *url = [NSURL URLWithString:stringURL]; 
NSData *urlData = [NSData dataWithContentsOfURL:url]; 
if (urlData) 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"music.mp3"]; 
    [urlData writeToFile:filePath atomically:YES]; 
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:filePath] error:nil]; 

    } 
} 

} 
+0

您是否检查了[Google云端硬盘iOS版API指南](https://developers.google.com/驱动/ IOS/devguide /文件)? –

+1

[如何在iOS上使用目标C在本地下载和保存文件?](http://stackoverflow.com/questions/5323427/how-do-i-download-and-save-a-file- local-on-ios-using-objective-c) – Abhijeet

+0

每次按下播放按钮时都会下载文件。我需要检查文件是否已经下载。 –

回答

0

如果你想查看该文件的music.mp3是Documents文件夹中:

NSString *filePath = [documentsDirectory [email protected]"music.mp3"]; 
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; 

这才有意义,如果该文件是永远不变的文件。如果它可以是不同的文件,那么您需要将用于下载的URL与您存储的文件关联起来。