How to List out .EPUB files in iPhone? - iphone

I am creating an epub reader. In that I want to list out .epub files from iphone. So I want to know is there any possible way to list out the .epub files from iphone (not just from the project directory path but also anywhere else in the phone)?

No, this is not possible.
Since there is no filesystem access, except the the directory with in apps sandbox.
All apps have to store the files they use with there sandbox, you tell iOS that you app can op .epub files. Which will allow the user to open the file from, example an email in your app.

As answered by #rckoenes, it is not possible to access the filesystem other than your app bundle.
You can access the files in your app bundle like this:
NSString *bundlePathName = [[NSBundle mainBundle] bundlePath];
NSError *error;
NSArray *bundleContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePathName error:&error];
for (NSString *currentItem in bundleContents) {
if ([currentItem rangeOfString:#"." options:NSBackwardsSearch].location != NSNotFound) {
int tempIndex = (int)([fileName rangeOfString:#"." options:NSBackwardsSearch].location);
tempIndex++;
NSString *aStrExtension = [[fileName substringWithRange:NSMakeRange(tempIndex, [fileName length]-tempIndex)] lowercaseString];
if ([aStrExtension isEqualToString:#"epub"]) {
//Add this file to an array, to make it available for choosing and view its details
}
}
}

If you mean you want to open ePub files saved not inside your application bundle, then you cant, you will have access only to the files inside your app sandbox

As per the #rckoenes: Any files out of App bundle is not accessible,
So I retrieved .epub files like this way.
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *manager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:bundleRoot];
NSString *filename;
while ((filename = [direnum nextObject] )) {
if ([filename hasSuffix:#".epub"]) { //change the suffix to what you are looking for
[arrayListofEpub addObject:[filename stringByDeletingPathExtension]];
}
}

Related

How to package a set of videos in multiple directories and play them in an iPhone app?

I am making a video delivery application and sending a bunch of files in the following directory structure:
Home/Week 1/Day 1/123.mp4,abc.mp4,42343.mp4
Home/Week 1/Day 2/123.mp4,xyz.mp4
etc. I need to maintain the directory structure and play the appropriate file.
NOTE: there are multiple files with the same name in different folders.
Current code:
I dragged the Home folder into xcode into the other sources folder > the action copied the files into the project directory and added the references.
This is the code I wrote for playing the video "abc" from day1 in week1 in home.
NSString* p = #"home/Week 1/Day 1/abc";
NSString* moviePath2 = [[NSBundle mainBundle] pathForResource:#"Home/Week 1/Day 1/abc" ofType:#"mp4"];
NSURL* movieURL2=[NSURL fileURLWithPath:moviePath2];
In this case, I get moviePath2 as Empty.
If I run this code:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docsDirectory stringByAppendingPathComponent:p];
if(![fileManager fileExistsAtPath:filePath])
{
NSLog(#"exists");
}
It prints the exists on console.
Can you please provide a solution for this?
Is my approach to package the directory structure with videos correct??
Thanks for the help.
Access your movie by simply movie name
NSURL* movieURL2=[NSURL fileURLWithPath: p];
Or you want to access it from main bundle then you have to copy your movie to that path
[fileManager copyItemAtPath:moviePath2 toPath:filePath error:nil];

How to list sound files in iOS?

In my app I imported 4 sound files. Now I want to list all the sound files in a View. When the user clicks any one of the sound, it needs to be selected and played as like in the Alarms app (choosing the sound for alarm). The difference here is I am getting the sound from my project. I have searched in SO and Google but I couldn't find a solution exactly for this problem.
Assuming that "In my app i imported 4 sound files" means that the files are in your app bundle (and also assuming that the extension of the files is MP3 - you can change it to whatever extensions they actually have):
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSFileManager *mgr = [[NSFileManager alloc] init];
NSArray *allFiles = [mgr contentsOfDirectoryAtPath:bundlePat error:NULL];
for (NSString *fileName in allFiles)
{
if ([[fileName pathExtension] isEqualToString:#"mp3"])
{
NSString *fullFilePath = [bundlePath stringByAppendingPathComponent:fileName];
// fullFilePath now contains the path to your MP3 file
DoSomethingWithFile(fullFilePath);
}
}

iTunes file sharing. But sharing over http? iOS

I have a file which I want to file share. 'hello.mp3'
At the moment the code enables file sharing if the file is in the app.
I was hoping I would be able to do it over http. So file sharing with the link instead of the sound being in the app. I want to do this because it will save memory for the user.
Here is the current code. The code allows file sharing if the file is in the app. I want it so the user will 'download' the sound from a http server such as http://test.com/hello.mp3
Thanks
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSArray *names = [NSArray arrayWithObjects: #"hello.mp3",
#"hi.mp3", nil];
for (NSString *fileName in names)
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:fileName];
if (![fileManager fileExistsAtPath:documentDBFolderPath])
{
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error];
}
}
}
I may be wrong, but that may not be allowed. In the guidelines:
2.7 Apps that download code in any way or form will be rejected.
While an audio file may not be "code", if it is used within the app, perhaps as a language translation item, etc...I would think it would be rejected...just my 2 cents worth though.
Correct, you can "stream" content over HTTP, but if your using HTTP AND saving to the "Documents" directory to hold content you risk rejection by the app store.
If you need a workaround maybe use the "tmp" directory and save a file off a file there. The complaint Apple has is mainly using phone data more than should be in a 3rd party application.

Permanent file changes on iPhone simulator

I'm in trouble with paths, relative paths, NSBundle and all the path/file related operations :)
While i run the simulator everthing goes right but all the file changes are not permanent, so everytime i run my app i have to repeat the initial setup of my app.
The question:
What is the proper way to read and write files (from resource dir) and make all the file changes permanent (updated into the project folder) ?
Thanks
EDIT: i write a simple method to do it, it's correct ?
-(NSString *) getPath:(NSString *)forResource
{
NSString *pathRes, *docPath, *destPath;
NSArray *foundPaths;
NSFileManager *fs = [[NSFileManager alloc] init];
foundPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
docPath = [foundPaths objectAtIndex:0];
destPath = [[NSString stringWithFormat:#"%#/%#",docPath,forResource] stringByStandardizingPath];
if(![fs fileExistsAtPath:destPath])
{
pathRes = [[NSBundle mainBundle] pathForResource:forResource ofType:nil];
[fs copyItemAtPath:pathRes toPath:destPath error:nil];
}
return destPath;
}
You cannot write to the application folder on the device. Write any persistent data to your documents directory. You can copy files from the resources directory to the documents directory on the first run to keep your existing logic.
NSSearchPathForDirectoriesInDomains( NSDocumentDirectory , NSUserDomainMask , YES );

iPhone: Can access files in documents directory in Simulator, but not device

I'm writing an app that copies some contents of the bundle into the applications Document's directory, mainly images and media. I then access this media throughout the app from the Document's directory.
This works totally fine in the Simulator, but not on the device. The assets just come up as null. I've done NSLog's and the paths to the files look correct, and I've confirmed that the files exist in the directory by dumping a file listing in the console.
Any ideas? Thank you!
EDIT
Here's the code that copies to the Document's directory
NSString *pathToPublicationDirectory = [NSString stringWithFormat:#"install/%d",[[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]];
NSString *manifestPath = [[NSBundle mainBundle] pathForResource:#"content" ofType:#"xml" inDirectory:pathToPublicationDirectory];
[self parsePublicationAt:manifestPath];
// Get actual bundle path to publication folder
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathToPublicationDirectory];
// Then build the destination path
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d", [[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]]];
NSError *error = nil;
// If it already exists in the documents directory, delete it
if ([fileManager fileExistsAtPath:destinationPath]) {
[fileManager removeItemAtPath:destinationPath error:&error];
}
// Copy publication folder to documents directory
[fileManager copyItemAtPath:bundlePath toPath:destinationPath error:&error];
I am figuring out the path to the docs directory with this method:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
And here's an example of how I'm building a path to an image
path = [NSString stringWithFormat:#"%#/%d/%#", [self applicationDocumentsDirectory], [[thisItem valueForKey:#"publicationID"] intValue], [thisItem valueForKey:#"coverImage"]];
It turned out to be an issue where the bundle was not being updated on the device and apparently didn't have the same set of files that the Simulator had. This blog post helped a bit: http://majicjungle.com/blog/?p=123
Basically, I cleaned the build and delete the app and installed directly to the device first instead of the simulator. Interesting stuff.
I don't see where documentsDirectory is defined.
NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d", [[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]]];
Perhaps the following will do the trick
NSString *destinationPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:#"%d", [[[manifest objectAtIndex:i] valueForKey:#"publicationID"] intValue]]];
Your copy code looks fine, and you say you're getting no errors.
But I'm intrigued by "The assets just come up as null." Are you sure you're accessing the file name later with the exact same name?
Unlike the simulator, a real iPhone has a case sensitive file system.