Adding AirDrop File sharing Feature to Your IOS Apps

Source: Internet
Author: User
Tags file url

http://www.appcoda.com/ios7-airdrop-programming-tutorial/


Adding AirDrop File sharing Feature to Your IOS AppsOctober, by Simon Ng, comments

AirDrop is Apple's answer to file and data sharing. Before the debut of iOS 7, users need to rely in 3rd-party apps such as Bump to share files between IOS devices. IOS 7, Apple introduced a new feature called AirDrop to all IPhone 5 models, the Fourth-generation ipad, the ipad Mini, and Fifth-generation IPod touch models. With AirDrop, you can easily share data with other nearby IOS devices. In brief, the feature allows-share photos, videos, contacts, URLs, Passbook passes, app listings on the app Store, Media listings on the ITunes Store, location in Maps, etc.

As a developer, wouldn ' t being great to incorporate the AirDrop feature in your app? So your users can easily share photos, the text file or any types of document easily with nearby devices. The Uiactivityviewcontroller class bundled in the IOS 7 SDK makes it simple for developers to integrate AirDrop feature in Their apps. The class shields you from the underlying details of the file sharing. What are you just need are tell the class the objects you want to share and it handles the rest. In this tutorial, we'll demonstrate the usage of Uiactivityviewcontroller and see how can I use it to share Images/doc Uments by using AirDrop.

Let ' s get started.

AirDrop Overview

Before we step into the implementation has a quick look at AirDrop. It's very simple-to-use AirDrop. Simply Bring up Control Center and tap AirDrop to enable it. You can either select ' Contact only ' or ' Everyone ' depending on whom your want to share the data with. If you choose the-only option, your device would only be discovered by people listed in your contacts. Obviously, your device can be discovered by anyone for the Everyone option.

AirDrop uses Bluetooth to scan for nearby devices. When a connection was established via Bluetooth, it ' ll create an Ad-hoc Wi-Fi network to link the devices together, all Owing for faster data transmission. It doesn ' t mean you need-connect the devices to a Wi-Fi network in order to use AirDrop. Your WiFi simply needs to is on for the data transfer.

Say want to share a photo in the Photos app from one iPhone to another. Assuming you ' ve enabled AirDrop on both devices, to share the photos with another device, tap the Share button (the one WI Th a arrow pointing up) at the lower-left of the screen.

In the AirDrop area, you should see the name of the devices that is eligible for sharing. AirDrop is isn't available when the screen is turned off. The sure the device on the receiving side was switched on. You can then select the device to share the photo. On the other device, you'll see a preview of the photo and a confirmation request. The recipient can accept or decline to receive the image. If you choose the Accept option, the photo was then transferred and automatically saved in the camera roll.

AirDrop doesn ' t just work with the Photos app. can also find the share option in the very most of the built-in apps such as Contacts, ITunes, App Store, Safari, to name a few . If you're new to AirDrop, you should now has a better idea.

Let's move on and see what we can incorporate AirDrop feature in your apps to share various types of data.

A Quick Look at Uiactivityviewcontroller

Think it ' ll take a lot of efforts to implement the AirDrop feature. Conversely, just need a few lines of code to add AirDrop support. The Uiactivityviewcontroller class available in IOS 7 SDK makes it super easy to integrate the feature. The AirDrop have been built into the class.

The Uiactivityviewcontroller class is a standard view controller, provides several standard services, such as copying Items to the Clipboard, sharing content to social media sites, sending items via Messages, etc. In IOS 7 SDK, the class comes with the AirDrop feature built-in.

Say, you have a array of objects to share using AirDrop. All initiate a uiactivityviewcontroller with the array of objects and present it need:

1
2
Uiactivityviewcontroller *controller = [[Uiactivityviewcontroller alloc] Initwithactivityitems:objectstoshare Applicationactivities:nil];
[Self Presentviewcontroller:controller animated:yes completion:nil];

With just-lines of code, you can bring up the activity view with AIRDROP option. Whenever there is a nearby device detected, the activity controller automatically shows the device and handles the data tr Ansfer if you choose to.

Optionally, you can exclude certain types of activities. Say, you can just display the AirDrop activity by excluding all other activities. Use the following code:

1
2
3
4
5
6
7
8
9
10
11
12
Uiactivityviewcontroller *controller = [[Uiactivityviewcontroller alloc] Initwithactivityitems:objectstoshare Applicationactivities:nil];

Nsarray *excludedactivities = @[uiactivitytypeposttotwitter, Uiactivitytypeposttofacebook,
Uiactivitytypeposttoweibo,
Uiactivitytypemessage, Uiactivitytypemail,
Uiactivitytypeprint, Uiactivitytypecopytopasteboard,
Uiactivitytypeassigntocontact, Uiactivitytypesavetocameraroll,
Uiactivitytypeaddtoreadinglist, Uiactivitytypeposttoflickr,
Uiactivitytypeposttovimeo, Uiactivitytypeposttotencentweibo];
Controller.excludedactivitytypes = excludedactivities;

[Self Presentviewcontroller:controller animated:yes completion:nil];

The activity View controller now is only shows the AirDrop option:

You can use Uiactivityviewcontroller to share different types of data including NSString, UIImage and Nsurl. Nsurl to share a link, it allows developers to transfer any types of the files by using file URL.

On the receiving side, when the other device receives the data, it ' ll automatically open an app based on the data type. Say, if an UIImage is transferred, the received image would be displayed in Photos app. When you transfer a PDF file, the other device would open it in Safari. If you just share a NSString object, the data would be the presented in Notes app.

A Glance at the AirDrop Demo App

To give your a better idea about Uiactivityviewcontroller and AirDrop, we ll build a AirDrop demo app. The app is very simple. When it was first launched, you'll see a table view listing a few files including an image file, a PDF file and a text file . You can tap the file and view the content. In the content view, there are an action button on the top-right corner of screens. Tapping it would bring up the AirDrop option and you can share the image or document with nearby device.

You ' re encouraged to build the demo app from scratch. But to save your time, you can download this project template to start with. When you open Xcode project, you should find the following Storyboard:

I have already implemented the Listtableviewcontroller and Documentviewcontroller for you. If you compile and run the app, you'll be presented with a list of files. When you tap any of the file, the image or document content would be displayed. But the share button isn't yet implemented and that's what we ' re going to talk about.

Adding AirDrop Feature

In the project template, the Listtableviewcontroller are used to displayed the list of files in a table view, while the DOC Umentviewcontroller presents the document content via a Web view. The action button in the document view was associated with the share:method of the Documentviewcontroller class. Edit the method with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-(Ibaction) share: (ID) Sender {
Nsurl *url = [self fileToURL:self.documentName];
Nsarray *objectstoshare = @[url];

Uiactivityviewcontroller *controller = [[Uiactivityviewcontroller alloc] Initwithactivityitems:objectstoshare 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];

}

If you're not forgetful, the above code should is very familiar to you as we've discussed it at the very beginning. The above code simply creates a Uiactivityviewcontroller, excludes all activities except AirDrop and presents the Controll Er as a modal view. The tricky part is what you define the objects to share. Here we turn the file to share into a Nsurl object and pass the file URL as an array to AirDrop.

The first and lines of code is responsible for the file URL conversion. The DocumentName property stores the current file (e.g. ios-game-kit-sample.pdf) displaying in document view. We simply the Filetourl:method with the document name and it returns the corresponding file URL. The Filetourl:method is bundled in the project template and here is the code:

1
2
3
4
5
6
7
-  (nsurl *)  filetourl: (nsstring*) filename
{
    nsarray * Filecomponents = [filename componentsseparatedbystring:@ "."];
    nsstring *filepath = [[nsbundle mainbundle] pathforresource:[ Filecomponents objectatindex:0] oftype:[filecomponents Objectatindex:1]];

    return [nsurl fileurlwithpath:filepath];
}

The code is very straightforward. For example, the ios-game-kit-sample.pdf is transformed to file:///Users/simon/Library/Application%20Support/ iphone%20simulator/7.0.3/applications/a5321493-318a-4a3b-8b37-e56b8b4405fc/airdropdemo.app/ Ios-game-kit-sample.pdf. The file URL varies depending on the device you ' re running. But the URL of should begin with the "file://" protocol. With the file URL of object, we create the corresponding array and pass it to Uiactivityviewcontroller for AirDrop sharing.

Build and Run the AirDrop Demo

You're done. That's need to implement AirDrop sharing. Compile and run the app on a real iPhone. Yes, you need a real device to test AirDrop sharing. The sharing feature won ' t work on the Simulator.

Uniform Type Identifiers (UTIs)

When you share a image to another IOS device, the receiving side automatically opens Photos app and loads the image. If you transfer a PDF file, the receiving device may prompt you to pick an app for opening the file or open it directly in IBooks. How can IOS know which app to use for the type of data?

UTIs (short for Uniform Type Identifiers) is Apple's answer to identify data handled within the system. In brief, a uniform type identifier are a unique identifier for a particular type of data or file. For instance, com.adobe.pdf represents a PDF document and Public.png represents a PNG image. You can find the full list of registered UTIs here. Application that is capable of opening a specific type of file have registered to handle this UTI with the IOS. So whenever this type of file is opened, IOS hands off that file to the specific app.

The system allows multiple apps to register the same UTI. In this case, IOS would prompt user with the list of capable apps for opening the file. For example, if you share a PDF document, your may experience the following screens in the receiving device:

Wrap up

AirDrop is a very cool feature introduced in IOS 7. It offers a great to share data between devices. Best of all, the built-in uiactivityviewcontroller have made it easy for developers to add AirDrop support in their apps. As can see from the demo app, it just needs a few lines of code to implement the feature. I highly recommend you to put the This sharing feature in your app.

For your complete reference, you can download the full source code of the "Xcode project from here."

As always, leave us comment and share your thought about the tutorial. We love to hear your feedback.

Adding AirDrop File sharing Feature to Your IOS Apps

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.