IOS 6 Share list--uiactivityviewcontroller detailed

Source: Internet
Author: User
Tags uikit

IOS 6 Share list--uiactivityviewcontroller detailed

2013-06-03 01:42:33 Comment

A share list view is available after iOS 6, which is managed through Uiactivityviewcontroller. The main purpose of Apple's design is to replace the Share Action menu (Actionsheet) and share the Action menu as an Action menu for sharing purposes.

The selection button on the Action menu allows you to share the image with others. But as iOS continues to grow, more and more content needs to be shared, and it will be presented in a modal view. A shared list of ipad devices that will be presented in a popover view.

You can see that there are many pages in the share list, with 9 apps on each page, so you can share more content by sliding pages left and right.

Built-in Activity list item

The share list uses Uiactivityviewcontroller to control its rendering and closing, and all subclass objects that inherit uiactivity abstract classes can be rendered in the list.

Here we introduce an example of using Uiactivityviewcontroller to manage the built-in activity list item, which clicks on the action button in the left-hand toolbar and pops up the share list modal view (shown in the image below), if we select Weibo, The Weibo compose dialog will pop up (pictured on the right), where you can write a microblog, click the "Send" button to send a tweet, or a prompt dialog if no Weibo account information is set up in the system settings.

Let's look at the Code implementation section below, code reference VIEWCONTROLLER.M's ShareAction: Method:

-(Ibaction) ShareAction: (ID) Sender {      NSString *texttoshare = @ "Please log in to the iOS Cloud and network Communications Service website. ”; ①      UIImage *imagetoshare = [UIImage imagenamed:@ "iosshare.jpg"];②      nsurl *urltoshare = [Nsurl urlwithstring:@] Http://www.iosbook3.com "]; ③      Nsarray *activityitems = @[texttoshare, Imagetoshare, Urltoshare];       ④      Uiactivityviewcontroller *ACTIVITYVC = [[Uiactivityviewcontroller alloc]initwithactivityitems:activityitems      Applicationactivities:nil];  ⑤      //Not present active project      activityvc.excludedactivitytypes = @[uiactivitytypeprint, Uiactivitytypecopytopasteboard,      Uiactivitytypeassigntocontact,uiactivitytypesavetocameraroll]; ⑥      [self PRESENTVIEWCONTROLLER:ACTIVITYVC animated:true completion:nil];  ⑦      }

Select Weibo, and send Weibo these are all uiactivityviewcontroller for us, and we don't need to write a single line of code.

Customizing Activity list Items

Sometimes, depending on what we share, we need to automatically define the active list items. For example, if your app is a video-viewing app, you may need to share the video to "Youku" video sites. In this section, we describe the custom Activity list item.

All subclass objects that inherit uiactivity abstract classes can be rendered in the list. This includes the iOS system through the built-in list of 9 activities, or we can customize the list items.

Here we introduce an example of using Uiactivityviewcontroller to manage custom Activity list items, click the Action button in the Left Screen toolbar, and the share list modal view will pop up, where "open book" in the list is our custom list item, click " Open Book list item to do some action, here is the action is to open the book website.

Let's look at the Code implementation section below, code reference VIEWCONTROLLER.M's ShareAction: Method:

-(Ibaction) ShareAction: (ID) Sender {      Nsurl *urltoshare = [Nsurl urlwithstring:@ "http://iosshare.cn/"];①      Nsarray *activityitems = @[urltoshare];      Bookactivity *bookactivity = [bookactivity new];  ②      Nsarray *applicationactivities = @[bookactivity];      Uiactivityviewcontroller *ACTIVITYVC = [[Uiactivityviewcontroller alloc]      initwithactivityitems:activityitems      Applicationactivities:applicationactivities];  ③      [self PRESENTVIEWCONTROLLER:ACTIVITYVC animated:yes completion:nil];      }

ShareAction: The method is similar to the built-in activity list item in the previous section, in this case we only need one data item (that is, provide the URL), and the ① line code is the URL provided. The ② line code instantiates the active list item bookactivity and then needs to put it in a Nsarray collection. The ③ code instantiates the Uiactivityviewcontroller object, and the applicationactivities part of the constructor method is nil in the built-in activity list item, in the Custom Activity list item is the specified custom Activity list item object collection, which is Nsarray collection.

The code for the Custom Activity list Item code implementation BookActivity.h is as follows:

#import <UIKit/UIKit.h>                 @interface bookactivity:uiactivity                 @property (nonatomic,strong) Nsurl *url;                 @end

The custom Activity list item must inherit Uiactivity, and the property URL provides the data item for the list item service. The code for BOOKACTIVITY.M is as follows:

@implementation bookactivity-(NSString *) Activitytype {return Nsstringfromclass ([self class]); ①  }-(NSString *) Activitytitle {return nslocalizedstringfromtable (@ "Open book" @ "Bookactivity",  NIL);  ②}-(UIImage *) activityimage {return [UIImage imagenamed:@ "book"];③}- (BOOL) Canperformwithactivityitems: (Nsarray *) Activityitems {for (id activityitem in activityitems) {④if ([AC      Tivityitem Iskindofclass:[nsurl class]) {⑤if ([[UIApplication sharedapplication] Canopenurl:activityitem]) {⑥      return YES;      }}} return NO; }-(void) Preparewithactivityitems: (Nsarray *) Activityitems {for (id activityitem in activityitems)  {if ([Activityitem Iskindofclass:[nsurl class]]) {⑦_url = Activityitem; ⑧}}}-(void) performactivity {BOOL completed = [[UIApplication SharedapplicatiOn] openurl:_url];  ⑨[self activitydidfinish:completed]; ⑩} @end

IOS 6 Share list--uiactivityviewcontroller detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.