iOS App data store
Last Update:2015-07-10
Source: Internet
Author: User
<span id="Label3"></p>Common ways for iOS app data storage<pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="hljs haml">-<span class="ruby"> <span class="hljs-constant">XML属性列表(plist)归档-<span class="ruby"> <span class="hljs-constant">Preference(偏好设置)-<span class="ruby"> <span class="hljs-constant">NSKeyedArchiver归档(<span class="hljs-constant">NSCoding)-<span class="ruby"> <span class="hljs-constant">SQLite3 -<span class="ruby"> <span class="hljs-constant">Core <span class="hljs-constant">Data</span></span></span></span></span></span></span></span></span></span></span></span></code></pre></pre>Application sandbox <ul> <ul> <li>Each application has its own application sandbox (the application sandbox is the file system Directory) and is isolated from other file Systems. Apps must stay in their sandbox, other apps can't access the sandbox</li> <li>Apply Sandbox directory < assume app name layer><br></li> <li></li> </ul> </ul>Application Sandbox structure Analysis<p><p></p></p>Common ways to get sandboxed apps <ul> <ul> <li><p>Sandbox root directory: nsstring *home = nshomedirectory ();</p></li> <li><p>Documents: (2 Ways)<br>-<code>利用沙盒根目录拼接”Documents”字符串</code></p></li> </ul> </ul><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"><span class="hljs-built_in">NSString *home = NSHomeDirectory();<span class="hljs-built_in">NSString *documents = [home stringByAppendingPathComponent:<span class="hljs-string">@"Documents"];</span></span></span></code></pre></pre><p><p>Not recommended because a new version of the operating system may modify the directory name<br>-<code>利用NSSearchPathForDirectoriesInDomains函数</code></p></p><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"><span class="hljs-comment">// NSUserDomainMask 代表从用户文件夹下找<span class="hljs-comment">// YES 代表展开路径中的波浪字符“~”<span class="hljs-built_in">NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, <span class="hljs-literal">NO);</span></span></span></span></code></pre></pre><p><p><code>// 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素</code></p></p><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"><span class="hljs-built_in">NSString *documents = [array objectAtIndex:<span class="hljs-number">0];</span></span></code></pre></pre>Property List<pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="hljs objectivec">- 属性列表是一种XML格式的文件,拓展名为plist- 如果对象是<span class="hljs-built_in">NSString、<span class="hljs-built_in">NSDictionary、<span class="hljs-built_in">NSArray、<span class="hljs-built_in">NSData、<span class="hljs-built_in">NSNumber等类型,就可以使用writeToFile:atomically:方法直接将对象写到属性列表文件中</span></span></span></span></span></code></pre></pre> <ul> <ul> <li>Suppose you store an array<br> <ul> <li>Write file</li> </ul></li> </ul> </ul><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-obcj hljs objectivec"> <span class="hljs-comment">//directory : Gets which folder nscachesdirectory, searches for caches files <span class="hljs-comment">//domainmask: gets the scope under which the folder nsuserdomainmask, Indicates that the current user goes down to search <span class="hljs-comment">//expandtilde: whether to expand the full path <span class="hljs-comment">//stored under/library/caches Span class= "hljs-comment" >//gets The path of the caches folder <span class="hljs-built_in">nsstring *cachespath = Nssearchpathfordirectoriesindomains (nscachesdirectory, nsuserdomainmask, <span class="hljs-literal">YES) [<span class="hljs-number">0]; <span class="hljs-comment">//stitching file path <span class="hljs-built_in">nsstring *filepath =[cachespath Stringbyappendingpathcomponent:<span class="hljs-string">@ "user.plist"]; <span class="hljs-comment">/* * Write to file */[users writetofile:filepath atomically:<span class="hljs-literal">YES]; </span> </span></span></span></span></span></span></span></span></span></span></span></code></pre></pre><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="hljs haml">-<span class="ruby"> 读取文件 </span></code></pre></pre><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"> <span class="hljs-comment">/* * 读取文件 */ <span class="hljs-built_in">NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, <span class="hljs-literal">YES)[<span class="hljs-number">0]; <span class="hljs-built_in">NSString *path =[cachesPath stringByAppendingPathComponent:<span class="hljs-string">@"user.plist"]; <span class="hljs-built_in">NSArray *array =[<span class="hljs-built_in">NSArray arrayWithContentsOfFile:path]; <span class="hljs-built_in">NSLog(<span class="hljs-string">@"%@",array);</span></span></span></span></span></span></span></span></span></span></code></pre></pre>Preference settings<pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"><span class="hljs-comment">/* * NSUserDef Aults is used to make preferences store */<span class="hljs-comment">/* * advantages: no need to care about file name, The system will automatically create a key value pair for fast storage */<span class="hljs-comment">/* * Stored under library/preferences */<span class="hljs-comment">/* * Storage * * [[<span class="hljs-built_in">nsuserdefaults Standarduserdefaults]setobject:<span class="hljs-string">@ "jack" forkey:<span class="hljs-string">@ "account"]; [[<span class="hljs-built_in">nsuserdefaults standarduserdefaults]setobject:<span class="hljs-string">@ "123" Forkey:<span class="hljs-string">@ "pwd"]; [[<span class="hljs-built_in">nsuserdefaults standarduserdefaults]setbool:<span class="hljs-literal">YES forKey: <span class="hljs-string">@ "login"]; </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></code></pre></pre><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"> <span class="hljs-comment">/* * 读取 */ <span class="hljs-built_in">NSString *account = [[<span class="hljs-built_in">NSUserDefaults standardUserDefaults]objectForKey:<span class="hljs-string">@"account"]; <span class="hljs-built_in">BOOL login = [[<span class="hljs-built_in">NSUserDefaults standardUserDefaults]objectForKey:<span class="hljs-string">@"login"]; <span class="hljs-built_in">NSString *pwd = [[<span class="hljs-built_in">NSUserDefaults standardUserDefaults]objectForKey:<span class="hljs-string">@"pwd"]; <span class="hljs-built_in">NSLog(<span class="hljs-string">@"%@,%d,%@",account,login,pwd );</span></span></span></span></span></span></span></span></span></span></span></span></code></pre></pre>Archive <ul> <ul> <li>Person Object</li> </ul> </ul><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"><span class="hljs-preprocessor"># Import <span class="hljs-title"><foundation/foundation.h><span class="hljs-comment">/* * Be careful not to write nscopying */< Span class= "hljs-class" ><span class="hljs-keyword"> @interface <span class="hljs-title">person: <span class="hljs-title">nsobject<<span class="hljs-title">nscoding><span class="hljs-comment">/* * Age */< Span class= "hljs-keyword" > @property (<span class="hljs-keyword">nonatomic,<span class="hljs-keyword">assign) Span class= "hljs-built_in" >nsinteger age; <span class="hljs-comment">/* * Name */<span class="hljs-keyword"> @property (<span class="hljs-keyword">nonatomic,< Span class= "hljs-keyword" >strong) <span class="hljs-built_in">nsstring *name; </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></code></pre></pre><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"><span class="hljs-comment"><span class="hljs-comment">/* * profile properties */-(<span class="hljs-keyword">id) initwithcoder: (nscoder *) ADecoder{<span class="hljs-preprocessor"> #warning call Initwithcoder <span class as long the parent complies with nscodeing protocol>/* * UIView will call the Initwithcoder method when parsing */<span class="hljs-keyword">if (<span class="hljs-keyword">self = [<span class="]" hljs-keyword ">super Init") {<span class="hljs-comment">/* * File properties must be saved to the member variable */_age =[adecoder decodeintegerforkey:< Span class= "hljs-string" >@ "age"; _name = [adecoder Decodeobjectforkey:<span class="hljs-string">@ "name"];} <span class="hljs-keyword">return <span class="hljs-keyword">self;} <span class="hljs-comment">/* * tells the system which objects need to be archived */-(<span class="hljs-keyword">void) encodewithcoder: (nscoder *) aCoder {[acoder encodeobject:_name forkey:<span class="hljs-string">@ "name"]; [acoder encodeinteger:_age forkey:<span class="hljs-string">@ "age"];} </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></code></pre> <ul> <ul> <li> archive/solve file </li> </ul> </ul><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"> <span class="hljs-comment"><span class="hljs-comment">/* * Archive: Custom objects you want to store in a sandbox (folder) must use archive */<span class="hljs-comment">/* * Archive */person *per Son =[[person alloc]init]; Person<span class="hljs-variable">.age = <span class="hljs-number">10; Person<span class="hljs-variable">.name = <span class="hljs-string">@ "adobe"; <span class="hljs-comment">/* * Get Temp folder path */<span class="hljs-built_in">nsstring *temp =nstemporarydirectory (); Span class= "hljs-comment" >/* * Read Document folder path */<span class="hljs-built_in">nsstring *documentpath = Nssearchpathfordirectoriesindomains (nsdocumentationdirectory, nsuserdomainmask,<span class="hljs-literal">YES) [ <span class="hljs-number">0]; <span class="hljs-comment">/* * Stitching path */<span class="hljs-built_in">nsstring *filepath =[temp Stringbyappendingpathcomponent:<span class="hljs-string">@ "person.plist"]; [nskeyedarchiver Archiverootobject:person tofile:filepath]; </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></code></pre><pre class="prettyprint hljs-dark"><pre class="prettyprint hljs-dark"><code class="language-objc hljs objectivec"><span class="hljs-comment">/* * 解档 */ <span class="hljs-built_in">NSString *tempPath = NSTemporaryDirectory(); <span class="hljs-comment">/* * 拼接路径 */ <span class="hljs-built_in">NSString *filePath = [tempPath stringByAppendingPathComponent:<span class="hljs-string">@"person.plist"]; Person *p =[NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; <span class="hljs-built_in">NSLog(<span class="hljs-string">@"%@,%ld",p<span class="hljs-variable">.name,p<span class="hljs-variable">.age);</span></span></span></span></span></span></span></span></span></code></pre></pre><p><p><strong></strong></p></p><p><p>iOS App data store</p></p></span>