2013-05-20 43 views
6

希望大家都知道iOS 6包含ActionSheet (UIActivityViewController).的新樣式UIActivityViewController可以使用像字符串,網址,圖像等參數啓動。下面是該代碼片段(其中項目是一個數組與字符串和url參數)。iOS 6 - UIActivityViewController項目

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

但是,當我們選擇Mail,Facebook或Twitter等不同的共享選項時,我們可以分配不同的參數嗎?

一種方法是我們可以實現UIActivityItemSource,我們需要實現的源方法

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

它總是返回一個字符串值。但我需要傳遞一個數組,以便可以分配各種參數,如URL,圖像和標題。

任何想法我們如何能夠實現這一目標?

回答

28

對於內置iOS UIActivityViewController項目(如Mail,Facebook和Twitter),您無法更改任何內容。爲了實現UIActivityViewController中項目的自定義操作,您必須爲每個需要的自定義活動創建UIActivity的自定義子類。這裏有一個例子:

- (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非常有限的例子。

#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 
+5

是的,如果你打算回答投票答案,至少有共同的禮貌解釋原因。否則,有什麼意義? –