我正在构建一个应用程序,该应用程序在Apple的服务器上托管了非消费类应用内购买。我已经成功地将我的应用内购买下载,但是当我将它们保存到文档目录中时,我似乎无法找到或访问它们。
这是我用来从下载的contentURL下载文件的功能。下载完成后调用它,传入download.contentURL将其在缓存中的位置移动到文档文件夹。
-(void)downloadFromURL: (NSURL *) temporaryURL {
NSLog(@"The download's contentURL is %@", temporaryURL.absoluteString);
NSString *folderName = [[temporaryURL path] lastPathComponent];
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folder = [pathArr objectAtIndex:0];
NSString *filePath = [folder stringByAppendingPathComponent:folderName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *writeError = nil;
NSData *downloadData = [[NSData alloc] initWithContentsOfURL:temporaryURL];
[downloadData writeToURL: fileURL options:0 error:&writeError];
if( writeError) {
NSLog(@"Error in writing file %@' : \n %@ ", filePath , writeError);
return;
}
NSLog(@"File successfully downloaded. Url is %@",fileURL.absoluteString);
myFileURL = fileURL;
}
myFileURL是一个全局变量,稍后会调用它来初始化AVAudioPlayer,但是当我调用时
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:[myFileURL path]]){
NSLog(@"File DOES NOT exist at that url");
} else{
NSLog(@"File DOES exist at that url");
}
它表示该路径中不存在文件。在从苹果服务器下载的app购买内容中编写和访问的任何想法或简单方法?我也尝试在URL的末尾添加“/Contents/filename.mp3”无效。