-2

How can I check if a file is download and save on device? Help me please.

- (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];

    }
}

}
  • Did you check [Google Drive's API guide for iOS](https://developers.google.com/drive/ios/devguide/files)? – Kristoffer Bohmann Feb 28 '16 at 15:46
  • 1
    Possible duplicate of [How do I download and save a file locally on iOS using objective C?](http://stackoverflow.com/questions/5323427/how-do-i-download-and-save-a-file-locally-on-ios-using-objective-c) – Abhijeet Feb 28 '16 at 16:02
  • The file is downloaded every time I press the play button . And I need to carry out checks if the file already downloaded . – Alex Taravinko Feb 28 '16 at 16:23

1 Answers1

0

If you want to see if the file music.mp3 is in your Documents folder:

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

This only makes sense if that file is always the same file. If it can be different files then you need to associate the URL you use to download with the file you stored it in.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192