IOS 6 Sharing list-Details of uiactivityviewcontroller

Source: Internet
Author: User

A share list view is provided after iOS 6, which is managed through uiactivityviewcontroller. Apple designed it to replace the actionsheet menu, which is an Action menu for sharing purposes.

You can share images with others by clicking the select button on the Action menu. However, as IOS functions continue to increase, more and more content needs to be shared, which will be presented in a modal view. The sharing list of iPad devices, which is displayed in the popover view.

We can see that there are many pages in the sharing list, and each page has nine applications. you can slide pages left and right to share more content.

Built-in activity list items

The sharing list uses uiactivityviewcontroller to control its rendering and closure. Any subclass objects that inherit the uiactivity abstract class can be displayed in the list.

The following example shows how to use uiactivityviewcontroller to manage the built-in activity list items. Click the action button on the left toolbar to display the share list modal view (shown in the figure below ), if we select Weibo, a dialog box will pop up (shown in the right figure). You can write Weibo here. After that, you can click "send" to send Weibo, if you have not set Weibo account information in system settings, a dialog box is displayed.

We will introduce the code implementation section below. For the code, refer to the preview action of viewcontroller. M: method:

-(Ibaction) Upload action :( ID) sender {nsstring * texttoshare = @ "Please log on to the IOS cloud and network communication service website ."; ① Uiimage * imagetoshare = [uiimage imagenamed: @ "ios0000.jpg"]; ② nsurl * urltoshare = [nsurl urlwithstring: @ "http://www.iosbook3.com"]; ③ nsarray * activityitems = @ [texttoshare, imagetoshare, urltoshare]; ④ uiactivityviewcontroller * activityvc = [[uiactivityviewcontroller alloc] initwithactivityitems: activityitemsapplicationactivities: Nil]; ⑤ // No activity Project activityvc exists. excludedactivitytypes = @ [uiactivitytypeprint, uiactivitytypecopytopasteboard, uiactivitytypeassigntocontact, parameters]; ⑥ [self presentviewcontroller: activityvc animated: true completion: Nil]; 7}

We chose Weibo and sent Weibo. These are all encapsulated by uiactivityviewcontroller. We don't need to write a line of code.

Custom activity list items

In some cases, the activity list items need to be automatically defined based on the shared content. For example, if your app is a video watching app, you may need to share the video to the "Youku" video website. The following section describes the items in the Custom activity list.

Any subclass objects that inherit the uiactivity abstract class can be displayed in the list. This includes 9 built-in activity lists in the IOS system or custom list items.

The following example shows how to use uiactivityviewcontroller to manage custom activity list items. Click the action button on the left toolbar to display the share list modal view, in the list, "Open Book" is a custom list item. Click "Open Book" to perform some operations. Here, the operation is to open the book website.

We will introduce the code implementation section below. For the code, refer to the preview action of viewcontroller. M: 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:activityItemsapplicationActivities:applicationActivities];  ③[self presentViewController:activityVC animated:YES completion:nil];}

Lateral Action: The method is similar to the built-in activity list item in the previous section. In this example, we only need one data item (URL), and the first line of code provides a URL. The second line of code instantiate the activity list item bookactivity, and then put it in an nsarray collection. The Code in line ③ instantiates the uiactivityviewcontroller object. In the constructor, The applicationactivities parameter is set to nil in the built-in activity list and the custom activity list item is set to nil, is the nsarray set.

The code for customizing the activity list item to implement 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 from the uiactivity. The attribute URL provides the list item service data items. The bookactivity. m code 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 ([activityItem 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

From IOS network programming and cloud application best practices: Guan Dongsheng @ Tony _ Guan Dongsheng

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.