NSUrl is not stored as absolutestring in NSUserDefault

孤街浪徒 提交于 2019-12-23 09:09:10

问题


I am referring Previous Post for storing NSURL of iPod Library in NSUserDefault. But it is not stored in NSUserDefault after application is closed.

I am using other NSStrings to store in NSUserDefaults which is perfectly done, But when i store NSUrl as absolute string.. it does not stores the value.

What could be the reason??

EDIT

Following code i am using to save NSUserDefault Value:

currentItem = [collection.items objectAtIndex:songCount];
songURL = [currentItem valueForProperty:MPMediaItemPropertyAssetURL];
[[NSUserDefaults standardUserDefaults] setObject:[currentItem valueForProperty:MPMediaItemPropertyTitle] forKey:@"songTitle"];
[[NSUserDefaults standardUserDefaults] setObject:[songURL absoluteString] forKey:@"songURL"];
avPlayer = [[AVPlayer alloc] initWithURL:songURL];
NSLog(@"songTitle: %@  songURL : %@",[[NSUserDefaults standardUserDefaults]objectForKey:@"songTitle"], songURL);

Following error comes when i try to save the NSURL:

2011-09-13 18:47:23.258 Tabata Timer[933:707] songURL : ipod-library://item/item.mp3?id=-3715406019015217536
2011-09-13 18:47:23.258 Tabata Timer[933:707] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value 'ipod-library://item/item.mp3?id=-3715406019015217536' of class 'NSURL'.
2011-09-13 18:47:23.260 Tabata Timer[933:707] songTitle : Ghajini
2011-09-13 18:47:24.860 Tabata Timer[933:707] *** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value 'ipod-library://item/item.mp3?id=-3715406019015217536' of class 'NSURL'.
2011-09-13 18:47:24.963 Tabata Timer[933:707] songURL : ipod-library://item/item.mp3?id=-3715406019015217536
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.5 (8L1)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).

回答1:


If you want to store NSURL then why are you converting it into string and saving it as string. It will increase your work effort.

You can try this:

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];     
[defaults setURL:[NSURL URLWithString:@"http://www.google.com"] forKey:@"urlValue"];
[defaults synchronize];

NSURL *url=[defaults URLForKey:@"urlValue"];
NSLog(@"%@",url);



回答2:


The only thing I can think of that would explain it is that the URL is at least partially randomly generated and thus fails to load after you app removes reference to it. What do you mean, "closed"? Backgrounded? Or really terminated?

It could also be that you should be saving it as a string:yourURLString forKey:@"URL" instead of an object. It could be confusing the app. Try NSLog to output the URL string and see what you get. Let me know what it is. That will clarify things.




回答3:


your string should have property declared...then try to store it in NSUserDefaults



来源:https://stackoverflow.com/questions/7411508/nsurl-is-not-stored-as-absolutestring-in-nsuserdefault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!