2012-10-07 24 views
62

मैं आईओएस में एक कस्टम UIActivity कैसे बना सकता हूं?मैं आईओएस में कस्टम यूआईएक्टिविटी कैसे बना सकता हूं?

कारण मैं चाहता हूं कि यह मेरे ऐप्स में से एक में एक समीक्षा ऐप बटन जोड़ना है जो उपयोगकर्ता को ऐप स्टोर में समीक्षा अनुभाग में ले जाता है। मैं इस तरह के कस्टम UIActivity कैसे बना सकता हूं?

उत्तर

168

सबसे पहले, फ़ाइलें बनाएं। मैं नाम मेरा ActivityViewCustomActivity करने के लिए चुना

मेक इस तरह दिखना ActivityViewCustomActivity.h: यहाँ:

#import "ActivityViewCustomActivity.h" 

@implementation ActivityViewCustomActivity 

- (NSString *)activityType 
{ 
    return @"yourappname.Review.App"; 
} 

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

- (UIImage *)activityImage 
{ 
    // Note: These images need to have a transparent background and I recommend these sizes: 
    // [email protected] should be 126 px, iPadShare should be 53 px, [email protected] should be 100 
    // px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making. 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     return [UIImage imageNamed:@"iPadShare.png"]; 
    } 
    else 
    { 
     return [UIImage imageNamed:@"iPhoneShare.png"]; 
    } 
} 

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems 
{ 
    NSLog(@"%s", __FUNCTION__); 
    return YES; 
} 

- (void)prepareWithActivityItems:(NSArray *)activityItems 
{ 
    NSLog(@"%s",__FUNCTION__); 
} 

- (UIViewController *)activityViewController 
{ 
    NSLog(@"%s",__FUNCTION__); 
    return nil; 
} 

- (void)performActivity 
{ 
    // This is where you can do anything you want, and is the whole reason for creating a custom 
    // UIActivity 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=yourappid"]]; 
    [self activityDidFinish:YES]; 
} 

@end 

यह वही मेरी छवि की तरह दिखाई देता है:

#import <UIKit/UIKit.h> 

@interface ActivityViewCustomActivity : UIActivity 

@end 

ActivityViewCustomActivity.m इस तरह बनाएं .PSD मैंने बनाया है: - दुर्भावनापूर्ण लिंक हटा दिया गया - और यहां मूल 250 पीएक्स है .png http://i.imgur.com/pGWVj.png

अब आपके विचार नियंत्रक में ऐसा करते हैं:

#import "ActivityViewCustomActivity.h" 

और अब भी आप प्रदर्शित करने के लिए अपने UIActivityViewController हैं:

NSString *textItem = @"Check out the yourAppNameHere app: itunes http link to your app here"; 
    UIImage *imageToShare = [UIImage imageNamed:@"anyImage.png"]; 

    NSArray *items = [NSArray arrayWithObjects:textItem,imageToShare,nil]; 

    ActivityViewCustomActivity *aVCA = [[ActivityViewCustomActivity alloc]init]; 

    UIActivityViewController *activityVC = 
    [[UIActivityViewController alloc] initWithActivityItems:items 
                applicationActivities:[NSArray arrayWithObject:aVCA]]; 

    activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll]; 

    activityVC.completionHandler = ^(NSString *activityType, BOOL completed) 
    { 
     NSLog(@"ActivityType: %@", activityType); 
     NSLog(@"Completed: %i", completed); 
    }; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     self.popoverController = [[UIPopoverController alloc] initWithContentViewController:activityVC]; 

     CGRect rect = [[UIScreen mainScreen] bounds]; 

     [self.popoverController 
        presentPopoverFromRect:rect inView:self.view 
        permittedArrowDirections:0 
        animated:YES]; 
    } 
    else 
    { 
     [self presentViewController:activityVC animated:YES completion:nil]; 
    } 
+1

महान काम करता है! धन्यवाद। – OscarTheGrouch

+1

आईफोन और आईपॉड टच के लिए ऐप्पल मैनुअल के मुताबिक, छवियों 43 से 43 अंकों से बड़ी नहीं होनी चाहिए (जो रेटिना डिस्प्ले वाले उपकरणों के लिए 86 पिक्सेल से 86 के बराबर होती है।) आईपैड के लिए, छवियों को 55 x 55 अंक से बड़ा नहीं होना चाहिए (जो रेटिना डिस्प्ले के साथ आईपैड के लिए 110 से 110 पिक्सल के बराबर होना चाहिए।) – voromax

+0

ऐप्पल मैनुअल सबकुछ के लिए काम नहीं करता है। मेरे द्वारा चुने गए छवि आकार परीक्षण और त्रुटि से थे, और जो मुझे चाहिए उसके लिए पूरी तरह से काम करते थे। – klcjr89

22

यहाँ मेरी स्विफ्ट संस्करण है - मैं एक से अधिक कस्टम कार्रवाई की आवश्यकता इसलिए मैंने इस वर्ग को बनाया। प्रतिनिधियों या प्रोटोकॉल की कोई ज़रूरत नहीं है।

1. अपने दृश्य नियंत्रक में कस्टम कक्षा

class ActivityViewCustomActivity: UIActivity { 

    var customActivityType = "" 
    var activityName = "" 
    var activityImageName = "" 
    var customActionWhenTapped:((Void)-> Void)! 

    init(title: String, imageName:String, performAction: (() ->())) { 
     self.activityName = title 
     self.activityImageName = imageName 
     self.customActivityType = "Action \(title)" 
     self.customActionWhenTapped = performAction 
     super.init() 
    } 

    override func activityType() -> String? { 
     return customActivityType 
    } 

    override func activityTitle() -> String? { 
     return activityName 
    } 

    override func activityImage() -> UIImage? { 
     return UIImage(named: activityImageName) 
    } 

    override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool { 
     return true 
    } 

    override func prepareWithActivityItems(activityItems: [AnyObject]) { 
     // nothing to prepare 
    } 

    override func activityViewController() -> UIViewController? { 
     return nil 
    } 

    override func performActivity() { 
     customActionWhenTapped() 
    } 
} 

2 उपयोग करें

मैं एक UIBarButtonItem है, जो निम्नलिखित

@IBAction func actionButtonPressed(sender: UIBarButtonItem) { 

    var sharingItems = [AnyObject]() // nothing to share... 

    let myCustomActivity = ActivityViewCustomActivity(title: "Mark Selected", imageName: "removePin") { 
     println("Do something") 
    } 

    let anotherCustomActivity = ActivityViewCustomActivity(title: "Reset All", imageName: "reload") { 
     println("Do something else") 
    } 

    let activityViewController = UIActivityViewController(activityItems:sharingItems, applicationActivities:[myCustomActivity, anotherCustomActivity]) 

    activityViewController.excludedActivityTypes = [UIActivityTypeMail, UIActivityTypeAirDrop, UIActivityTypeMessage, UIActivityTypeAssignToContact, UIActivityTypePostToFacebook, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll] 

    activityViewController.popoverPresentationController?.barButtonItem = sender 

    self.presentViewController(activityViewController, animated: true, completion: nil) 
} 
कॉल करने के लिए इसे संलग्न किया है
+0

बेकार है कि ऐप्पल के पास डिफ़ॉल्ट रूप से यह नहीं है। – 3lvis

1

-activityViewCon का उपयोग करके एक ईमेल संगीतकार इंटरफ़ेस डालने का एक उदाहरण यहां दिया गया है यूआईएक्टिविटी की ट्रोलर विधि। इससे पता चलता है कि यूआईकिट व्यू कंट्रोलर या अपने स्वयं के कस्टम व्यू कंट्रोलर को आपके द्वारा चुने गए किसी भी उद्देश्य के लिए कैसे रखा जाए। यह supformActivity विधि supplants।

#import <MessageUI/MessageUI.h> 
#import <UIKit/UIKit.h> 

@interface EPSuggestionsActivity : UIActivity <MFMailComposeViewControllerDelegate> 

@end 

@implementation EPSuggestionsActivity 

.... 

- (UIViewController *)activityViewController{ 

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init]; 
    picker.mailComposeDelegate = self; 

    NSString *emailAddress = @"[email protected]"; 
    NSArray *toRecipients = @[emailAddress]; 
    [picker setToRecipients:toRecipients]; 
    [picker setSubject:@"Suggestions"]; 

    return picker; 
} 

#pragma mark - MFMailComposeViewControllerDelegate Method 

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { 

    [self activityDidFinish:YES]; // Forces the activityViewController to be dismissed 
} 

@end 

सूचना है कि -activityDidFinish मेल संगीतकार प्रतिनिधि से कहा जाता है उपयोगकर्ता ईमेल इंटरफ़ेस खारिज कर दिया है के बाद। गायब होने के लिए UIActivityViewController इंटरफ़ेस प्राप्त करना आवश्यक है। यदि आप अपना खुद का व्यू कंट्रोलर लिखते हैं तो उसे एक प्रतिनिधि विधि की आवश्यकता होगी जिसे समाप्त होने पर कॉल किया जाए और आपको प्रतिनिधि के यूआईएक्टिविटी का अपना उप-वर्ग बनाना होगा।

8

मेरे स्विफ्ट 3 कार्यान्वयन DogCoffee के के आधार पर:

class ActivityViewCustomActivity: UIActivity { 

    // MARK: Properties 

    var customActivityType: UIActivityType 
    var activityName: String 
    var activityImageName: String 
    var customActionWhenTapped:() -> Void 


    // MARK: Initializer 

    init(title: String, imageName: String, performAction: @escaping() -> Void) { 
     self.activityName = title 
     self.activityImageName = imageName 
     self.customActivityType = UIActivityType(rawValue: "Action \(title)") 
     self.customActionWhenTapped = performAction 
     super.init() 
    } 



    // MARK: Overrides 

    override var activityType: UIActivityType? { 
     return customActivityType 
    } 



    override var activityTitle: String? { 
     return activityName 
    } 



    override class var activityCategory: UIActivityCategory { 
     return .share 
    } 



    override var activityImage: UIImage? { 
     return UIImage(named: activityImageName) 
    } 



    override func canPerform(withActivityItems activityItems: [Any]) -> Bool { 
     return true 
    } 



    override func prepare(withActivityItems activityItems: [Any]) { 
     // Nothing to prepare 
    } 



    override func perform() { 
     customActionWhenTapped() 
    } 
} 
संबंधित मुद्दे