Enable Data Sharing Feature in iOS App Using Airdrop

0
Shares
Enable Data Sharing Feature in iOS App Using Airdrop
Rate this post

iOS Airdrop Tutorial

Before the release of iOS 7, Apple users had to rely on various third-party apps for sharing files between iOS devices.

However, that’s not the case anymore!

You can now share data between iOS devices using Airdrop feature.

In iOS 7, Apple has announced a new feature called Airdrop to all iPhone 5 models, fifth-generation iPod models, and iPad mini. So basically, with Airdrop, you can share data such as photos, videos, URLs, app listings, location in Maps, and transfer files from mac to mac.

As an iOS developer, how would you feel knowing that you can add this feature in your native iOS app?

Sounds good, right?

In this iOS tutorial, we’ll demonstrate how you can add this feature in iOS app with a step-by-step process.

Let’s Get Started

  • Create a new project under the file menu and select “Single View Application”.

AirDrop1

  • In the next tab, give a name to your project. Here, we’re going to name it as SOAirDropShare.

AirDrop2

  • Next, add resources in your project in “Resources” folder.
  • After adding resources, we’ll show the list of object we need to share through Airdrop in table view in “ViewController.Swift”.
//MARK: UITableViewDataSource

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
if indexPath.row == 0 {
cell.textLabel?.text = "IMAGE"
} else if indexPath.row == 1 {
cell.textLabel?.text = "PDF"
} else if indexPath.row == 2 {
cell.textLabel?.text = "TEXT"
}

return cell
}
  • Now when you click on any row to share it, it’ll navigate to “DetailVC.Swift” with the file name.
//MARK: UITableViewDelegate

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let objDetailVC = self.storyboard?.instantiateViewControllerWithIdentifier("DetailVCID") as! DetailVC
if indexPath.row == 0 {
objDetailVC.fileName = "Airdrop.png"
} else if indexPath.row == 1 {
objDetailVC.fileName = "Airdrop.pdf"
} else if indexPath.row == 2 {
objDetailVC.fileName = "Airdrop.txt"
}

self.navigationController?.pushViewController(objDetailVC, animated: true)

}
  • Now in “DetailVC.swift”, get URL of a content to be shared by AirDrop.
let arrComponent = fileName.componentsSeparatedByString(".")
let filePath = NSBundle.mainBundle().pathForResource(arrComponent[0], ofType: arrComponent[1])

localFilePath = NSURL(fileURLWithPath: filePath!)

In this example project, we are installing content in UIWebView.

let request = NSURLRequest(URL: localFilePath)
web.loadRequest(request)
  • Now when we click on share button it will open an action sheet.
@IBAction func actionShare(sender: AnyObject) {
let objShare = [localFilePath]
let objActivityController = UIActivityViewController(activityItems: objShare, applicationActivities: nil)
// Exclude all activities except AirDrop.
let excludedActivities = [UIActivityTypePostToTwitter, UIActivityTypePostToFacebook, UIActivityTypePostToWeibo, UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo]
objActivityController.excludedActivityTypes = excludedActivities
// Present the controller
self.presentViewController(objActivityController, animated: true) {

}
}
  • You can see AirDrop option on top of action sheet.

AirDrop4

 

  • When you click on it, it will be the search nearest iOS devices.

AirDrop5

  • Once when you click on the nearest device it will send content(image/pdf/text) to that particular device.

AirDrop6

AirDrop7

AirDrop8

And done!

It was a quite simple, right? In case you face any problem, you can contact our developers for help.

However, if you’re not a techie, but wish to add this feature in your startup app, do contact us to hire iPhone app developer.

We’re an iPhone app development company in India with more than 5 years of experience. We’ve worked with 25+ successful Startups and helped build their startup mobile apps successfully. If you wish to know more about us, you can also check our portfolio. Having 100+ developers’ team, we follow the standard approach to build the iPhone apps and help our client succeed in their businesses.

You can download a free copy of this Airdrop demo from Github as well.

 
0
Shares
 

Want to Talk? Contact Us,

Get your free consultation now