this question is regarding xcode objective c and iphone development:
So I want to store an array in a new plist file and I know how to retrieve the file path and write the data into the file (at least I think I do) and all that jazz once the plist is created, but how do I actually create the plist file the first time the app is run or the first time I go to enter data into it? I want it to live in the documents folder of my app.
I'm assuming this is pretty simple I just can't seem to find documentation on it.
I ended up using NSKeyedValue there was a great tutorial here:
http://vimeo.com/1454094
I know technically this is not the answer to the question but it did solve my problem.
To save:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array writeToFile:[#"/path/to/file.plist"] atomically: TRUE];
To retrieve:
NSMutableArray *array = [[NSMutableArray arrayWithContentsOfFile:[#"/path/to/file.plist"]] retain];
[myArray writeToFile:aFile atomically:YES];
Related
I'm using the QuickDialog framework. It's an awesome framework but I have a strange problem.
I have a JSON that I got back from my Webservice. Everything is working, only it will not build the QPickerElements. I get the following warning in my console.
Couldn't build element for type QPickerElement
But now the strange thing. When I paste the code in the demo of the framework. Everything works like it is supposed to...
This is how I load up the JSON in my app.
NSString *jsonSample = data2.form;
NSLog(#"JSON SAMPLE IS %#",jsonSample);
id const parsedJson = [NSJSONSerialization JSONObjectWithData:[jsonSample dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
QRootElement *root = [[QRootElement alloc] initWithJSON:parsedJson andData:nil];
TestViewController *quickformController = (TestViewController *) [[TestViewController alloc] initWithRoot:root];
[quickformController setScriptId:_event.eve_id];
[self.navigationController pushViewController:quickformController animated:YES];
Can anybody help me with this problem?
I assume that you have the QuickDialog project embedded into yours as is the recommendation in quickdialog webpage, só, click on the quickdialog root project, then in target click in QuickDialog, in the right hand side select "Build Phases", add all .m (or whatever you want) into "Compiled Sources", then add the same .h into "Copy Headers"
I have cached the contents of a url using the following method and have saved the text as an NSUserDefault as I need to access it in another UIViewController.
This is the code that I am using to download the contents of a URL and save as an NSUserDefault and this is the code that I am using to load the contents of that NSUserDefault into a UITableView.
//Downloading URLcontents and save to an NSUser Default
NSString *url = [[NSString stringWithFormat:#"http://www.pharmacon.site50.net/database/1.xml"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *myTxtFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
[[NSUserDefaults standardUserDefaults] setObject:myTxtFile forKey:#"one"];
//Loading an NSUserDefault and placing it in a UITextView
NSString *myTxtFile = [[NSUserDefaults standardUserDefaults] stringForKey:#"one"];
textView.text=myTxtFile;
Here is what a typical XML file looks like that I have downloaded;
<item>
<din>1</din>
<category>Drugs</category>
<name>Paracetamol</name>
<phonetic>Pa-ra-ce-ta-mol</phonetic>
<spoken>Paracetamol.mp3</spoken>
<description>
Paracetamol is a drug commonly used for headaches and minor aches and pains.
</description>
<purpose>
Paracetamol is best used for minor to moderate aches and pains and can also be used to treat mild headaches.
</purpose>
<author>Sam Vale</author>
<editor>Harrison Fuller</editor>
</item>
Now, what I want to be able to do is read this NSUserDefault but actually extract just the text in the name region (so in this case, "Paracetamol) and then put that into the text of a UITableView cell, as the text of a button or a UILabel (choose whichever you reckon would be best or what you know).The reason I want to do this is cause I am going to display results that I download off the database in a UITableView.
Any help would be greatly appreciated.
You'll need NSXMLParser for this. There's plenty of examples on how to use it to parse XML available on the internet.
You know how you can Reset the coredata store on an iPhone simulator when you've changed your entity structure?
Do I need to perform a similar process when I've created a new version of my core data store that is different from what I last ran on my iPhone? If so, how, please?
Thanks
Just for convenience, until you code a way to remove the persistent store through your app, you can just delete the app off the phone. (Hold your finger on the home screen until icons get wiggly, then click the x on your app.) Then with your phone connected to your Mac, choose Product > Run in XCode and it will reinstall your app on the phone, but with empty data directories.
For deployment, of course, you need to come up with a way to do it without deleting the app, if you will ever change your data model after deployment (assume you will). Data migration is the best option, but if all else fails delete the persistent store file. It would be preferable to prompt for the user's approval before doing that. If they have important data they can decline and maybe get the old version of your app back to view the data and migrate it by hand, or they can wait until you release version 2.0.1 that fixes your data migration bug.
Here is the routine I use to reset my App content. It erases the store and any other file stored.
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:rootURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
for (NSURL *itemURL in content) {
[localFileManager removeItemAtURL:itemURL error:NULL];
}
[localFileManager release];
}
If you only want to erase the store, since you know its file name, you can refrain from enumerating the document directory content:
- (void) resetContent
{
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSString * rootDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *rootURL = [NSURL fileURLWithPath:rootDir isDirectory:YES];
NSURL *storeURL = [rootURL URLByAppendingPathComponent:#"myStore.sqlite"];
[localFileManager removeItemAtURL:storeURL error:NULL];
[localFileManager release];
}
But please note that in many cases, its better to migrate your store when you change your model, rather than to delete it.
locate your app in /Users/username/Library/Application Support/iPhone Simulator/4.3.2 (iOS Version may be different) and delete the .sqlite file
You can look at the path that is being sent to the persistentStoreCoordinator on setup, and remove that file. Usually the approach I have taken is that I set up the store to auto migrate, and if that fails I delete the store and attempt one more time to create the persistentStoreCoordinator which will use the now empty path.
Don't forget you may need to repopulate anything stored in the old database.
Is it possible to programatically find out name of all apps installed on my iOS device ?
Is there any API available for same ?
Thanks for the help
No, on iOS applications has no access to information of/about other applications due to sandboxed environment.
Yes it is possible to get list of all installed app
-(void) allInstalledApp
{
NSDictionary *cacheDict;
NSDictionary *user;
static NSString *const cacheFileName = #"com.apple.mobile.installation.plist";
NSString *relativeCachePath = [[#"Library" stringByAppendingPathComponent: #"Caches"] stringByAppendingPathComponent: cacheFileName];
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent: #"../.."] stringByAppendingPathComponent: relativeCachePath];
cacheDict = [NSDictionary dictionaryWithContentsOfFile: path];
user = [cacheDict objectForKey: #"User"];
NSDictionary *systemApp=[cacheDict objectForKey:#"System"];
}
systemApp Dictionary contains the list of all system related app
and user Dictionary contains other app information.
Not from the device. However, from the desktop you could peek into the iTunes library.
There are ways to do this without a jailbroken device and not get your app rejected.
1. get a list of currently running processes see this SO answer. You will need to translate from process name to app name.
2. Check to see if any apps have registered a unique URL scheme with UIApplicationDelegate canOpenURL. There are a few sites cataloging known url schemes, this is the best one.
If an app is not currently running and does not register a custom url scheme then it will not be detected by these methods. I am interested in hearing a method that will be allowed in the app store that works better than this.
try this, it will work even with non-jailbroken devices:
#include <objc/runtime.h>
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
SEL selector=NSSelectorFromString(#"defaultWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:selector];
SEL selectorALL = NSSelectorFromString(#"allApplications");
NSLog(#"apps: %#", [workspace performSelector:selectorALL]);//will give you all **Bundle IDS** of user's all installed apps
You can do it by checking whether an application is installed or not by using canOpenURL method or by checking the background processes and matching them with the name of the app you are interested in.
You can use runtime objective c to get the list of all installed apps. It will give you an array of LSApplicationProxy objects.
Following is a code snippet that prints Name of all applications installed in your device.
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
NSObject* workspace = [LSApplicationWorkspace_class performSelector:NSSelectorFromString(#"defaultWorkspace")];
NSMutableArray *array = [workspace performSelector:NSSelectorFromString(#"allApplications")];
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
for (id lsApplicationProxy in array) {
if(nil != [lsApplicationProxy performSelector:NSSelectorFromString(#"itemName")]){
[mutableArray addObject:[lsApplicationProxy performSelector:NSSelectorFromString(#"itemName")]];
}
}
NSLog(#"********* Applications List ************* : \n %#",mutableArray);
Don't forget to include <objc/runtime.h> .
I'm working on an app where a user can upload and download information. The information is downloaded in such a way that when it's downloaded, it's ordered according to when it was submitted.
What I'd like is for the user to be able to reorganize the table view, save the order to a preference file (all ready working) and from now on, whenever data is request, the order now conforms to the users re-organized list.
Maybe my brains just fried, but is there an array function that can accomplish this, or am I on my own? Any suggestions how to go about writing something like this?
Thanks
What about NSSortDescriptor:
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:
[NSArray arrayWithObject:
[[[NSSortDescriptor alloc] initWithKey:#"keyToBeSorted"
ascending:YES
selector:#selector(localizedCaseInsensitiveCompare:)] autorelease]]];