Exploring Airdrop

Airdrop introduced in OS X 10.7 and iOS 7 for the first time, the concept is based on creating an ad-hoc Wifi network by using Bluetooth as Transport layer , that's why sender and receiver dont need to be connected in a pre-registered network, Just be in a relatively close range with receiver then you can drop your data to Receiver!It's also secure by encrypting and configurable by authorized devices (for example You can authorize only devices from your contact list)  

To use Airdrop first prepare your data , Image, file, texts, ...NSData,... the only thing required to share your data is to adopt UIActivityItemSource protocol.

(UIActivityItemSource is the protocol if adopted by a data type , this data will be sharable by Airdrop)

then you submit your data to UIActivityViewController which in turn submit your data to lower level Airdrop support in IOS/OS X)

usually you dont need to focus on detail about Airdrop , everything is ready through UIActivityViewController.Initializer for UIActivityViewController has the prototype of:

init(activityItems activityItems: [AnyObject],applicationActivities applicationActivities: [UIActivity]?)

activityItems is the list of shareable items.

class MySharableData : NSObject, UIActivityItemSource {

    let sharableData: NSData

    let typeIdentifier: String

    let subject: String

    let previewImage: UIImage

    init(data: NSData, typeIdentifier: String, subject: String, previewImage: UIImage) {

        self.sharableData = data

        self.typeIdentifier = typeIdentifier

        self.subject = subject

        self.previewImage = previewImage


     @objc func activityViewControllerPlaceholderItem(activityViewController: UIActivityViewController) -> AnyObject {

        return sharableData


    @objc func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {

        return sharableData


    @objc func activityViewController(activityViewController: UIActivityViewController, subjectForActivityType activityType: String?) -> String {

        return subject


    @objc func activityViewController(activityViewController: UIActivityViewController, dataTypeIdentifierForActivityType activityType: String?) -> String {

        return typeIdentifier


    @objc func activityViewController(activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: String?, suggestedSize size: CGSize) -> UIImage? {

        // look at suggestedSize and resize image (see AirDrop sample code for how to do this)

        return previewImage



Then somewhere in your view controller you bind an outlet and call 

@IBAction func share(sender: AnyObject) {

  let itemSource = MySharableData(sharableData, "identifier", "subject", previewImage)

    let vc = UIActivityViewController(activityItems: [itemSource],applicationActivities: [])

    presentViewController(vc, animated: true, completion: nil)


 © Xosrov 2016