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

How I Reduced the Size of My React Native App by 85%

How and Why You Should Do It I borrowed 25$ from my friend to start a Play Store Developer account to put up my first app. I had already created the app, created the assets and published it in the store. Nobody wants to download a todo list app that costs 25mb of bandwidth and another 25 MB of storage space. So today I am going to share with you how I reduced the size of Tet from 25 MB to around 3.5 MB. Size Matters Like any beginner, I wrote my app using Expo, the awesome React Native platform that makes creating native apps a breeze. There is no native setup, you write javascript and Expo builds the binaries for you. I love everything about Expo except the size of the binaries. Each binary weighs around 25 MB regardless of your app. So the first thing I did was to migrate my existing Expo app to React Native. Migrating to React Native react-native init  a new project with the same name Copy the  source  files over from Expo project Install all de...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

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 = { ...