Using AirDrop in our apps for files transferences between devices without using third parties

We recently had the need to share our iKaizen application data between different devices. Although the usual environment of using this app is in industrial environments, we needed that this data transfer doesn’t depend on any device, server, router, etc. a part of the iPads themselves.

To solve this need we decided to use the AirDrop function included by default on Apple devices. This functionality allows us to exchange any file between two apple devices by simply turning on the WiFi (no need to be connected to a network) and the bluetooth.

The first thing we need to use AirDrop is to create the file we want to share. To do this, all the data we want to introduce in the file are converted to NSData type.

    NSData *kaizenFileData = [NSKeyedArchiver archivedDataWithRootObject:self.sharedObject];

After this we create the file name (nameofthekaizen.ikaizen) and the path to the file in the application sandbox:

    NSString *fileName = [kaizenName stringByReplacingOccurrencesOfString:@" " withString:@""];
    fileName = [NSString stringWithFormat:@"%@.ikaizen", fileName];
    NSString *dataPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
    dataPath = [NSString stringWithFormat:@"file:/%@", dataPath];    
    NSString *outputDirectory = [NSString stringWithFormat:@"%@", NSTemporaryDirectory()];
    NSString *path = [outputDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", fileName]];

We create the url from the path so that the AirDrop functionality could access the file:

    NSURL *url = [NSURL fileURLWithPath:path];

And finally we create the file:

   [kaizenFileData writeToFile:path atomically:YES];

Once we have done this,  we just have to show the UIActivityViewController with the options to share the file. We create it as follows, using the url created earlier:

    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];

Dicho controlador tiene una propiedad “excludedActivityTypes”, en la que podemos indicar que opciones de compartición no queremos que se muestren en él, en nuestro caso, por ejemplo, vamos a quitar las opciones de imprimir y copiar al portapapeles así como las de compartir en las redes sociales: This controller has a property “excludedActivityTypes”, in which we can indicate the sharing options that we don’t want to show, in our case, for example, we are removing the options to print and copy to clipboard as well as sharing in social networks:

    NSArray *excludedActivities = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook,
                                    UIActivityTypePostToWeibo,
                                    UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
                                    UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
                                    UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
                                    UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo];
    controller.excludedActivityTypes = excludedActivities;

Finally we just have to show it so that the user could decide how to share it:   

    [viewController presentViewController:controller animated:YES completion:nil];

From this moment the operating system handles the data transfer, notifying the device that other device wants to send a file. If we want to receive the file, the device give us the option to open it using the app iKaizen, previously we have to add the following configuration in Xcode project (Info.plist file):

airdrop

By using this configuration, we are informing the operating system that a new file extension (.ikaizen) exists, and that these files could be opened with the app iKaizen. Once done, when receiving via AirDrop (or email, messages, etc.) a file of this type, the operating system will give us the option to open it with our app.

Finally for making the app which receives the file capable of it, we must add the following method to AppDelegate:

    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
        if (url){
            [[KZAirDropManager manager] didReceiveData:[NSData dataWithContentsOfURL:url]];
        }
        return YES;
    }

This method is called just after pressing “Open with iKaizen” and we pass to it the url where we can find the file sent by the other device. The only thing left to do is get the file data and treat them as we need to.

As you can see, AirDrop is a convenient way to exchange files, given the independence of third parties that it provide us, its transfer speed and its simple implementation, 100% recommended!.

Leave a Comment

Responsable » Solidgear.
Finalidad » Gestionar los comentarios.
Legitimación » Tu consentimiento.
Destinatarios » Los datos que me facilitas estarán ubicados en los servidores SolidgearGroup dentro de la UE.
Derechos » Podrás ejercer tus derechos, entre otros, a acceder, rectificar, limitar y suprimir tus datos.

By completing the form you agree to the Privacy Policy

¿Necesitas una estimación?

Calcula ahora