Read a plist from website - ios

I am incorporating an image gallery into my project. It currently reads from a local plist file, and I need to change it to read from a web server. I've read a number of ways to do this, but can't make it work in my context. This seems maddeningly simple.
Below is my code:
NSString *plistPath =
[[NSBundle mainBundle] pathForResource:#"Images" ofType:#"plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfFile:plistPath];
You can view my plist here
This is what I've tried to no avail:
//get image URLs
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSDictionary *imagePaths = [[NSDictionary alloc] initWithContentsOfURL:plistPath];
I'd greatly appreciate anyone's help.

Try this and it should be work :
NSURL *plistPath = [NSURL URLWithString:#"http://kpd.altervista.org/Images.plist"];
NSArray *imagePaths = [NSArray arrayWithContentsOfURL:plistPath];
NSLog(#"%#", imagePaths);

The URL you provide serves a plist that contains an array.
Your code tries to read it as a dictionary.
Just change your code to read it as an array and you'll be one step closer.
But as others have said in the comments, you can't download a file (plist or otherwise) to your bundles directory. You have to download it to your Documents directory or similar.

Related

Generate a ppt file with byte array/NSData, in objective c?

I have a NSData value, generated from a byte array response. Now I need to save this as a .ppt file in iPhone/iPad, I learned from this link that there is no way to directly do it.
I already tried saving it as a PDF first, but failed in that too, since I'm unable to reproduce the charts from the nsdata.
Now is there another way to get this done other than sending the file through mail associated to the device?
Please do give out only the ways by which I can carry this out in the background without the help of the user? Any help will be appreciated.Thanks
I got it working, tried a lot but nothing worked and finally ran into some random code blocks, using which I got this wonderful piece of code,
//--------------------------------------------------------------------------------generating input URL
NSString *urlAddress = #"url containing the pdf data";
NSURL *theRessourcesURL = [NSURL URLWithString:urlAddress];
//--------------------------------------------------------------------------------defining file location details & writing a ppt file with a name "new.ppt"
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathFloder = [[NSString alloc] initWithString:[NSString stringWithFormat:#"%#",#"new.ppt"]];
NSString *defaultDBPath = [documentsDirectory stringByAppendingPathComponent:pathFloder];
NSData *tmp = [NSData dataWithContentsOfURL:theRessourcesURL];
[tmp writeToFile:defaultDBPath atomically:YES];

Get value from xxx.plist to Build Settings in Target

I have created a custom plist named as test.plist in Supporting Files folder. In that i stored App product name and App version number.
Now, how could i get product name from test.plist for PRODUCT NAME in Build Settings in Target.
I can get values from user defined build settings. But i don't want that.
Note: I need to store product name in test.plist only. And others can access the value from this file.
Thanks in Advance..
you can read the values from the plist in main bundle, put below code in viewDidLoad , method, Please note that all files in main bundle are read only if you want to write any data to plist then you have to copy it to app's document directory
NSString *bundle = [[NSBundle mainBundle] pathForResource:#”test” ofType:#”plist”];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:bundle];
NSString *productName = [dict objectForKey:#"ProductName"];
NSString * appVersion = [dict objectForKey:#"AppVersion"];
Another way is to read app properties
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleDisplayName"];
NSString appVersion = [NSString stringWithFormat:#"Version %# (%#)", [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"], kRevisionNumber];
Hope it helps!

Unable to read plist from a bundle outside my own app

What am I doing wrong? Trying to read Info.plist of Calendar.app into an array but I get nil array. Here is my code:
NSBundle* xyzbundle = [NSBundle bundleWithPath:#"/Applications/Calendar.app/Contents"];
// Path to the plist
NSString *path = [xyzbundle pathForResource:
#"Info" ofType:#"plist"];
// Build the array from the plist
NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];
// Show the string values
for (NSString *str in array2)
NSLog(#"--%#", str);
Thank you for your help.
You cant do it. iOS kernel creates a sandbox environment for each of the application. Any application can not read/write data of other application. There are some possible exceptions but in general you cant do it.

write to plist - error [duplicate]

I have created save.plist in a resource folder. I have written some data within that directly (without using coding). I am able to read that data but I'm not able to write through code to the same save.plist. By using following code I am trying to write the data but it gets stored within my .app plist.
The code is here
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"save" ofType:#"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSMutableDictionary *temp = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
if (!temp) {
NSLog(errorDesc);
[errorDesc release];
}
// [temp setValue:#"123" forKey:#"line1"];
// [temp writeToFile:plistPath atomically: YES];
//Reading data from save.plist
NSLog([temp objectForKey:#"name"]);
NSLog([temp objectForKey:#"wish"]);
NSNumber *num=[temp valueForKey:#"roll"];
int i=[num intValue];
printf("%d",i);
//writitng the data in save.plist
[temp setValue:#"green" forKey:#"color"];
[temp writeToFile:plistPath atomically: NO];
NSMutableDictionary *temp1 = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription:&errorDesc];
NSLog([temp objectForKey:#"color"]);
I want that, the data which I want to write should get written into save.plist only which is stored in references. I am new with this concept. So if anyone knows it please help me.
Thanks in advance.
:-)
I don't know if I understand your question, but if you want to write into a .plist within your .app bundle you are probably doing something wrong. If you want to store preferences, you should consider using NSUserDefaults.
If you really want to modify a bundled .plist - here is some code:
NSString *plistPath = nil;
NSFileManager *manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"Contents/Info.plist"])
{
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[infoDict setObject:[NSNumber numberWithBool:hidden] forKey:#"LSUIElement"];
[infoDict writeToFile:plistPath atomically:NO];
[manager changeFileAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] atPath: [[NSBundle mainBundle] bundlePath]];
}
}
Update:
Nate Flink pointed out that some of the NSFileManager methods used above are deprecated.
He posted an answer with the replacement methods below:
https://stackoverflow.com/a/12428472/100848
Updated version of the original awesome example by weichsel (thank you!). Xcode threw a couple warnings one of which is a deprecated method on NSFileManager. Updated here with non-deprecated methods from iOS 5.1
NSString *plistPath = nil;
NSFileManager *manager = [NSFileManager defaultManager];
if ((plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"mySpecial/PathTo.plist"]))
{
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[infoDict setObject:#"foo object" forKey:#"fookey"];
[infoDict writeToFile:plistPath atomically:NO];
[manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
}
}
When you build the app, it will create an executable file "appName.app" and all the files are built in the bundle. Therefore, you can't access to resource folder when the app is running because all the data is in the bundle(not in folder).
However, you can access to a temp folder which contains some information of the app.
You can find the temp folder here:
Open finder--click on your username(under PLACES)--Library--Application Support--iPhone Simulator--User--Applications--(here you can find all the temp folders of your iPhone apps)
You can access to this temp folder by:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
If you name your file save.plist, you can access to it like this:
NSString *filePath = [documentsDirectory stringByAppendingString:#"_save.plist"];
Then you just save your file to this filePath and it will appear in the temp folder named "Documents_save.plist".
*Note that the temp folder's name varies every time you run the app.
Recommend a book for you: 《Beginning iPhone Development--Exploring the iPhone SDK》. In Chapter 11 you can find what you want.
To summarize some of the other answers:
You're problem is that you're trying to write the file back into the folder that contains your application. That folder is not writable at runtime. Everything you're doing is fine, you just need to pick a different location to write your file to.
You can use the NSSearchPathForDirectoriesInDomains function to find a more suitable folder for this data. (Such as the #"Documents" folder.)
try this:
-(void)add:(NSRunningApplication *) app {
if ([self contains:app]) return;
[self.apps addObject:app.localizedName];
[self.apps writeToFile:self.dataFile atomically:YES];
}
from "Cocoa Programming".
you have to copy your plist into document directory...
because you cannot save anything without saving into document file....when you copied it will allow to write/modify on plist

NSSring from plist created from NSArray

I create a NSArray and write to a file: a.plist.
I use NSString: initWithContentsOfFile, and I can see the content in xml.
Then, I add a.plist to another project
and then I use NSString: initWithContentsOfFile to get the xml string.
filePath = [[NSBundle mainBundle] pathForResource:#"a" ofType:#"plist"];
NSString *plistStr = [[NSString alloc]initWithContentsOfFile:filePath];
However, it failed to recreate the xml string.
I user NSArray to test:
NSArray *plist2Array = [[NSArray alloc]initWithContentsOfFile:filePath];
But it successfully.
I think it may result from "Text Encoding" when I add it to another project.
The problem is I tried UTF8,UTF16 and so on.
I still can't find solution.
Hope for your help,thanks!
I find it!
rename the file: a.plist ->a.txt(or any else),
then in another project you can get the xml string.

Resources