2016-1-7 the implementation of the first full app private address book 5: saving data
Last Update:2016-01-07
Source: Internet
Author: User
<span id="Label3"></p><p><p>One: Login Interface</p></p><p><p>1): After the user clicks the login button and successfully logs in, according to the switch situation at this time choose whether to save the data, the code is as Follows:</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>([acount isequaltostring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Zhangsan</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>]&&[password isequaltostring:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">123</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]) {[self performseguewithidentifier:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Contactsviewcontroller</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">sender:nil]; [mbprogresshud showsuccess:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Landing Success</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: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">after the user login successful, read the switch information at this time, and according to the circumstances of the User's account or password stored in the sandbox</span></span>Nsuserdefaults *defaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; [defaults SetObject:self.acountField.text forkey:accountkey]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(self.remerberSwitch.on) {[defaults SetObject:self.passwordField.text forkey:passwordkey]; } [defaults synchronize]; }</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{[mbprogresshud showerror:</span></span><span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Incorrect account password</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">]; }</span></span></pre><p><p>2): in the Viewdidload method to determine the switch situation at this time, choose whether to automatically fill the password or automatic login, the code is as Follows:</p></p><pre>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) Viewdidload {[super viewdidload]; Self.loginBtn.enabled</span></span>=<span style="color: #000000;"><span style="color: #000000;">NO;</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">read user preferences about switches from the sandbox</span></span>Nsuserdefaults *defaults =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; Self.loginSwitch.on</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults boolforkey:autologinswitchforkey]; Self.remerberSwitch.on</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults boolforkey:rememberpwdswitchforkey];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">read the user account or password from the sandbox, depending on the switch that has been read</span></span>Self.acountField.text =<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:accountkey]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(self.remerberSwitch.on) {self.passwordField.text</span></span>=<span style="color: #000000;"><span style="color: #000000;">[defaults objectforkey:passwordkey]; } [self textchange];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">determine if the automatic login switch is open and automatically log in</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">if (self.loginSwitch.on) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dispatch_after (dispatch_time (dispatch_time_now, (int64_t) (1.5 * nsec_per_sec)), dispatch_get_main_queue (), ^{< /c0></span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">[self loginbtnclicked];</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;">//</span></span><span style="color: #008000;"><span style="color: #008000;"> }</span></span></pre><p><p>3):</p></p><pre>-<span style="color: #000000;"><span style="color: #000000;">(ibaction) Rememberswitchchange {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If the automatic login is turned on at this time, and remember the password is off, automatic login should be turned off</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self.loginSwitch.isOn = = YES && Self.remerberSwitch.isOn = =<span style="color: #000000;"><span style="color: #000000;">NO) {[self.loginswitch Seton:no animated:yes]; }</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Save the switch data to the Sandbox.</span></span>Nsuserdefaults *defautls =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; [defautls SetBool:self.remerberSwitch.on forkey:rememberpwdswitchforkey]; [defautls SetBool:self.loginSwitch.on forkey:autologinswitchforkey]; [defautls synchronize]; }</span></span>-<span style="color: #000000;"><span style="color: #000000;">(ibaction) Autologinswitchchange {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">If the automatic login is turned on at this time, and remember the password is off, you should open the Remember password</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(self.remerberSwitch.isOn = = NO && Self.loginSwitch.isOn = =<span style="color: #000000;"><span style="color: #000000;">YES) {[self.remerberswitch Seton:yes animated:yes]; } nsuserdefaults</span></span>*defautls =<span style="color: #000000;"><span style="color: #000000;">[nsuserdefaults standarduserdefaults]; [defautls SetBool:self.remerberSwitch.on forkey:rememberpwdswitchforkey]; [defautls SetBool:self.loginSwitch.on forkey:autologinswitchforkey]; [defautls synchronize];}</span></span></pre><p><p>Ii: in Contactsviewcontroller</p></p><p><p>1);</p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">defines a file path</span> @property (copy, nonatomic) nsstring *contactpath;</pre></pre><p><p>Set file path properties at the same time lazy loading and data model array lazy loading</p></p><pre><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">lazy Load file path for contacts</span></span>-(nsstring *<span style="color: #000000;"><span style="color: #000000;">) contactpath{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">_contactpath) {nsstring</span></span>*doc =<span style="color: #000000;"><span style="color: #000000;">[nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, YES) lastobject]; _contactpath</span></span>= [doc Stringbyappendingpathcomponent:<span style="color: #800000;"><span style="color: #800000;">@"</span></span><span style="color: #800000;"><span style="color: #800000;">Contacts.acrhiver</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;"><span style="color: #000000;">_contactpath;}</span></span>-(nsmutablearray *<span style="color: #000000;"><span style="color: #000000;">) contacts{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">_contacts) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">get the data from the sandbox first</span></span>_contacts =<span style="color: #000000;"><span style="color: #000000;">[nskeyedunarchiver unarchiveObjectWithFile:self.contactPath];</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">NSLog (@ "%d", self.contacts.count); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">0x7fe0085f94a0</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">0x7ff800fb3c40</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(!<span style="color: #000000;"><span style="color: #000000;">_contacts) {nsmutablearray</span></span>*contacts =<span style="color: #000000;"><span style="color: #000000;">[nsmutablearray array]; _contacts</span></span>=<span style="color: #000000;"><span style="color: #000000;">contacts; } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">_contacts;}</span></span></pre><p><p>2): Add the following code in the proxy method of Addcontactviewcontroller and Edicontactviewcontroller:</p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;"> Sync the data to the Sandbox.</span> [nskeyedarchiver archiveRootObject:self.contacts toFile:self.contactPath];</pre></pre><p><p>three: actual effect:</p></p><p><p></p></p><p><p></p></p><p><p>2016-1-7 the implementation of the first full app private address book 5: saving data</p></p></span>