How can I load a .pod File from an URL in COCOS3D - iphone

I am making an app and I want to load some 3d models who are stored in a server. I can easily acces them in the browser with a link.
The idea of the app is that you have a menu and you can select which model do you want to see.

In cocos3D you can create a node from a pod file by simply executing:
[CC3PODResourceNode nodeFromResourceFile:podFileName];
Here you can find a full working sample.
If your pod file is available online, you could first download it locally:
NSURL *url = [NSURL URLWithString:YOUR_URL_HERE];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
then pass filePath to nodeFromResourceFile.
filePath identifies a file residing in the user documents directory (one of the few places where you can write files to, under iOS):
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"PODFILE.pod"];
If you want to save to a temporary directory, use:
NSTemporaryDirectory()
instead of documentsDirectory.

Related

iOS : NSString file path to NSURL

I have this :
NSURL *url = [NSURL fileURLWithPath:#"/Users/myusername/Library/Application Support/iPhone Simulator/4.2/Applications/5ABF1395-4A80-46C0-BD4A-419ED98CE367/Documents/DBV/v.m4v"];
Then I launch movieViewController but it always crashes.. This code doesn't work on the iPhone simulator neither on the device... How can I fix it ?
EDIT :
Before to write file path by hand, I used the correct way to select a folder.
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
Then i got this log:
file://localhost/Users/myusername/Library/Application%20Support/iPhone%20Simulat‌​or/4.2/Applications/5ABF1395-4A80-46C0-BD4A-419ED98CE367/Documents/DBV/v.m4v
Then I thought it was because of spaces in folder name , so i decided to write the full path by hand for debugging (replacing each %20 by space)
EDIT 2 : Notice : I'm trying to access a dynamically created file in Documents folder, not a file from my bundle.
For files in documents, you should be getting the path from NSSearchPathForDirectoriesInDomains.
//get list of document directories in sandbox
NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex: 0];
Then you append the file name to that.

iphone writeToFile: problem

Is there any way to save my .xml file to another directory other than "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..."
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
NSLog(#"Error writing file !");
}
i wish to writeToFile: my .xml file to the desktop , any idea on how?
May the below code help,
NSString *documentsDirectory = #"/Users/student/Desktop/";
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
NSLog(#"Error writing file !");
}
From within the iPhone Simulator, you should be able to successfully use #"/Users/student/Desktop/accUserNames.xml" as the path to write to. However, you can't do this on an iOS device (you'll be restricted to the application's sandbox directory — it's recommended you write to the Documents folder or other folders in there, depending what type of data you're storing).
Edit: I think I understand your problem. This part of your code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
effectively finds the path to "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..." (this is the normal place you want to save things). But if you just want to save to the desktop temporarily, you should just use appFile = #"/Users/student/Desktop/accUserNames.xml".
Note: I don't advocate this as a long-term solution, but if you just want to see the output of your program temporarily, it works fine.
If you want to upload your file to a server ("www.blahblah.com" in your example), then using the write to file methods is not the correct approach. This is only for writing data to the local file system (or on a network share, but that doesn't apply to iPhones).
If you want to transfer data to a webserver, you will need to have something on the server that will listen for a connection request, then it will need to accept the data which is transferred from your app. You cannot just write a file to "www.blahblah.com" as you would to a file system
Probably not with code, but you could try this: Open "Automator" (in the Utilities folder) and chose "Folder Action". As Input folder, you specify the directory of the documents (/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/...) and then you select, from "Files & Folders", "Duplicate Finder Items" and "Move Finder Items" and select the Desktop. Hit "Save", give it a name, and all files in the documents folder should be copied to the desktop.

Simplest way on iPhone to unzip downloaded file?

Goal: download a zipped file, unzip it, and save it in the iPhone app's Documents directory.
The following code makes use of the initWithGzippedData method that was added to NSData in the Molecule app found here:
http://www.sunsetlakesoftware.com/molecules
As adapted to my app:
NSString *sFolder = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *sFileName = [sFolder stringByAppendingPathComponent:#"MyFile.db"];
NSURL *oURL = [NSURL URLWithString: #"http://www.isystant.com/Files/MyFile.zip"];
NSData *oZipData = [NSData dataWithContentsOfURL: oURL];
NSData *oData = [[NSData alloc] initWithGzippedData:oZipData];
[oZipData release];
b = [oData writeToFile:sFileName atomically:NO];
NSLog(#"Unzip %i", b);
Result: A zip file is successfully downloaded. From it a new, supposedly unzipped file is created in the Documents directory with the desired name (MyFile.db) but it has zero bytes.
Anybody see the problem? Or else is there a simpler way to unzip a downloaded file than the one used in the Molecules app?
I think that your problem may be that you are attempting to gzip-deflate a Zip file. Those are two different compression algorithms.
I based the gzip-deflating code in Molecules on this NSData category (the code of which I've copied into this answer) provided by the contributors to the CocoaDev wiki. What you'll want to do is use their -zlibDeflate implementation, which should properly unzip a Zip file.
Unrelated to your problem, instead of using NSHomeDirectory() and appending a path component, the recommended approach for finding the documents directory is the following:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
You should make sure your file is never too big, as you are loading it fully into memory before the unzip starts.

iphone storing and then reading a file from Documents folder

this must be easy but I want to put a file in the Documents folder which is read in at start up. I have the code on how to read and have confirmed its looking in the correct directory. However the file I have ,RootList.txt when saved in the Resources folder in xcode, is stored under the Root.app folder and the Documents folder is empty. Therefore its not finding this file when I start the app.
Is there a way to ensure a file is built into the Documents directory at start up (I'm running this in the simulator).
The alternative is a plist which works fine as well but I'm just curious.
In these situations I follow this approach:
First save your RootList.txt in Resources folder in xCode.You have nothing in your Documents folder, yet.
In the beginning of your applicationDidLaunch call, do:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:#"RootList.txt"];
if(![fileManager fileExistsAtPath:path])
{
NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:#"/RootList.txt"]];
[data writeToFile:path atomically:YES];
}
Now, your file is in Documents folder at startup.

How to grab property list file created in Xcode?

I am having a hard time figuring out how to get the property list file that was created using Xcode. I created a property list file using array with NSString members. I want to grab that file and get all the NSString members and save it to a UITextField. But my problem is that I can't see that file. I don't know if I'm looking in the wrong path, or I don't know where the property file is saved.
Most likely, if the file was added via Xcode, the file is in your bundle. To open it use:
NSDictionary *plist = [NSDictionary arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"NameOfFile" ofType:#"plist"]];
Run this in the simulator and then look in the Finder, in Library/Application Support/iPhone Simulator/Applications/... to see if you can find the file, open it in and double check it to make sure Xcode added the file.
To make a copy in your Documents folder:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"My.plist"];
if (![plist writeToFile:path atomically:YES])
NSLog(#"not successful in writing the high scores");