IOS development-detailed description of UIActivityViewController

Source: Internet
Author: User

IOS development-detailed description of UIActivityViewController

This was used during the sharing yesterday. Write something to record.


The UIActivityViewController class is a standard view controller. By using this controller, your applications can provide various services.

The system provides some common standard services, such as copying content to the clipboard, publishing an announcement to the social network, and sending content via email or SMS.

Applications can also customize services. (My sharing is a user-defined service, and I will write a tutorial later)

Your application is responsible for configuring, displaying, and dismissing this view controller.

The configuration of viewcontroller involves the specific data objects required by viewcontroller. (You can also specify a Custom Service list to allow applications to support these services ).

When displaying the view controller, you must use the appropriate method based on the current device type. On the iPad, you must use popover to display the view controller. The iPhone and iPodtouch must be displayed in a modal manner.


I. UIActivityViewController class reference

Inherited from

UIViewController: UIResponder: NSObject

Yes

NSCoding (UIViewController)

UIAppearanceContainer (UIViewController)

NSObject (NSObject)

Framework

/System/Library/Frameworks/UIKit. framework

Availability

IOS6 and later

Statement in

UIActivityViewController. h




II. Initialize Activity View Controller
- (id)initWithActivityItems:(NSArray *)activityItems applicationActivities:(NSArray *)applicationActivities;

Initialize Based on the specified data and return a new activity view controller.

Parameter 1. an array of data objects used by activityItems during activity execution. The object type in the array is variable and dependent on the data managed by the application. For example, data may be composed of one or more strings/image objects, representing the selected content.
Objects in the array can also be replaced by the UIActivityItemSource Protocol, for example, the UIActivityItemProvider object. The source and provider assume the role of the proxy, and then provide the corresponding data according to the actual situation. Here, the shared data includes text, images, and access URLs. Of course, these are optional. For example, if you do not want to add a url, you can only include text and images. This array cannot be nil. At least one object is required.


2. applicationActivities is an array of UIActivity objects, representing the custom services supported by applications. This parameter can be nil.

The Return Value Returns an activity view controller to be displayed.

Example

NSString * textToShare = @ "Text to be shared"; UIImage * imageToShare = [UIImage imageNamed: @ "ios0000.jpg"]; NSURL * urlToShare = [NSURL URLWithString: @ "http://blog.csdn.net/hitwhylz"]; NSArray * activityItems = @ [textToShare, imageToShare, urlToShare]; UIActivityViewController * activityVC = [[UIActivityViewController alloc] identifier: activityItems applicationActivities: nil];




3. Access Completion Handler

@property(nonatomic,copy) UIActivityViewControllerCompletionHandler completionHandler;  // set to nil after call
typedef void (^UIActivityViewControllerCompletionHandler)(NSString *activityType, BOOL completed);
When the activityview controller is fired, the completion handler is executed. This can be used to process some operations performed after activityview controller is used.

Whether you click "cancel" or select a service, this block will be called after it is finished, and you can capture the selected service for related operations.


The parameters of the block are as follows: 1. The activityType service type selected by the user. For a Custom Service, this value is returned by the activityType method of the UIActivity object. For activities defined by the system, this value is a string listed in "Built-inActivity Types" in the UIActivity class reference.

2. completed
If the service is executed, YES is returned. If NO, NO is returned. When a user does not select a service but disconnects view controller, this parameter is also set to NO.

Example

// Write a block to the activityVC attribute completionHandler. // It is called after the execution of UIActivityViewController and is used for subsequent processing. UIActivityViewControllerCompletionHandler myBlock = ^ (NSString * activityType, BOOL completed) {NSLog (@ "activityType: % @", activityType); if (completed) {NSLog (@ "completed ");} else {NSLog (@ "cancel");} // return to the upper-level interface [self. navigationController dismissModalViewControllerAnimated: YES];}; // initialize completionHandler. After the post ends (done or cancell), the blog will be called activityVC. completionHandler = myBlock;




4. ExcludedActivityTypes attributes

@property(nonatomic,copy)NSArray *excludedActivityTypes
By default, UIActivityViewController displays all services available for the provided content, but we can also exclude specific Activity types.
This requires the use of the excludedActivityTypes attribute, which can declare the list of services that we do not want to display.
Activity types are classified into two categories: "operation" and "share". The specific type can be distinguished by name.

UIKIT_EXTERN NSString *const UIActivityTypePostToFacebook     NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypePostToTwitter      NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypePostToWeibo        NS_AVAILABLE_IOS(6_0);    // SinaWeiboUIKIT_EXTERN NSString *const UIActivityTypeMessage            NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypeMail               NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypePrint              NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypeCopyToPasteboard   NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypeAssignToContact    NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypeSaveToCameraRoll   NS_AVAILABLE_IOS(6_0);UIKIT_EXTERN NSString *const UIActivityTypeAddToReadingList   NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIActivityTypePostToFlickr       NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIActivityTypePostToVimeo        NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIActivityTypePostToTencentWeibo NS_AVAILABLE_IOS(7_0);UIKIT_EXTERN NSString *const UIActivityTypeAirDrop            NS_AVAILABLE_IOS(7_0);

Each Activity type supports multiple data types. For example, a Tweet may consist of NSString and an additional image and/or URL.
Different Activity types support the following data types:

Example
// Exclude type, not displayed // default is nil. activity types listed will not be displayed activityVC. excludedActivityTypes = @ [UIActivityTypeAssignToContact, UIActivityTypePrint];


5. Display

When displaying the view controller, you must use the appropriate method based on the current device type. On the iPad, you must use popover to display the view controller. The iPhone and iPodtouch must be displayed in a modal manner.

Example

// Display activityVC in a modal mode. [Self presentViewController: activityVC animated: YES completion: nil];


After the demonstration, you will see the following:

Share and customize UIActivity.


On the way to learning, I will share with you


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.