iOS基础 -- 自定义UIActivity

//
//  StringReverserActivity.h
//  iPhone_cookbook_2
//
//  Created by lawlielt on 13-12-31.
//  Copyright (c) 2013年 lawlielt. All rights reserved.
//

#import 

@interface StringReverserActivity : UIActivity

@end


//
//  StringReverserActivity.m
//  iPhone_cookbook_2
//
//  Created by lawlielt on 13-12-31.
//  Copyright (c) 2013年 lawlielt. All rights reserved.
//

#import "StringReverserActivity.h"

@interface StringReverserActivity ()
@property (nonatomic, strong) NSArray *activityItems;
@end

@implementation StringReverserActivity
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    [self activityDidFinish:YES];
}

/**
 *   return value of this method is an object of type NSString that is a unique identifier of your activity. This value will not be displayed to the user and is just for iOS to keep track of your activity’s identifier. There are no specific values that you are asked to return from this method and no guidelines available from Apple, but we will follow the reverse- domain string format and use our app’s bundle identifier and append the name of our class to the end of it. So if our bundle identifier is equal to edu.self.iPhone_cookbook_2 and our class name is StringReverserActivity, we will return edu.self.iPhone_cookbook_2.StringReverserActivity from this method
 */
-(NSString *)activityType{
    return [[NSBundle mainBundle].bundleIdentifier stringByAppendingFormat:@".%@",NSStringFromClass([self class])];
}

/**
 *  return a string to be displayed to the user in the activity view controller. Make sure this string is short enough to fit into the activity view controller
 */
-(NSString *)activityTitle{
    return @"Reverse String";
}

/**
 *  return an instance of UIImage that gets displayed in the activity view controller. Make sure that you provide both Retina and non-Retina versions of the image for both iPad and iPhone/iPod. The iPad Retina image has to be 110×110 pixels and the iPhone Retina image has to be 86×86 pixels. Obviously, divide these dimensions by 2 to get the width and the height of the non-Retina images. iOS uses only the alpha channel in this image, so make sure your image’s background is transparent and that you illustrate your image with the color white or the color black.
 */
-(UIImage *)activityImage{
    return [UIImage imageNamed:@"Reverse"];
}

/**
 *  This method’s parameter is an array that will be set when an array of activity items is passed to the initializer of the activity view controller. Remember, these are objects of arbitrary type. The return value of your method will be a Boolean indicating whether you can perform your actions on any of the given items or not. For instance, our activity can reverse any number of strings that it is given. So if we find one string in the array, that is good enough for us because we know we will later be able to reverse that string. If we are given an array of 1,000 objects that contains only 2 strings, we will still accept it. But if we are given an array of 1,000 objects, none of which are of our acceptable type, we will reject this request by returning NO from this method
 */
-(BOOL)canPerformWithActivityItems:(NSArray *)activityItems{
    for(id object in activityItems){
        if([object isKindOfClass:[NSString class]]){
            return YES;
        }
    }
    return NO;
}

/**
 *  This method gets called if you returned YES from the canPerformWithActivityItems: method. You have to retain the given array for later use. You don’t really actually have to retain the whole array. You may choose to retain only the objects that you need in this array, such as the string objects
 */
-(void) prepareWithActivityItems:(NSArray *)activityItems{
    NSMutableArray *stringObjects = [[NSMutableArray alloc] init];
    for (id object in activityItems){
        if ([object isKindOfClass:[NSString class]]){
            [stringObjects addObject:object];
        }
    }
    self.activityItems = [stringObjects copy];
}

-(void) performActivity{
    NSMutableString *reversedStrings = [[NSMutableString alloc] init];
    for (NSString *string in self.activityItems){
        [reversedStrings appendString:[self reverseOfString:string]];
        [reversedStrings appendString:@"\n"];
    }
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Reversed" message:reversedStrings delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
}

-(NSString *)reverseOfString:(NSString *)paramString{
    NSMutableString *reversed = [[NSMutableString alloc] initWithCapacity:paramString.length];
    for(NSInteger counter = paramString.length - 1;counter >= 0;counter--){
        [reversed appendFormat:@"%c",[paramString characterAtIndex:counter]];
    }
    return [reversed copy];
}
@end

在ViewController调用方法如下:

 NSArray *itemsToShare = [[NSArray alloc] initWithObjects:@"item1",@"item2",@"item3", nil];
    UIActivityViewController *activity = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:@[[StringReverserActivity new]]];
    [self presentViewController:activity animated:YES completion:nil];


你可能感兴趣的:(iOS)