[miss static iOS Development original Digest]-appdelegate store global variables and nsuserdefaults standarduserdefaults save and read data through models, store custom objects
Last Update:2015-06-18
Source: Internet
Author: User
<span id="Label3"></p><p><p>Because of the needs of app development, we need to get JSON format data from API interface and save the theme color and related URL of the temporary app.</p></p><p><p></p></p><p><p>There are a number of scenarios:</p></p><p><p>1, Save as global variable via appdelegate, then get</p></p><p><p>2, using Nsuserdefault</p></p><p><p></p></p><p><p>The first: by means of the appdelegate method:</p></p><p><p></p></p><p><p>Defining Global Variables</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> AppDelegate.h</span><span style="color: #008000;">/</span><span style="color: #008000;"></span>///<span style="color: #008000;"> Created by Missajj on 15/5/5.</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> Copyright (c) 2015 MISSAJJ. All Rights Reserved.</span> <span style="color: #008000;">//</span> <span style="color: #000000;"></span> <UIKit/UIKit.h><span style="color: #000000;"></span><uiapplicationdelegate ><span style="color: #000000;"></span><span style="color: #000000;"></span>*<span style="color: #000000;"></span>* * <span style="color: #000000;">globalabouttag; @end</span></pre></pre><p><p></p></p><p><p>Assigning values within a <span style="color: #000000;">APPDELEGATE.M</span> :</p></p><pre><pre>_globalappthemecolor =<span style="color: #000000;"></span>= appdetails.about;</pre></pre><p><p></p></p><p><p>In the desired VC header import</p></p><p><p>#import "AppDelegate.h"</p></p><p><p></p></p><pre><pre>-(<span style="color: #0000ff;">void</span><span style="color: #000000;">) viewdidload { [super viewdidload]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;"></span> appdelegate * appdelegate= (appdelegate*) [[uiapplication sharedapplication]<span style="color: #0000ff;">delegate</span><span style="color: #000000;">];}</span></pre></pre><p><p></p></p><p><p>Get variables</p></p><p><p>NSString *about = appdelegate.globalabouttag;<br>NSString *theme = appdelegate.globalappthemecolor;</p></p><p><p>A place where code needs are used flexibly</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">Navi set to global theme color</span> Self.navigationController.navigationBar.barTintColor = [uicolor colorwithhexstring: Appdelegate.globalappthemecolor alpha:<span style="color: #800080;">1</span>];</pre></pre><p><p></p></p><p><p>====== I am Gorgeous split line ^_^ =======</p></p><p><p></p></p><p><p></p></p><p><p>The second type: through the <strong>nsuserdefaults</strong> method:</p></p><p><p>Find the relevant information, self-organized a copy of the <strong>Nsuserdefaults</strong> Digest to have a common need for the program ape FRIENDS.</p></p><p><p>one,<strong>nsuserdefaults</strong> Simple method of application</p></p><p><p><strong>nsuserdefaults</strong> generally can access some short information, such as deposit and then read out a string to <strong>nsuserdefaults</strong></p></p><p><p></p></p><p><p>Note: The key value must be the same to be read out oh!</p></p><p><p>Nsuserdefaults only support: nsstring, nsnumber, nsdate, nsarray, nsdictionary, Not all the data can be put in the drop Oh ~</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Storing Data</span></span><span style="color: #000000;"><span style="color: #000000;">NSString</span></span>*<span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>= [nsstring stringwithstring<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">I want to store the string contents</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Nsuserdefaults</span></span>*userdefaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; [userdefaults setobject:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">string</span></span>Forkey:<span style="color: #800000;"><span style="color: #800000;">@ "theme"</span></span><span style="color: #000000;"><span style="color: #000000;">];<br><span style="color: #008000;">//where The <span style="color: #008000;">data is <span style="color: #008000;">stored</span> , don't forget this sentence</span> </span> [[nsuserdefaults standarduserdefaults] synchronize];<br><br></span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the data where it's needed</span></span>NSString *getstringvalue = [[nsuserdefaults standarduserdefaults] objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Theme</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>];<br><br><br></pre><p><p></p></p><pre><pre></pre></pre><p><p>second, If you need to save more data, you can save and read through the model</p></p><p><p></p></p><p><p>1. Model Code</p></p><p><p></p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">//</span> <span style="color: #008000;" areasmodel.h < span> <span style="color: #008000;">//</span> <span style="color: #008000;">//</span> <span style=" Color: #008000; ">//</span> <span style="color: #008000;"> Created by Missajj on 15/5/7. </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> Copyright (c) Missajj 2015. All Rights Reserved. </span> <span style="color: #008000;">//</span> <span style="color: #000000;"> #import </span> <foundation/foundation.h><span style="color: #000000;">@ Interface Areasmodel:nsobject </span> <span style="color: #008000; >/* </span> <span style=" color: #008000; "> * About Model </span> <span style="color: #008000;">*/</span> <span style="color: #000000;"> @property (nonatomic,copy) NSString </span> *<span style="color: #000000;"> about; @property (nonatomic,copy) nsstring </span> *<span style="color : #000000; "> appthemecolor; @end <br> <br> </span> </span></pre></pre><p><p></p></p><p><p>Remember that these two methods must be written in the M File.</p></p><p><p>-(id) initwithcoder: (nscoder *) Coder</p></p><p><p>-(void) encodewithcoder: (nscoder *) Coder</p></p><p><p>The custom class object is then encoded into the NSData and then read from the Nsuserdefaults.</p></p><p><p></p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">AREASMODEL.M</span></span><span style="color: #808080;"><span style="color: #808080;">///</span></span><span style="color: #008000;"><span style="color: #008000;">/</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Created by Missajj on 15/5/7.</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Copyright (c) 2015 MISSAJJ. All Rights Reserved.</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #000000;"><span style="color: #000000;">#import</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">AreasModel.h</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">@implementation Areasmodel</span></span>-(id) initwithcoder: (nscoder *<span style="color: #000000;"><span style="color: #000000;">) adecoder{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self =<span style="color: #000000;"><span style="color: #000000;">[super Init]) {self.about</span></span>= [adecoder decodeobjectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> about</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; Self.appthemecolor</span></span>= [adecoder decodeobjectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Appthemecolor</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;">self <span style="color: #000000;">;}</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>) Encodewithcoder: (nscoder *<span style="color: #000000;"><span style="color: #000000;">) Acoder{[acoder encodeObject:self.about forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> about</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [acoder EncodeObject:self.appThemeColor forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Appthemecolor</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; } @end</span></span></pre><p><p></p></p><p><p></p></p><p><p>2. Code to store data</p></p><p><p></p></p><pre>//////////////////////////<br>Above omitted .....<br>//////////////////////////<br><br> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">URL encoded into UTF8</span></span>Dirpath =<span style="color: #000000;"><span style="color: #000000;">[dirpath stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; Nsurl</span></span>* Dirurl =<span style="color: #000000;"><span style="color: #000000;">[nsurl urlwithstring:dirpath]; Nsmutableurlrequest</span></span>* Dirrequest =<span style="color: #000000;"><span style="color: #000000;">[nsmutableurlrequest requestwithurl:dirurl]; NSData</span></span>*dirjsondata =<span style="color: #000000;"><span style="color: #000000;">[nsurlconnection sendsynchronousrequest:dirrequest returningresponse:nil error:nil];</span></span><span style="color: #000000;"><span style="color: #000000;">nsdictionary</span></span>*dirlistjsondata = [nsjsonserialization Jsonobjectwithdata:dirjsondata options:<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">error:nil]; Nsdictionary</span></span>* Dicdata = [dirlistjsondata objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Data</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">#pragma</span></span>Mark = = Save temporary topic and about information = = =<span style="color: #000000;"><span style="color: #000000;">NSString</span></span>*about = [dicdata objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;"></span> about</span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; NSString</span></span>*theme = [dicdata objectforkey:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Theme</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; <span style="color: #008000;">// <span style="color: #008000;">Create model</span> </span> Areasmodel</span></span>*themeandabout =<span style="color: #000000;"><span style="color: #000000;">[[areasmodel alloc] init]; Themeandabout.about</span></span>=<span style="color: #000000;">about <span style="color: #000000;">; Themeandabout.appthemecolor</span></span>=<span style="color: #000000;"><span style="color: #000000;">theme; <span style="color: #000000;"><span style="color: #008000;">// <span style="color: #008000;">Save <span style="color: #000000;"><span style="color: #000000;"><span style="color: #008000;"><span style="color: #008000;">data, archive to Nsuserdefault</span></span></span></span> </span> </span></span> NSData</span></span>*themeandaboutdata =<span style="color: #000000;"><span style="color: #000000;">[nskeyedarchiver archiveddatawithrootobject:themeandabout]; [[nsuserdefaults standarduserdefaults] setobject:themeandaboutdata forkey:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Themeandabout</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; [[nsuserdefaults standarduserdefaults] synchronize];</span></span></pre><p><p></p></p><p><p>3. Get the Data code</p></p><p><p></p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">get saved data</span> NSData *getthemeandaboutdata = [[nsuserdefaults standarduserdefaults] objectforkey:<span style="color: #800000;">@ "</span><span style="color: #800000;">themeandabout </span><span style="color: #800000;">"</span><span style="color: #000000;"><br><br><span style="color: #008000;">// <span style="color: #008000;">turn into model to get data</span> </span> </span> *getthemeandabout =<span style="color: #000000;"> [nskeyedunarchiver unarchiveobjectwithdata:getthemeandaboutdata]; NSLog (</span><span style="color: #800000;">@ "</span><span style="color: #800000;">%@,%@</span><span style="color: #800000;">"</span>, getthemeandabout.appthemecolor, getthemeandabout.about);</pre></pre><p><p></p></p><p><p></p></p>= = The mind and body, there is always a on the road = = = As long as the road, will not back = =<p><p> [miss static iOS Development original Digest]-appdelegate store global variables and nsuserdefaults standarduserdefaults save and read data through models, store custom objects </p> </p></span>