UIActivityViewController

UIActivityViewController

本文介绍了iOS 6 - UIActivityViewController项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望大家都知道iOS 6包含新样式的 ActionSheet(UIActivityViewController)。可以启动 UIActivityViewController 使用像string,url,image等参数。下面是代码片段(其中items是一个带字符串和url参数的数组)。

Hope everyone is aware of iOS 6 contains new style of ActionSheet (UIActivityViewController). The UIActivityViewController can be initiated with the paramentes like string, url, image etc. Below is the code snippet for that (where items is an array with string and url params).

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];

但是,当我们选择不同的共享选项(如Mail)时,我们是否可以分配不同的参数Facebook或Twitter?

But, is there any way that we can assign different parameters when we select different share options like Mail, Facebook or Twitter?

一种方法是我们可以实现UIActivityItemSource,我们需要实现源方法

One method is we can implement UIActivityItemSource, where we need to implement the source methods

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType

总是返回一个字符串值。但我需要传递一个数组,以便我可以分配各种参数,如URL,图像和标题。

which always returns a string value. But I need to pass an Array, so that I can assign various parameters like URL, image and a title.

我们知道如何实现这个目标吗?

Any idea how we can achieve this?

推荐答案

你可以不会为内置的iOS UIActivityViewController项目(如Mail,Facebook和Twitter)更改任何内容。要为UIActivityViewController中的项实现自定义操作,必须为所需的每个自定义活动创建UIActivity的自定义子类。下面是一个示例:

You can not change anything for the built in iOS UIActivityViewController items like Mail, Facebook and Twitter. In order to implement custom actions for items in your UIActivityViewController you must create a custom subclass of UIActivity for each custom activity you want. Here is an example:

- (UIActivityViewController *)getActivityViewController {
    MyFeedbackActivity *feedbackActivity = [[MyFeedbackActivity alloc] init];
    MyFacebookActivity *facebookActivity = [[MyFacebookActivity alloc] init];
    MyMailActivity *mailActivity = [[MyMailActivity alloc] init];

    NSArray *applicationActivities = @[feedbackActivity, facebookActivity, mailActivity];
    NSArray *activitiesItems = @[@"A string to be used for MyFeedbackActivity", @"A string to be used for MyFacebookActivity", @"A string to be used for MyMailActivity"];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activitiesItems applicationActivities:applicationActivities];

    // Removed un-needed activities
    activityVC.excludedActivityTypes = [[NSArray alloc] initWithObjects:
                                                     UIActivityTypeCopyToPasteboard,
                                                     UIActivityTypePostToWeibo,
                                                     UIActivityTypePostToFacebook,
                                                     UIActivityTypeSaveToCameraRoll,
                                                     UIActivityTypeCopyToPasteboard,
                                                     UIActivityTypeMail,
                                                     UIActivityTypeMessage,
                                                     UIActivityTypeAssignToContact,
                                                     nil];

    return activityVC;
}

一个非常有限的子类UIActivity示例,其中包含有关方法的文档有兴趣重写以处理您的自定义数据/操作。

A very limited example of a subclassed UIActivity with documentation on the methods that you will be interested in overriding to handle your custom data/actions.

#import "MyFeedbackActivity.h"

@implementation MyFeedbackActivity

- (NSString *)activityType {
    return @"MyFeedbackActivity";
}

- (NSString *)activityTitle {
    return @"Feedback";
}

- (UIImage *)activityImage {
    return [UIImage imageNamed:@"feedback"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
    return YES;
}

- (UIViewController *)activityViewController {
    /**
     * DESCRIPTION:
     * Returns the view controller to present to the user.
     * Subclasses that provide additional UI using a view controller can override this method to return that view controller. If this method returns a valid object, the system presents the returned view controller modally instead of calling the performActivity method.
     * Your custom view controller should provide a view with your custom UI and should handle any user interactions inside those views. Upon completing the activity, do not dismiss the view controller yourself. Instead, call the activityDidFinish: method and let the system dismiss it for you.
     */
}

- (void)prepareWithActivityItems:(NSArray *)activityItems {
    /**
     * DESCRIPTION:
     * Prepares your service to act on the specified data.
     * The default implementation of this method does nothing. This method is called after the user has selected your service but before your service is asked to perform its action. Subclasses should override this method and use it to store a reference to the data items in the activityItems parameter. In addition, if the implementation of your service requires displaying additional UI to the user, you can use this method to prepare your view controller object and make it available from the activityViewController method.
     */
}

-(void)performActivity {
    /**
     * DESCRIPTION:
     * Performs the service when no custom view controller is provided.
     * The default implementation of this method does nothing. If your service does not provide any custom UI using the activityViewController method, override this method and use it to perform the activity. Your activity must operate on the data items received in the prepareWithActivityItems: method.
     * This method is called on your app’s main thread. If your app can complete the activity quickly on the main thread, do so and call the activityDidFinish: method when it is done. If performing the activity might take some time, use this method to start the work in the background and then exit without calling activityDidFinish: from this method. Instead, call activityDidFinish: from your background thread after the actual work has been completed.
     */
}

@end

这篇关于iOS 6 - UIActivityViewController项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 12:06