i got the following problem :
i am trying to create a file with the following Code :
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle FileHandleForWritingAtPath:filePath]retain];
NSString *data = #"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];
My Question is the following. Running this on a MAC does not give me any Errors whatsoever. But the File itself seems to be nonexistant or at least not locateable.
I need to find and access the File and i have to be able to export it to my Mac using a mobile iOS Device
thanks in advance :)
h4wkeye
You need to check that savedFile is not nil (see the docs here).
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle fileHandleForWritingAtPath:filePath]retain];
if (nil == savedFile) {
NSLog(#"Could not create saved file at %#", filePath);
}
NSString *data = #"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];
Related
I am very new to iOS. I create PDF and load this PDF on UIWebView
now this time I want to save or download this PDF in iPhone when we tapped download button then all exits PDF supporter show like as open ibook ,open in chrome. This type of option show but when we tap any one then my application closed.
-(void)show_Button
{
NSArray *docDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [docDirectories objectAtIndex:0];
NSString *filePAth = [docDirectory stringByAppendingPathComponent:#"myPDF.pdf"];
NSLog(#"filePath = %#", filePAth);
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSLog(#"url2 = %#", url2);
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
docContr.delegate=self;
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
so how to save or download this pdf in Iphone please solve this problem....
I believe you can simple use the belo two line:
NSData *myFile = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"your_url"]];
[myFile writeToFile:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], #"yourfilename.pdf"] atomically:YES];
I hope this it will help you,
Saving the pdf into app
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: path]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"pdfname.pdf"];
NSError *writeError = nil;
[imageData writeToFile:filePath options:NSDataWritingAtomic error:&writeError];
if (writeError) {
NSLog(#"Error writing file: %#", writeError); }
Getting the pdf from the NSDocument Directory
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:&error];
for(int i=0;i<[filePathsArray count];i++)
{
NSString *strFilePath = [filePathsArray objectAtIndex:i];
if ([[strFilePath pathExtension] isEqualToString:#"pdf"])
{
NSString *pdfPath = [[stringPath stringByAppendingFormat:#"/"] stringByAppendingFormat:strFilePath];
NSData *data = [NSData dataWithContentsOfFile:pdfPath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[arrayOfImages addObject:image];
}
}
}
I am trying to familiarize myself with objective C, and my current goal is to read a list of items in a text file and store them in a NSString array.
Currently this is what I have:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"txt"];
NSData* data = [NSData dataWithContentsOfFile:filepath];
NSString* string = [[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
NSString* delimiter = #"\n";
listArray = [string componentsSeparatedByString:delimiter];
I am not sure if this matters, but myList.txt is in my Supporting Files.
At the moment, I only have one item in my list. However I am unable to store even that 1 item into my listArray.
I am sure it is something silly that I am missing, I am just new to Objective C.
EDIT:
I apologize for not mentioning this earlier. I AM NOT receiving any sort of error. My array is just null.
I'm going to suggest a little simplification which might solve your problem since I can't say what your problem is. From the information I'm not sure if you are getting the proper file contents when reading it in or not.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"myList" ofType:#"txt"];
NSError *error;
NSString *fileContents = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
if (error)
NSLog(#"Error reading file: %#", error.localizedDescription);
// maybe for debugging...
NSLog(#"contents: %#", fileContents);
NSArray *listArray = [fileContents componentsSeparatedByString:#"\n"];
NSLog(#"items = %d", [listArray count]);
If the content of the file is just like:
[{"Title":"20","Cost":"20","Desc":""},{"Title":"10","Cost":"10.00","Desc":""},{"Title":"5","Cost":"5.00","Desc":""}]
try this
-(id)readFromDocumentDBFolderPath:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile])
{
NSError *error= NULL;
NSData* data = [NSData dataWithContentsOfFile:appFile];
id resultData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
if (error == NULL)
{
return resultData;
}
}
return NULL;
}
From couple of days I was searching of how to play ogg on iPhone. But not successful. I found this post which points towards precompiled-ogg-vorbis-libraries-for-ios, but I don't know how to use this framework or libraries. I am new in coding. I also found this post
but it plays ogg audio from bundle. I want to play from a given url, like wikipedia audios. any help will be great. Thanks
UPDATE:
I found another library or file which can be use in iOS but, still I don't know how to use in Xcode. Ogg Vorbis I audio decoder by Sean Barrett. Any idea how to use this.
UPDATE 2:
Just tried to use it something like this, but output file have no sound. Any idea where I am going wrong.
NSString *file = #"http://upload.wikimedia.org/wikipedia/commons/b/b2/En-us-Pakistan.ogg";
NSURL *fileURL = [NSURL URLWithString:file];
NSData * data = [NSData dataWithContentsOfURL:fileURL];
unsigned char* array = (unsigned char*) [data bytes];
int len = [data length];
if (data != nil) {
NSLog(#"\nis not nil");
//NSString *readdata = [[NSString alloc] initWithContentsOfURL:(NSData *)data ];
stb_vorbis* Stream = stb_vorbis_open_memory(array, len, NULL, NULL);
if (Stream)
{
stb_vorbis_close(Stream);
//return true;
}
else
{
//return false;
}
}
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
NSData *ddata = [NSData dataWithBytes:byteData length:len];
if (ddata == nil) {
NSLog(#"data nil");
}
else{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"wikiaudio"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder if it doesn't already exist
NSString * filename = [#"hello" stringByAppendingString:#".mp3"];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", dataPath,filename];
[ddata writeToFile:filePath atomically:YES];
NSLog(#"filepath %#", filePath);
}
So I have a zip file, which gets extracted using Objective-Zip. The extracted contents then get put into an NSData object. I want to save the contents into the apps Documents folder. The problem is these are multiple files, and writeToFile:atomically: saves the contents of the zip into one file.
Any ideas? The other question I saw just said to use ZipArchive.
I need help fast, because the app needs to be finished by the end of my work tomorrow.
I think you need to use ZipArchive:
ZipArchive* za = [[ZipArchive alloc] init];
NSData *Data = [NSData dataWithContentsOfURL:url]; // here url is your zip url
NSArray *documentPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirString = [documentPathArray objectAtIndex:0];
NSString *filePath = [documentsDirString stringByAppendingPathComponent:#"temp.zip"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *writeError = nil;
[Data writeToURL: fileURL options:0 error:&writeError];
if( writeError) {
NSLog(#" Error in writing file %#' : \n %# ", filePath , writeError );
return;
}
if( [za UnzipOpenFile:filePath] )
{
BOOL ret = [za UnzipFileTo:documentsDirz overWrite:YES];
if( NO==ret )
{
NSLog(#"NO");
}
[za UnzipCloseFile];
}
for more info
i've a class (downloaded from internet) that read a value of a file in this way (and after split the file):
NSArray* array = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
where fileContents is my fullpathoffile.
in which way i must write my string?
i'm trying with this:
NSString *data = [NSString stringWithFormat:#"%#,%#""\n", latitudine.text, longitudine.text];
NSFileHandle *handle;
handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ];
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
[handle writeData:[data dataUsingEncoding:NSUTF8StringEncoding]];
i'm going crazy :D
Any helpS?!?
i've solve in this way:
NSString *data = somethingblablabla ;
NSData *myData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSFileHandle *writeData = [NSFileHandle fileHandleForWritingAtPath:[self dataPath]];
[writeData truncateFileAtOffset:[writeData seekToEndOfFile]];
[writeData writeData:myData];