2013-05-20 15 views
6

आशा है कि सभी को आईओएस 6 के बारे में पता है ActionSheet (UIActivityViewController). की नई शैली UIActivityViewController स्ट्रिंग, यूआरएल, छवि इत्यादि जैसे पैरामेंट्स के साथ शुरू की जा सकती है। नीचे कोड स्निपेट है (जहां आइटम एक सरणी है स्ट्रिंग और यूआरएल पैराम्स)।आईओएस 6 - UIActivityViewController आइटम

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

लेकिन, किसी भी तरह से है कि हम विभिन्न मापदंडों असाइन कर सकते हैं जब हम मेल, फेसबुक या ट्विटर की तरह अलग अलग शेयर विकल्प का चयन है?

एक विधि हम UIActivityItemSource, जहाँ हम स्रोत तरीकों

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

जो हमेशा एक स्ट्रिंग मान देता है लागू करने की आवश्यकता को लागू कर सकते है। लेकिन मुझे एक ऐरे पास करने की ज़रूरत है, ताकि मैं यूआरएल, छवि और शीर्षक जैसे विभिन्न मानकों को असाइन कर सकूं।

कोई विचार यह है कि हम इसे कैसे प्राप्त कर सकते हैं?

उत्तर

28

आप आईओएस UIActivityViewController आइटम जैसे मेल, फेसबुक और ट्विटर में निर्मित के लिए कुछ भी नहीं बदल सकते हैं। अपने UIActivityViewController में आइटम्स के लिए कस्टम क्रियाओं को लागू करने के लिए आपको अपनी इच्छित प्रत्येक कस्टम गतिविधि के लिए यूआईएक्टिविटी का एक कस्टम उप-वर्ग बनाना होगा। यहाँ एक उदाहरण है:

- (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; 
} 

तरीकों कि आप अपने कस्टम डेटा/क्रियाओं को संचालित करने अधिभावी में दिलचस्पी होगी पर प्रलेखन के साथ एक subclassed 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

हाँ, यदि आप नीचे मतदान करने जा रहे हैं तो कम से कम एक सामान्य सौजन्य है कि क्यों समझाया जाए। अन्यथा, बात क्या है? –