Tip:
Highlight text to annotate it
X
here's the problem that we're gonna sell got a very simple application here with
a text field
anna date picker
if i type in a value here let's try first to try
i could spend the day picker i'll select a value that's pretty
easy and memorable gently the first
and then we at the home button thanks a lot of it
go back in again
will see those values because we are moving from a foreground
tour background state just to regulate user interface elements have taken care
of by ohio s
however if we exit and either
double-click the home button
and decide to completely exit out of that application
or like that also
restart the simulator which would be like restarting the device
and then i open it up again
were backed a blank we're back to square one
because this information is not being persisted in any way
so we're going to save some data from the user interface
so it can be created
even if the iphone is restarted
i dot eight project here that have created some skelton placeholders for
just a break down what i have
in the user interface
all that i have here is a text field
there's a date picker and there's an invisible bun behind to make the text
field go away
and my header file
i have outlets for the text filled in the date picker
i have one i'd be action for dismissing the keyboard these on important but what
i have done
is defined three methods that i'm going to go and provide the code for
one to get a file path on the system
to to say some data and three to load some days i haven't done anything with
these yet
some in a jump over into the implementation fall where i can see my
three methods up here
this get file past the returns a string
right now
it's returning just an empty string just so this would compile
so all you need to provide some code
to provide a path
to this applications documents library
uh... they should be a really simple called ohio as saying
give me my directory
but it's a little longer than a strictly needs to be because we're still dealing
with the older styles
desktop coco functions rather than new
i west only once
some first going to create an array
and the corn going to do is to this rather long
and s search path for directories in domains which takes
a few different arguments here
oldest type them first before i talk about them
i pass in this function three parameters
and this document directory which means yes we're looking for the document
directory
anis user domain mask which is basically saying yes it's really just me
the current user
and then i'm using a boolean yes here but says
i want the full path i don't want any kind of relative path
now this is a regular
seat style function calls it doesn't need to be in square brackets
this will return me
anandessar a object
and when i see interested in is whatever's in the first position that
was a kid theoretically return me multiple paths
although i don't care about what's a position zero
some good to have a return statement against the path that position zero
which will be our document directory but i want to also attend a file name to it
so what i'm going to use
is strength by appending path component and then i'll just make up a name
for this file
i'll call it saved dot
p list
you can call it whatever you want
give myself a bit more room here
and that method will now returned the current file path to this applications
saved property last fall
so how would we use it
well here in the method called saved asia i'm actually going to writing code
to grab the values of the text field and the date picker and just store them in
the file
so i create an array and allocated just with
two things the value of the text field and of the date currently held in the
date picker that's all i need to do
so now i have an n s array and it knows how to save itself to a file
and it's going to say value
and i want to call the right to file
was the final well we can call the get file path
on the current object on self
anatomically which is putting in a boolean variable yes
they should be pretty instant
now what about file already existed while doesn't matter chiz kind overwrite
it
so that's me saving the state
of this publication
okay it's very simple it's just
to user interface elements but you could sub saving whatever you wanted here
annex we've got the load mathur so what actually happens if this application is
loading and trying to reason these values
well first
i've got to go and get the actual file paths all creating a string for this
i'm first on the usable helper method of the anna's file manager to say does
anything existed that path
if so then i can read it and
and fill the values on my user interface
and i'm basically going to do in reverse what i did in the same data
trading u_n_ ashrae
and initialize this with the contents of the file
which we just retrieved
a couple of months before
then simply sent the values of the tanks filled in the date picker
to the entries in that direction
i could use dot syntax like amusing here or i could have used the objective c
square brackets it doesn't matter
whatever's a position zero px goes into the text field
what i was a position one that goes into the dictator
but right now of course
none of these methods are being called nobody's collins save data nobody's
calling low data does nothing magical
about those method names
so the first the failing to say is
if this application is loading from device restart or simulator restart
we're going to call loads data
to ask if that file exists and if so
load the contents of the injuries user interface
well this one easy place to put this but it's not gonna fix all our problems
i'm going to put it here in view did load
just going to be self
cynical to los data
that will do it
the thing is view did load is only called when the apt is launched from
scratch this is not called when the m loose in the background to the
foreground and if we were worried about that
we have to be careful here 'cause we can't rely
any of these view based methods if you did unload view will appear view did
appear view will disappear view did disappear
none of these will be called when the background foreground operations are
happening
we are intrested
in the application level events
where intrested particularly
in application did enter background an application will enter foreground
with these get cold
in the amp delegate dot and file not in your view controller file
now there are ways
but we can
volunteer we can popped in our view controller to also
responds to these events
you know because the same data unload data methods are declared in our view
controller and the ab delegate created the view controller objects in the first
place
meaning that it did it
out here
as self dot view controller
i can actually call them directly here
so an application due to enter background i'm going to say self-taught
view controller to access that oneview control it
and we should have the accessible method save data
application will enter for ground
self
few controller
load data
save men to build this application build has succeeded i'll go ahead and run it
now before i actually testers
uh... disconnect quit back into xcode
have installed this application correctly the reason i just quit right
back out of it is when you were running directly from xcode and he decides to
both
leave the apt go back and terminated check a few things
extra can get
very confused indeed
so i ran it wants to install it on the simulator
and i understand your spotlight
freshly access the simulated directly
so we're not currently in debugging mode
amid a open up that application
wrong today and we've got a text field here
movement type something and just this saved
question mark
change the wheel to and earlier date like january fourteenth
methodist exit out of it go from the four go into the background
well this was working anyway
the real key issue
is can i exit the application by double clicking the home clicking the minus
button
and when this opens again
it still has the data
where actually loading it in from that file
i could even quit out of the simulation
then open it back up again
it's like a restart the device
click the apt
and it's loading that information
a technically speaking this was indeed even a little bit of overkill
before we were doing is trying to recreate the user interface it pretty
much does a good job of that anyway
but this would be
one basic approach for a
simple ability
to persist data to a file on the ohio west of us
the testing foreground and background events
in the simulator and using xcode can be a little tedious
has definitely one place where some patients
and some an *** long statements can come in very useful indeed