Got NSCocoaErrorDomain code=261 when reading xml file - ios

I am trying to read a COLLADA file (xml formatted, utf-8 encoding) with following code:
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:#"astroBoy_walk_Max" ofType:#"dae"];
NSError *error;
NSString *xmlContent = [NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:&error];
The returned xmlContent is nil and error #"NSCocoaErrorDomain" - code: 261.
Then I tried with other encoding like NSASCIIStringEncoding and the xmlContent is not nil but contained string "bplist00Ô\x01\x02\x03\x04\x05\x06\a\bZzippedData_\x10\x1creaderMinimumRequiredVersionVzippedWversionO\x12..." which obviously not original file content.
And later I renamed the file's extension to dat, and read with almost the same code:
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:#"astroBoy_walk_Max" ofType:#"dat"];
NSError *error;
NSString *xmlContent = [NSString stringWithContentsOfFile:xmlPath encoding:NSASCIIStringEncoding error:&error];
Everything looks good, the xmlContent contains correct file content string...
What is the problem when I reading the raw .dae file?

The collada files are compressed on compilation time
When you include a scene file in Collada or Alembic format in your
Xcode project, Xcode automatically converts the file to SceneKit’s
compressed scene format for use in the built app. The compressed file
retains its original .dae or .abc extension.
from Apple Documentation
If you open the app bundle and read the collada file as text you'll realize it's different from the one you added to your project

Related

ObjectiveC - Reading ePub File Gotten From Dropbox

So I'm pretty lost with this one and really new to epub files. I've done a bit of searching but can't seem to put everything together in my head.
My app uses DropBox's Chooser API to get a file from a user's DropBox folder. In this case, I want to open up a .epub file. So when the user chooses a file, the DropBox API gives me back an NSURL object to that file. For example:
https://dl.dropboxusercontent.com/1/view/e8bmxpkree6nc67/The%20Art%20of%20War.epub
And now, I've tried a couple different tools to try to read this file. Originally, I tried using KFEpubKit. But when I called:
epubURL; // The url from DropBox (shown above)
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
self.epubController = [[KFEpubController alloc] initWithEpubURL:epubURL andDestinationFolder:documentsURL];
self.epubController.delegate = self;
[self.epubController openAsynchronous:YES]
I would get back an error that the file couldn't be unzipped. The error reads as:
Epub Error: Error Domain=KFEpubKitErrorDomain Code=1 "Could not extract epub file." UserInfo=0x170275400 {NSLocalizedDescription=Could not extract epub file.}
I looked into the code and narrowed down the problem a little bit. The KFEpubKit uses the SSZipArchive utility to unzip files. And from this point on, I'm a bit stuck. The [SSZipArchive unzipFileAtPath: toDestination:] call seems to be failing when used with the epubURL.path. I'm not sure if this has something to do with the fact that my file is a .epub extension and not a .zip extension. Or maybe there's some stuff to do after getting the URL from DropBox and before giving it to the KFEpubKit tool?
In the end, I'm expecting to have to display the text of the book with a UIWebView. But I'm just not sure how to handle this .epub file. What should I do with the file from Dropbox? Any help is much appreciated.
A quick glance indicates that that SSZipArchive wants a local file URL, not a remote HTTP URL. Try downloading the file first (NSData with contents of URL, then write to some temp file) then create a file URL that points to the temp file, and send that into the KFEpubController:
// Download the file from dropbox
epubURL; // The url from DropBox (shown above)
NSData * epubData = [NSData dataWithContentsOfURL:epubURL];
NSString * tempPath = [NSTemporaryDirectory() stringByAddingPathComponent:#"temp.epub"];
[epubData writeToFile:tempPath atomically:YES];
NSURL *tempURL = [NSURL URLWithString:tempPath];
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
self.epubController = [[KFEpubController alloc] initWithEpubURL:tempURL andDestinationFolder:documentsURL];
// etc.
(Coding from memory.) All normal caveats apply here-- you'll want to do proper progress/error handling on the download, get rid of the temp file, etc, etc.

How to read content from plain text remote file with objective-c

I want to read a list (in plain text) from a remote file line by line.
I have found some answers but they're not the ones I'm looking for.
p.s. I've been programing in objective-c and developing in iOS for about 2 months, I'm a rookie i might not understand or recognize some terms. Please answer like you are talking to a beginner.
If i am not wrong you just want to read a text from remote file, so here it is.
NSString * result = NULL;
NSError *err = nil;
NSURL * urlToRequest = [NSURL URLWithString:#"YOUR_REMOTE_FILE_URL"];//like "http://www.example.org/abc.txt"
if(urlToRequest)
{
result = [NSString stringWithContentsOfURL: urlToRequest
encoding:NSUTF8StringEncoding error:&err];
}
if(!err){
NSLog(#"Result::%#",result);
}
To load the remote txt file, you should take a look at NSURLConnection or AFNetworking (there are other possibilities, these two are probably the most common).
You will then get the content of the file. Depending on what you intend to do with it, you may have to parse it, either with something as simple as -[NSString componentsSeparatedByString:] or with something a bit more powerful like NSScanner.
There are three steps involved in loading a file
create the object that specifies the location of the file
call the appropriate NSString class method to load the file into a
string
handle the error if the file is not found
In step 1, you need to either create an NSString with the full path to the file in the file system, or you need to create an NSURL with the network location of the file. In the example below, the code creates an NSURL since your file is on the network.
In step 2, use the stringWithContentsOfFile method to load a file from the file system, or the stringWithContentsOfURL method to load a file from the network. In either case, you can specify the file encoding, or ask iOS to auto-detect the file encoding. The code below auto detects while loading from the network.
In step 3, the code below dumps the file to the debug console if successful or dumps the error object to the console on failure.
Missing from this code is multithreading. The code will block until the file is loaded. Running the code on a background thread, and properly notifying the main thread when the download is complete, is left as an exercise for the reader.
NSURL *url = [NSURL URLWithString:#"www.example.com/somefile.txt"];
NSStringEncoding encoding;
NSError *error;
NSString *str = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error];
if ( str )
NSLog( #"%#", str );
else
NSLog( #"%#", error );

How to convert Excel file in csv and read data from iphone

I want to make an application that read excel file from url and convert data in to csv file and then i can read the csv file and display the data. how can i do this. please help me.
If you have a .xls file, you can use the open source DHlibxls library to read the file into your app. This is an ObjectiveC framework that wraps a C-based library. The library is quite mature.
How to convert Excel file in csv
Click here
and read data from iphone
//******** Get Data fron CSV file  **********//
NSString *path = [[NSBundle mainBundle] pathForResource:#"NameYourCsvFile" ofType:#"csv"];
NSError* error;
dataStr=[NSString stringWithContentsOfFile:path encoding:NSStringEncodingConversionAllowLossy error:&error ];

iOS - write jpeg file to file system

I'm new to iOS development and I'm trying to write an image as a jpeg file to the file system. From the logs I know that those file are indeed written to the file system, but when I try to save them to the camera roll they all appear black. I'm using the following code to write them as jpeg files:
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpegPath atomically:YES];
And the following code to write to camera roll:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Anybody know how to verify that those jpeg files are indeed written to the file system? And what I might be doing wrong in the second line of code?
EDIT: So here is the entire method:
- (BOOL)createImagesForSlides:(NSString *)documentsPath destinationURL:(NSURL *)destinationURL
{
NSFileManager *manager = [NSFileManager defaultManager];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:destinationURL];
NSString *folderName = [NSString stringWithFormat:#"/%#", document.title];
// create new folder
NSString *newFolderPath = [documentsPath stringByAppendingString:folderName];
BOOL result = [manager createDirectoryAtPath:newFolderPath withIntermediateDirectories:NO attributes:nil error:nil];
// create a jpeg file for each page of the pdf file
for (int i = 1; i <= document.numberOfPages; ++i) {
NSString *jpegPath = [NSString stringWithFormat:#"%#/%d.jpg", newFolderPath, i];
[UIImageJPEGRepresentation([[document pageForPageNumber:i] image], 1.0) writeToFile:jpegPath atomically:YES];
UIImageWriteToSavedPhotosAlbum([[document pageForPageNumber:i] image], nil, nil, nil);
}
return result;
}
document is a pointer to a CPDFDocument instance, and it's from some open source reader code available on github (iOS-PDF-Reader). What I basically do here is grab each page of the pdf document, generate an image, and then save them to the file system as jpeg file. Weird enough, even though there are more than 10 pages in the document, UIImageWriteToSavedPhotosAlbum only writes 5 files to camera roll. Any idea why?
It would probably be useful to see how you construct jpegPath here.
First, writeToFile:atomically: returns a BOOL, so check that for your first indication of success or failure.
There are a couple of ways you can verify that the image is written to the file system. If you are running on a device use something like iExplorer to access the file system and look at the file written. Since it is NSData* you can cheek the file size to make sure it looks reasonable. On the simulator, dig into the folder structure under ~/Library/Application Support/iPhone Simulator/ and examine the file. Without looking into the filesystem itself try reading the image back into another UIImage (imageWithData: in your case since you are writing a NSData* object).
There doesn't appear to be anything wrong with your UIImageWriteToSavedPhotosAlbum call according to the docs. It is OK for the last 3 arguments to be nil (all are marked as optional), you just have to be sure the UIImage is valid. Have you set a breakpoint to be sure you have a valid image (Xcode Quick Look feature)?

Converting an iPhone IOS Video File Uploader to work with a file stored in document directory

This is my first real project. I have an app that captures several seconds of video using AVFoundation, outputs this to a file in the documents directory and lets the user preview the video before they upload it using HTTP and a PHP script on my website.
All the video capture and preview work perfectly but I am stuck on uploading the video file.
I learnt a lot from this simpleSDK video which shows how to achieve the desired effect using a video file stored in the apps main bundle.
The code from the tutorial that set up videoData ready to upload originally looked like this:
NSData *videoData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Movie" ofType:#"mov"]];
NSString *urlString = #"http://www.iphonedevnation.com/video-tutorial/upload.php";
The filename of the video file that I need to upload is always unique and generated using CFUUIDCreateString. I join this string to the path for the documents directory, add ".mov" to the end of it and save it into a text file for retrieving later.
This all works as I am able to retrieve the filename from the file and use it to preview the movie clip elsewhere in the app.
My path is in an NSString, that I have tried converting to NSURL and removing the file suffix to get it to work with the NSData *videoData.........line but it doesn't compile, I get an "No known class method for selector 'dataWithContentsOfFile:ofType.' error. I am targeting iOS 5 and using Xcode 4.3 with ARC and Storyboards.
I've been at this for best part of 5 hours now so hopefully someone can help. My code, which included tips from elsewhere on converting from a NSString to NSURL follows:
NSString *content = [[NSString alloc] initWithContentsOfFile:lastSavedTalentFilenamePath
usedEncoding:nil
error:nil];
NSLog(#"content=%#",content);
//Need to now remove the '.mov' file type identifier
NSString *shortContent= [content substringToIndex:[content length]-4];
NSLog(#"***************shortContent***************%#", shortContent);
NSURL *convertedContent = [NSURL fileURLWithPath:shortContent];
NSLog(#"***************convertedContent***********%#",convertedContent);
NSData *videoData = [NSData dataWithContentsOfFile:convertedContent ofType:#"mov"];];
There is no NSData method called dataWithContentsOfFile:ofType:
The methods available are:
+ dataWithContentsOfFile:
+ dataWithContentsOfFile:options:error:
both of which take the file location as an NSString so there's not need to convert to an NSURL

Resources