storing pdf on local doesnt work in iphone - iphone

I'm new with iPhone development. I want to download a pdf using url and want to store it in a local folder.
Here is my code:
NSData *d = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://demo.com/ebookapplication/book_pdf/20111108094614hii.pdf"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myfile.pdf"];
[d writeToFile:path atomically:YES];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"pdf"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:myPathInfo toPath:path error:NULL];
}
NSURL *pdfurl = [NSURL fileURLWithPath:path];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:pdfurl];
[detail.bookweb loadRequest:requestObj];//to display data in web view
This code is working for display pdf in web view using url, but it doesn't work for storing pdf in our local system. Actually I want to store these downloaded pdf in one folder and and display it in library format. Please help.

-writeToFile:Atomically has already placed your file in the folder you've specified in the -copyItemAtPath:toPath: method, and is thus redundant. The document was already in the documents directory folder, you then copied it over to the same directory! I think that's your problem.
EDIT:
This Line HERE:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"myfile.pdf"];
specifies your path.
You can NSLog it with this line: NSLog(#"Document Path = %#", path);
As for your library comment, it shouldn't be that hard. My app, which basically does this already, wires
AQGridView: https://github.com/AlanQuatermain/AQGridView
And Apple's Docwatcher class from DITableView: http://developer.apple.com/library/ios/samplecode/DocInteraction/Listings/Classes_DITableViewController_h.html
Together to create an app that trolls the documents directory and adds files to an NSArray which are then displayed by the grid. A simpler approach would just be to use the DITableViewController though, considering it solves your problems, it's just the grid was for a little more pizzaz.

Related

Settings.plist iOS

I have a Settings.plist file in my iOS application. I access it using this code below:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"Settings.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:#"Settings" ofType:#"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
But, say I make some changes to the Settings.plist file in my application, like add another row. Then if I publish it, users that already have my app will have an out dated Settings.plist file. Any ideas on a workaround or anything? I have tried accessing the Settings.plist file straight from the NSString *bundle, but then the values don't "save".
You need to do a file modification date check, see my answer here. Then if the modification date is later than the last checked date, download the Settings.plist file and replace it in the users app.

How can i store streaming video in resource folder in iOS?

If i go to the point then my problem is, i want to make a web view where it will load a video and when stream starts then i want to download/store that video data in the resource folder of the application.How can i do that?
Please somebody help me by any kinds of help.
BR
Emon
In the run time you can't save a video to resource folder but you can save files in to
NSDocuments and retrive them using NSFileManagers.Go through this link
http://www.techotopia.com/index.php/Working_with_Files_on_the_iPhone
Try with this Code to save a file
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/filename.MP4",documentsDirectory];
NSData* videoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"your Url"]];
[[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
[filemanager createFileAtPath:filePath contents:videoData attributes:nil];
To retrive:
NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/filename.MP4",documentsDirectory];
BOOL exists = [fm fileExistsAtPath:newDirectory isDirectory:&isDir];
if (exists) {
//Play Video with contents of file path.
}

Error while writing data to a file in iphone

in my application, i have placed an empty
myFile.txt
when i have internet connection , i get json data from internet and save the json string in it with following code
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
[myString writeToFile:filePath automatically:YES encoding:NSUTF... error:nil];
//now while retrieving it when no internet connection
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"txt"];
NSString *myString = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF.. error:nil];
now myString returns with #""... why i am not been able to write data?
Best Regards
Whatever you want to do is not possible (or at least strongly discouraged) to update files in the app bundle.
If you’re downloading files and want to store them on the device you should use the Documents directory. You can get the path to this directory with:
- (NSString *)getDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
First check weather the contents are getting written to file. You can view the file in xcode.
What i suggest is when you run the app create this empty file. Then you can Read and write to that file easily.
Try Using Below code:
NSError* error = nil;
NSString* jsonFileName = [NSString stringWithFormat:#"myfile"];
NSString* jsonPath = [[NSBundle mainBundle] pathForResource:jsonFileName
ofType:#"txt"];
NSString* jsonString = [NSString stringWithContentsOfFile:jsonPath
encoding:NSUTF8StringEncoding error:&error];
Documents directory:
Write to file
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
//create content - four lines of text
NSString *content = #"One\nTwo\nThree\nFour\nFive";
//save content to the documents directory
[content writeToFile:fileName
atomically:NO
encoding:NSStringEncodingConversionAllowLossy
error:nil];
Display content:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:#"%#/textfile.txt",
documentsDirectory];
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];

Storing NSArray/NSMuatbleArray in File

I'm beginning to store an NSArray in file, but I keep getting nil as a response...
The Apple docs say that nil means your file doesn't exist, but I'm pretty sure mine does (I'm probably not building my path correctly)...
Here is the code that I use to store my NSArray in a file...
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"scorecards" ofType:#"dgs"];
NSArray *myArray = [[NSArray alloc] initWithObjects:#"J", #"ANA", #"MY", #"GDON", nil];
[myArray writeToFile:filePath atomically:YES];
NSArray *mynewArray = [NSArray arrayWithContentsOfFile:filePath];
NSLog(#"%#", [mynewArray objectAtIndex:2]);
I also took a screenshot of Xcode to show you guys that my file does exist...
The file is clearly saved in the group ScorecardsSaved and the file is called scorecards with the extension dgs (something I made up for my specific application - Dot Golf Scorecard - dgs)
Indeed, your file path uses the pathForResource method, thus it will point to a location in your application bundle, which is not writable.
Instead, you have to write to the application document directory on the device / simulator.
NSURL *documentsDirectory =
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
inDomains:NSUserDomainMask] lastObject];
NSURL *fileURL = [documentsDirectory
URLByAppendingPathComponent:#"storecards.dgs"];
So if your bundle version of the document contains some seed data, copy it to the app documents directory first.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME];
[myArray writeToFile:filePath atomically:YES];

save mp3 data in file on iphone

I'm developing an iPhone application and i need to download and Save mp3 media on the iPhone and play it,
I fished the downloading work and i put the data in NSMutableData variable But How can i save these data as mp3 file
any advice :)
If you want to save it to your applications Documents directory with a .mp3 file extension you can use the following:
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject]
stringByAppendingPathComponent:#"MyFileName.mp3"];
[[NSFileManager defaultManager] createFileAtPath:mp3FileName
contents:mp3Data
attributes:nil];
Where mp3Data is your NSMutableData. I use code similar to the above for saving PDF documents, custom file formats, jpgs and all sorts. I've not had cause to try mp3 yet but there's no reason why it shouldn't.
To read the file back into NSData to send to AVAudioPlayer:
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject]
stringByAppendingPathComponent:#"MyFileName.mp3"];
NSData *mp3Data = [NSData dataWithContentsOfFile:mp3FileName];
avAudioPlayer.data = mp3Data;
Above para of code untested, never had cause to use sounds in my apps.
here is the correct soulution appending up on #Diziet comment
'//prepare data to save in file
NSData *fileData=[[NSData alloc]initWithData:(NSData *)receivedData];
NSString *mp3FileName = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject]stringByAppendingPathComponent:#"MyFileName.mp3"];
//create the file
[[NSFileManager defaultManager] createFileAtPath:mp3FileName contents:receivedData attributes:nil];
//load the file for playing
NSString *FileNamePath = [[NSString alloc] initWithString:#"MyFileName.mp3"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:FileNamePath];
NSURL *url=[[NSURL alloc]initWithString:appSettingsPath ];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
player.delegate=self;
[player play];'