[UPDATE AT THE END OF THE QUESTION]
I use to play some audio files in my app.
I used to store the files in the app, but then I moved to On Demand Resourcesand NSBundleResourceRequest.
I have an IBAction to play a random file, and since I moved to ODR, it is not working. The only thing I changed was NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; (line 3)
NSBundleResourceRequest *request1;
- (IBAction)randomPlay {
NSString *bundleRoot = [[request1 bundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:#"self ENDSWITH '.mp3'"];
NSArray *onlyMP3s = [dirContents filteredArrayUsingPredicate:fltr];
.
.
.
audio = [onlyMP3s[arc4random_uniform((uint32_t)onlyMP3s.count)]
stringByReplacingOccurrencesOfString:#".mp3" withString:#""];
[self playSelectedAudio:audio];
}
The dirContents looks like:
when it is supposed to contain the mp3 files.
What am I doing wrong with bundleRoot?
My viewDidload is
- (void)viewDidLoad {
[super viewDidLoad];
tagsSet = [NSSet setWithObjects:#"sounds", nil];
request1 = [[NSBundleResourceRequest alloc] initWithTags:tagsSet];
[request1 conditionallyBeginAccessingResourcesWithCompletionHandler:^
(BOOL resourcesAvailable) {
if (resourcesAvailable) {
//don't do nothing. just use the file slater
} else {
[request1 beginAccessingResourcesWithCompletionHandler:^
(NSError * _Nullable error) {
if (error == nil) {
//download the files
} else {
}
}];
}
}];
I actually have another method to play a selected audio (not a random one)
-(void)playSelectedAudio:(id)audioToPlay {
NSURL *url = [NSURL fileURLWithPath:[[request1 bundle]
pathForResource:audioToPlay ofType:#"mp3"]];
and it is working fine.
So there is a problem in the bundleRoot, is is not where my files are located.
[UPDATE]
I actually did
- (IBAction)randomPlay {
NSURL *bundleRoot = [NSURL fileURLWithPath:[[request1 bundle]
pathForResource:#"" ofType:#"mp3"]];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtURL:bundleRoot
includingPropertiesForKeys:[NSArray array]
options:0 error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:
#"self ENDSWITH '.mp3'"];
NSArray *onlyMP3s = [dirContents
filteredArrayUsingPredicate:fltr];
.
.
.
audio = [onlyMP3s[arc4random_uniform((uint32_t)onlyMP3s.count)]
stringByReplacingOccurrencesOfString:#".mp3"
withString:#""];
[self playSelectedAudio:audio];
}];
}
and it is ALMOST working but is is always playing the first audio in the directory.
Didn't you forget beginAccessingResourcesWithCompletionHandler: call to actually download resources?
- (IBAction)randomPlay {
NSBundleResourceRequest *request1; // I hope you properly initialize this request :)
[request1 beginAccessingResourcesWithCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
// Probably, add here dispatch_async(dispatch_get_main_queue(), ^{
NSString *bundleRoot = [[request1 bundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:#"self ENDSWITH '.mp3'"];
NSArray *onlyMP3s = [dirContents filteredArrayUsingPredicate:fltr];
.
.
.
[self playSelectedAudio:audio];
// You will probably need to invoke [request1 endAccessingResources] later, when you finish accessing your audio.
}
else {
// handle error
[request1 endAccessingResources];
}
}
}
Several important notes from NSBundle.h:
The completion block will be invoked on a non-main serial queue when the resources are available or an error has occurred.
So you will probably need to use additional dispatch_async to main queue in completion handler. But this depends solely on your code.
Be sure to always invoke the -endAccessingResources method to balance a call to the begin method, even in the case of an error in the completion handler.
Related
Is it possible to get the contents of an iCloud URL without having to go through UIDocument?
You can use the following code for downloding the contents of icloud url without creating the object of NSDocument. This will be worked for me...
-(void)loadData:(NSMetadataQuery *)query
{
if ([query resultCount] == 1)
{
NSMetadataItem *item = [query resultAtIndex:0];
NSURL *fromURL = [item valueForAttribute:NSMetadataItemURLKey];
NSURL *toURL=[NSURL fileURLWithPath:#"PATH_OF_FILE_TO_STORE_DATA"];
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[coordinator coordinateReadingItemAtURL:fromURL options:0 writingItemAtURL:toURL options:NSFileCoordinatorWritingForReplacing error:nil byAccessor:^(NSURL *newReadingURL, NSURL *newWritingURL)
{
[[NSFileManager defaultManager] removeItemAtPath:newWritingURL.path error:NULL];
[[NSFileManager defaultManager] copyItemAtPath:newReadingURL.path toPath:newWritingURL.path error:nil];
}];
}
}
In iOS I want to move images to specific location using switch when switch is on it will move image to my specified location when it turn off it will move where to it comes from.
you can use following function to move all your images to new Path.
You need to pass just both DirectoryPath Old and New
- (void)moveFileFromFolder:(NSString *)oldFolderPath toFolder:(NSString *)newFolderPath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:oldFolderPath error:nil];
for (NSString *fileName in fileNames) {
NSString * oldFilePath = [oldFolderPath stringByAppendingString:fileName];
NSString * newFilePath = [newFolderPath stringByAppendingString:fileName];
NSError *error = nil;
[fileManager moveItemAtPath:oldFilePath toPath:newFilePath error:&error];
if (error) {
NSLog(#"error = %#", [error description]);
return;
}
}
}
I want to create an application that automatically downloads a folder from my own server and store it locally on the iPad/iPhone. How can i accomplish this and where does the folder get stored on the local iDevice ? Therefore how will i access it afterwards? Many thanks for your help
The best way to do so was actually putting all the pictures for example in one zip file, downloading it and unzipping it on the real device using this code :
dispatch_queue_t queue = dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSURL *url = [NSURL URLWithString:#"someDirectory/newPics.zip"];
NSError *error = nil;
// 2
NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(!error)
{
// 3
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *zipPath = [path stringByAppendingPathComponent:#"newPics.zip"];
[data writeToFile:zipPath options:0 error:&error];
if(!error)
{
ZipArchive *za = [[ZipArchive alloc] init];
// 1
if ([za UnzipOpenFile: zipPath]) {
// 2
BOOL ret = [za UnzipFileTo: path overWrite: YES];
if (NO == ret){} [za UnzipCloseFile];
// 3
NSString *imageFilePath = [path stringByAppendingPathComponent:#"newPics/pic1.png"];
//[self removeImage:zipPath];
[[NSFileManager defaultManager] removeItemAtPath:zipPath error: &error];
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *fileURL = [NSURL fileURLWithPath:imageFilePath];
[_webV loadRequest:[NSURLRequest requestWithURL:fileURL]];
});
}
}
else
{
NSLog(#"Error saving file %#",error);
}
}
else
{
NSLog(#"Error downloading zip file: %#", error);
}
});
It's the best way to do it , fast and reliable.
I am trying to save data into a directory created by NSFileManager but when I try and retrieve the data and NSLog it I get null. Also, when you create a directory does that mean you create a folder at a specified url path? Heres the code I am using
NSError *error = nil;
NSFileManager *manager = [[NSFileManager alloc] init];
NSArray *urlsArray = [manager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSLog(#"%#", urlsArray);
// This will create a new url and append a photo title to the end
NSURL *url = [[urlsArray lastObject] URLByAppendingPathComponent:[NSString stringWithFormat:#"%#", [self.recentPhoto objectForKey:FLICKR_PHOTO_TITLE]]];
NSLog(#"%#", url);
NSString *urlString = [NSString stringWithFormat:#"%#", [url absoluteString]];
NSLog(#"%#", urlString);
//create the directory
if(![manager fileExistsAtPath:urlString isDirectory:YES]){
BOOL success = [manager createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:&error];
if (!success) {
NSLog(#"Error creating data path: %#", [error localizedDescription]);
}
}
//get url for a photo image and then store it.
NSURL *imageURL = [FlickrFetcher urlForPhoto:self.recentPhoto format:FlickrPhotoFormatLarge];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
[imageData writeToURL:url atomically:YES];
//get data to check if its stored
NSData *checkImageData = [[NSData alloc] initWithContentsOfURL:url];
//This returns null
NSLog(#"%#", checkImageData);
//this returns 0
NSLog(#"%d", [manager isReadableFileAtPath:urlString]);
There are several issues with your code.
This:
NSURL *url = [[urlsArray lastObject] URLByAppendingPathComponent:[NSString stringWithFormat:#"%#", [self.recentPhoto objectForKey:FLICKR_PHOTO_TITLE]]];
should be:
NSURL *url = [[urlsArray lastObject] URLByAppendingPathComponent:[self.recentPhoto objectForKey:FLICKR_PHOTO_TITLE]];
This:
NSString *urlString = [NSString stringWithFormat:#"%#", [url absoluteString]];
should be:
NSString *urlString = [url path];
This:
BOOL success = [manager createDirectoryAtURL:url withIntermediateDirectories:YES attributes:nil error:&error];
should be:
BOOL success = [manager createDirectoryAtURL:[url URLByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error];
This:
[imageData writeToURL:url atomically:YES];
should be:
NSError *error = nil;
BOOL res = [imageData writeToURL:url options:NSDataWritingAtomic error:&error];
if (!res) {
NSLog(#"Unable to write to %#: %#", url, error);
}
The primary issue was the use of absoluteString instead of path to convert the URL to a file path. Secondary was passing the filename instead of the path when creating the directory. The needless use of stringWithFormat: didn't cause any issues but please break that habit now. Only use stringWithFormat: when you actually need to format a string. It is NOT needed to assign a string to a string variable.
You didn't specify a file name for your image inside the directory, this can be solved by adding this line right after the directory is created :
// Right after the directory is created (I suppose the format is png) :
url = [url URLByAppendingPathComponent:#"image.png"];
// Now you can continue with the rest of your code :
//get url for a photo image and then store it.
NSURL *imageURL = [FlickrFetcher urlForPhoto:self.recentPhoto format:FlickrPhotoFormatLarge];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
[imageData writeToURL:url atomically:YES];
//get data to check if its stored
NSData *checkImageData = [[NSData alloc] initWithContentsOfURL:url];
//This returns null
NSLog(#"%#", checkImageData);
//this returns 0
NSLog(#"%d", [manager isReadableFileAtPath:urlString]);
I have never created an instance of a file manager. I have always simply used the default.
See if this helps:
NSFileManager *manager = [NSFileManager defaultManager];
I want to retrieve in an array of the name of the files in Resources folder that have the .html extension. I have done this:
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Resources"];
NSError *error = nil;
NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
for (int i=0; i < [documentArray count] - 1; i++) {
NSLog(#"value %#", [documentArray objectAtIndex:i]);
}
But the for loop displays continously null. Anyone knows how can I get this task right? thank you
Don't use NSHomeDirectory. Use [[NSBundle mainBundle] resourcePath] to get path to app's resources.
Edited: here's your example code.
NSString *resPath = [[NSBundle mainBundle] resourcePath];
NSError *error = nil;
NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:resPath error:&error];
if (!error)
{
for (NSString * filename in filenames)
{
NSString *extension = [filename pathExtension];
if ([extension isEqualToString:#"html"])
{
NSLog(#"%#", filename);
}
}
}