Sunday, June 29, 2014

Airdrop - iOS 7. Programming

The UIActivityController class bundled in the iOS 7 SDK makes it simple for developers to integrate Airdrop feature into the applications. The application just needs to tell this class the objects it want to share and the UIActivityController will take care of the rest. 

Airdrop uses Bluetooth to scan nearby devices. When a connection is established via bluetooth, it will create an Adhoc WiFi network to link both the devices together allowing faster data transmission. It doesn't mean that the devices must be connected to the WiFi network but the WiFi device needs to be on for the data transfer. 

A UIActivityController is a standard view controller that provides several standard services such as copying item to clipboard, Sharing content to social media sites, sending items via messages etc. In iOS 7.0 SDK, this class comes with Airdrop feature built in. 

UIActivityController *controller = [[UIActivityController alloc]initWithActivityItems:objectsToShare applicationActivities:nil] ;
[self presentViewCotnroller:controller animated:YES completion:nil];

Optionally an application can exclude certain types of activities by providing excludeActivities Array. 

NSArray *excludeActivites = @[UIActivityTypePostToTwitter, UIActivityTypePostToFacebook, UIActivityTypePostToWeibo,UIActivityTypeMessage
    UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteBoard, UIActivytTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivtyTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWebo]

controller.excludedActivityTypes = excludedActivities; 

The above code will let the ActionSheet show only Airdrop in the action sheet 

Inorder to share the content from the application to the Airdrop recipient, application just needs to provide the correct path of the source application. For e.g. if the application is having the file in the Documents folder, Full path upto this folder needs to be passed. 


When the file is received on the other device, it get opened in the application which is registered in the corresponding content type. For e.g. if we just pass a string, it opens in note application. If its a pdf file it opens up in Safari, an image in Photos app. This is done using the UTI (Uniform Type Identifier. Each application would have registered using the corresponding type identifiers and OS will launch the corresponding app.

References:

No comments:

Post a Comment