Search code examples
iosairdrop

Can I send/receive sqlite Database by Airdrop


Is it possible to use Airdrop to send/receive a sqlite database (single file .sqlite) used by my app from one iPad to another? If so, how to I get to the file on the receiving device?

If not, what is the best option, from within an app, to send a file to the same app on a different device? Files will always be a sqlite database.


Solution

  • In case anyone stumbles onto this, the answer is YES, you it can be done. I'll outline the approach I took.

    First, I used a separate Core Data stack to create the sqlite file to be exported (in my case with some filtering, etc.), then renamed the sqlite on the sending device to have an extension that is unique to our app. Then, the following code is used to initiate the transfer:

    NSURL *url = [coreDataExporter urlToShare];
    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:[NSArray arrayWithObject:url] applicationActivities:nil];
    
    // Exclude all activities except AirDrop.
    NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                    UIActivityTypePostToWeibo,
                                    UIActivityTypeMessage, UIActivityTypeMail,
                                    UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                    UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                    UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                    UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
    controller.excludedActivityTypes = excludedActivities;
    
    // Present the controller
    [self presentViewController:controller animated:YES completion:nil];
    

    Use this link to set up your unique extension so it is recognized as a file belonging to your app:

    https://developer.apple.com/library/ios/qa/qa1587/_index.html

    You pretty much use that link as-is but substituting your own description, identifier, and extension. Much easier than some of what you'll find looking around the Internet. Takes five minutes.

    Once the transmission completes on the sending device, the file will reside in the Documents/Inbox directory as a read/delete only file. The App Delegate can implement the following method:

    • (void)applicationDidBecomeActive:(UIApplication *)application

    to copy the file wherever you need it on the receiving device. When the file is received on the receiving device it will know to start your app to process it and the DidBecomeActive will fire allowing you to handle it however you need to.

    Also, you can use:

    • (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

    in the app delegate to post a notification so that if the app is already active (thus DidBecomeActive doesn't fire) your app can listen for the notification to take appropriate action.

    It has been a pretty long slog to figure this out. Hopefully, posting this summary here will help someone down the road.