iPhone - Save URL without setURL:forKey: and NSURL

无人久伴 提交于 2019-12-05 04:48:38

问题


Is there anyway to save a URL with NSUserDefaults without setURL:forKey: that is only available to iOS 4.0 and later?

I am loading HTML files locally with fileURLWithPath, and it starts at an intro page and the user can click through to whatever. For now, everytime a user starts over it loads back default to intro.htm. I would like to be able to save their current page to NSUserDefaults on viewdiddissapear and reload it next time, but can't find any solutions besides setURL:forKey:. Anyone out there know a solution?


回答1:


You can turn the URL into a string:

[[NSUserDefaults standardUserDefaults]
 setObject:[url absoluteString] forKey:@"url"];

And vice versa:

NSURL *url = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults]
                                   objectForKey:@"url"]];


来源:https://stackoverflow.com/questions/3818532/iphone-save-url-without-seturlforkey-and-nsurl

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