iOS 7 之Airdrop 分享

iOS 7 introduces a new easy to use way to share your data with nearby devicescalledAirDrop 

用过mbp的都应该知道苹果有个比较好用的文件共享组件Airdrop 随着新系统的发布Airdrop也登陆了iPhone上 遗憾的是只有5和5S上又这个功能

                                 

                                  

关于Airdrop就不做过多的介绍了 类似蓝牙但是效率要比蓝牙高不少貌似


使用很简单苹果已经把方法写到了这个UIActivityViewController类里面


you can use to allowusers to easily share photos, documents, URLs, and other kinds of data from yourapp 

在自己的APP中你可以通过Airdrop把照片、文件、链接等数据分享给其他的小伙伴


代码如下:


UIActivityViewController*controller =[[UIActivityViewControlleralloc]initWithActivityItems:objectsapplicationActivities:nil];

[selfpresentViewController:controlleranimated:YES completion:nil]; 


If the user chooses to send the data via AirDrop, once the other user receives thedata, it will be opened in an app based on the type of data:

NSStringandNSAttributedStringappear as new notes inNotes.app.

UIImageandAVAssetare presented inPhotos.app.

NSURLis presented inSafari.app, unless it is a file URL, which will make the iOSto look for registered document types (UTI) and open the appropriate app.

It’s as simple as that when it comes to sending data. When it comes to receivingdata, it works the same way as it has in iOS in the past:

1.Register document types. You register your app to receive certain documenttypes of data.

2.User chooses app. When the user receives a document (whether via email, aweb link, AirDrop, etc.), the OS looks to see which app handles that type ofdocument and asks the user what app he/she wants to use to open the file.

3.Receive and process document. If the user chooses to open the document inyour app, you receive a URL to the data in an App Delegate callback.

The rest of this chapter walks you through how both send and receive data viaAirDrop in a practical example project, but if this is enough to get you started, feelfree to stop reading at this point and give it a shot in your app. 


由于内容太多 先介绍分享任意数据的吧

iOS 7 Airdrop 允许用户分享任意数据类型的数据 只需要给他们一个 UTI(Uniform TypeIdentifier)就是数据类型标示符还必须得是唯一的


Registering UTIs

The interesting part of AirDrop is file sharing. You can share any data type — aslong as you turn it into a file first. For example, you can turn your custom objectsinto NSData, save them locally in a temporary folder with a custom Uniform TypeIdentifier (UTI), and then pass the file URL to AirDrop. 


新建DocumentsViewController.m 

代码如下:


  • -  (void)updateDocumentsToShareWithDocument:(NSURL*)document{

    ... insert at the end of the method implemetnation ...

    // Update object to share.

    self.objectsToShare= [self.toSharecopy];}

  • -  (void)updateViewWithNotification:(NSNotification*)notification{

    self.objectsToShare= nil;

    ... the rest of the code ...

The Documents tab displays the list of files in app’s Documents directory that endwith .dm. User can select one or more files to send via AirDrop. 


新建BaseViewController.h 


/**
@property objectsToShare
@brief An array of objects that will be shared via AirDrop when

the activity view controller is presented.*/ 

@property (nonatomicstrongNSArray *objectsToShare; 

objectsToShare holds a list of things to share via AirDrop. Each subclass will fillthis with the appropriate set of objects. 


// Create a UIBarButton item with UIBarButtonSystemItemAction anddisplay it in the navigation bar.
- (void)displayActionButton
{

UIBarButtonItem *actionButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(actionButtonTapped:)];

[self.navigationItem setLeftBarButtonItem:actionButtonanimated:YES];

}

// Remove the action bar button item.

- (void)hideActionButton{

[self.navigationItem setLeftBarButtonItem:nil animated:YES];} 


就不翻译了 自己看吧

displayActionButton and hideActionButton will be called when the list ofshareable objects changes. This pair of methods will initialize and display the actionbutton, or remove it from the navigation bar.

Next, add the following custom setter method: 

- (void)setObjectsToShare:(NSArray *)objectsToShare{

_objectsToShare = [objectsToShare copy];

// If there is an object in the array to share, display the// action button; otherwise, hide the action button.
if ([objectsToShare count])

[self displayActionButton];else

}

[self hideActionButton]; 


In order to have more control over what is being shared, you’re providing a customsetter method for objectsToShare. The method will check the count of objects andhide or display the action button depending on whether there are any entries in thearray.

Next up is the workhorse method to invoke the share sheet dialog.Still working in BaseViewController.m, add the following method: 


// Configure and present an instance of UIActivityViewControllerfor AirDrop only.
- (void)presentActivityViewControllerWithObjects:(NSArray*)objects

{

// 1 - Create an instance of UIActivityViewController with theobject.

UIActivityViewController *controller =[[UIActivityViewController alloc] initWithActivityItems:objectsapplicationActivities:nil];

// 2 - Exclude all activities except AirDrop.

NSArray *excludedActivities = @[UIActivityTypePostToTwitter,UIActivityTypePostToFacebook, UIActivityTypePostToWeibo,UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint,UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList,UIActivityTypePostToFlickr, UIActivityTypePostToVimeo,UIActivityTypePostToTencentWeibo];

controller.excludedActivityTypes = excludedActivities; 

// 3 - Present it.

[self presentViewController:controller animated:YEScompletion:nil];

1. Creates an instance of UIActivityViewController, passing in the array ofobjects.

2. Excludes all other possible options that UIActivityViewController can present— such as sharing via Twitter —since you want to limit the user to just AirDrop. 

3. Displays UIActivityViewController as modal.
Finally, link the above method with the action button in the navigation bar by

adding this method: 

- (IBAction)actionButtonTapped:(id)sender{

[selfpresentActivityViewControllerWithObjects:self.objectsToShare];} 


When the action button is tapped, this method ties things together by callingpresentActivityViewControllerWithObjects:with the current list of objects toshare. 


后面还会给大家介绍如何通过Airdrop分享图片、链接、媒体文件、文字等等。。。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值