Database Reference
In-Depth Information
if the serialization is successful. It just passes the result and the potential
error off to the completion block. It is up to the caller to handle any error in
the processing.
5.4
Importing Recipes
The reverse of exporting recipes is to be able to import them. The experience
we are looking for is as follows:
1.
The user receives a recipe in another application (for example, in Mail).
2.
The user taps the recipe, and it opens in our application.
3.
Our recipe application receives the data and consumes it.
To accomplish this workflow, we need to step into our
UIApplicationDelegate
and
do a few updates.
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
if
([self managedObjectContext]) {
[self consumeIncomingFileURL:url];
}
else
{
[self setFileToOpenURL:url];
}
return
YES;
}
The first change adds the method
-application:openURL:sourceApplication:annotation:
.
This method will be called whenever another application is requesting that
we open a file. If our application has been running for a while and the Core
Data stack is fully initialized, then we can process the file immediately. If the
stack has not fully initialized (for instance, it freshly launched with the
opening of the file), then we can store the
NSURL
and use it once the context
has been initialized.
//Finish UI initialization
if
(![self fileToOpenURL])
return
;
[self consumeIncomingFileURL:[self fileToOpenURL]];
}
The next change takes place in
-contextInitialized
. Once the context is fully initial-
ized, we can consume the
NSURL
that was passed in on launch. Since we are