Skip to main content

How to Use iOS - AirDrop File Sharing - Objective C

AirDrop File Sharing





Objective

The main objective of this blog post is to introduce AirDrop new feature of iOS 7
AirDrop:
AirDrop is a new feature in iOS 7, which is used to share file and data between iOS devices. Using AirDrop we can share photos, videos, location in Maps, application from App Store List, URLs etc. This new feature introduced by Apple works in all iPhone5 models, 4th Generation iPad and iPad mini and 5th generation iPod touch models. In iOS 7 SDK apple bundled UIActivityViewController class which we are using to share file and data. Using this class we just need to pass the objects, which we want to share. This example explains how to use this new UIActivityViewController class for file sharing.

Step 1 Open Xcode Project & select Template

First lets create one XCode project using Single View Application template name it as AirDropDemo as shown in below figure. 
create-template

Step 2 Fill project data

In the next step enter Product Name as AirDropDemo. And fill all other information as shown in below figure. 
choose-options-for-new-project


Step 3 AirDrop file Sharing

Using AirDrop file sharing is very easy we just need to enable AirDrop from Control Center as shown in below figure. Here if we choose Contacts Only then we can share file with the person who is there in our contacts list. And using everyone option we can share file with any person. 


airdrop-demo

To scan nearby devices AirDrop uses Bluetooth and when using Bluetooth connection is done, device will create an ad-hoc Wi-Fi network to connect devices with each other that provides better speed for data transmission. Here for AirDrop no need to connect devices in Wi-Fi network just we need to on Wi-Fi for data transmission.

Step 4 UITableView Delegate & dataSource Methods

Now in Xcode project lets create one ViewController named it FileListViewController in which we are displaying list of files, which we want to share using below code. Here files is a array of Files name which is created in viewDidLoad method.

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
      // Return the number of sections. return 1;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
      // Return the number of rows in the section. return [files count]; 
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
     cell.textLabel.text = [files objectAtIndex:indexPath.row]; return cell; 
}


Step 5 Load UIWebView



Next create another ViewController named it DocumentViewController in which we have done the logic to share files using AirDrop. In this class in viewDidLoad method we first load our file in webview using below code.

#pragma mark - View lifecycle 

- (void)viewDidLoad 
      [super viewDidLoad]; 
      NSURL *url = [self generateFileURL:self.documentName]; 
      NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:urlRequest]; 
}

Here we have used below generateFileURL: function to generate the NSURL from filepath.

- (NSURL *)generateFileURL:(NSString*)filename 


      NSArray *fileComponents = [filename componentsSeparatedByString:@"."]; 
      NSString *filePath = [[NSBundle mainBundle] pathForResource:[fileComponents objectAtIndex:0] ofType:[fileComponents objectAtIndex:1]]; 
      return [NSURL fileURLWithPath:filePath]; 
}


Step 6 Sharing Event

Now when user presses on share button we are presenting UIActivityViewController for file sharing. First create UIActivityViewController object with the array of object, which we want to share then present that ViewController.

- (IBAction)btnSharePress:(id)sender
      NSURL *url = [self generateFileURL:self.documentName]; 
      NSString *string = @"AirDrop File Sharing"; 
      UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[string, url] applicationActivities:nil]; 
      NSArray*excludedActivities=@[UIActivityTypePostToWeibo,UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo];

      activityViewController.excludedActivityTypes = excludedActivities; 
      [self presentViewController:activityViewController animated:YES completion:^{ }]; 
}


Step 7 Activity Item which Prevent for Sharing

Using AirDrop if we want to exclude some file sharing option then we can do it by assigning the array of activityItems which we don’t want to include in file sharing option using below code.
NSArray *excludedActivities = @[UIActivityTypePostToWeibo, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo];
activityViewController.excludedActivityTypes = excludedActivities;
I hope you find this tutorial helpful. If you are facing any issues or have any questions regarding AirDrop File Sharing please feel free to post a reply over here, I would be glad to help you.

Comments

Popular Posts

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode In iOS 8, Apple introduced  size classes , a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view. Different Size Classes There are two sizes for size classes:  compact , and  regular . Sometime you’ll hear about any.  Any  is the generic size that works with anything. The default Xcode layout, is  width:any height:any . This layout is for all cases. The Horizontal and vertical dimensions are called  traits , and can be accessed in code from an instance of  UITraitCollection . The  compact  size descr...

What are the Alternatives of device UDID in iOS? - iOS7 / iOS 6 / iOS 5 – Get Device Unique Identifier UDID

Get Device Unique Identifier UDID Following code will help you to get the unique-device-identifier known as UDID. No matter what iOS user is using, you can get the UDID of the current iOS device by following code. - ( NSString *)UDID { NSString *uuidString = nil ; // get os version NSUInteger currentOSVersion = [[[[[UIDevice currentDevice ] systemVersion ] componentsSeparatedByString: @" . " ] objectAtIndex: 0 ] integerValue ]; if (currentOSVersion <= 5 ) { if ([[ NSUserDefaults standardUserDefaults ] valueForKey: @" udid " ]) { uuidString = [[ NSUserDefaults standardDefaults ] valueForKey: @" udid " ]; } else { CFUUIDRef uuidRef = CFUUIDCreate ( kCFAllocatorDefault ); uuidString = ( NSString *) CFBridgingRelease ( CFUUIDCreateString ( NULL ,uuidRef)); CFRelease (uuidRef); [[ NSUserDefaults standardUserDefaults ] setObject: uuidString ForKey: @" udid " ]; [[ NSUserDefaults standardUserDefaults ] synchro...