ios7 uiactivityviewcontroller

####iOS7 Programming Cookbook 第一章学习笔记 UIActivityViewController

#####Presenting Custom Sharing Options with UIActivityViewController

#####StringReverserActivity.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
@interface  () <UIAlertViewDelegate>

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

@implementation

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
//点击button事件后调用
[self activityDidFinish:YES];
}

- (NSString *) activityType{
// NSLog(@"csj %@",[[NSBundle mainBundle].bundleIdentifier stringByAppendingFormat:@".%@", NSStringFromClass([self class])]);
//com.pixolity.ios.cookbook.Presenting-Custom-Sharing-Options-with-UIActivityViewController.StringReverserActivity
return [[NSBundle mainBundle].bundleIdentifier stringByAppendingFormat:@".%@", NSStringFromClass([self class])];
}

//反响字符串
- (NSString *) activityTitle{
return @"Reverse String";
}

//设置按钮图标
- (UIImage *) activityImage{
return [UIImage imageNamed:@"Reverse"];
}

//重写返回,默认不返回
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems{
/**
* (
Item 1,
Item 2,
Item 3
)

*/

for (id object in activityItems){
if ([object isKindOfClass:[NSString class]]){
return YES;
}
}

return NO;

}

//点击按钮
- (void) prepareWithActivityItems:(NSArray *)activityItems{
//创建可变数组
NSMutableArray *stringObjects = [[NSMutableArray alloc] init];
//快速枚举 item
for (id object in activityItems){
if ([object isKindOfClass:[NSString class]]){
[stringObjects addObject:object];
}
}

//copy给items
self.activityItems = [stringObjects copy];
}

//倒转 string
- (NSString *)reverseOfString:(NSString *)paramString{

NSMutableString *reversed = [[NSMutableString alloc]
initWithCapacity:paramString.length];

for (NSInteger counter = paramString.length - 1;//6-1 = 5(counter)
counter >= 0;
counter--){
[reversed appendFormat:@"%c", [paramString characterAtIndex:counter]];
}

return [reversed 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];

}

@end

#####ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


@implementation ViewController

- (void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];

//创建item数组
NSArray *itemsToShare = @[@"Item 1", @"Item 2", @"Item 3"];

//创建activity,进入view直接弹出
UIActivityViewController *activity = [[UIActivityViewController alloc]initWithActivityItems:itemsToShare applicationActivities:@[[StringReverserActivity new]]];

//modal弹出
[self presentViewController:activity animated:YES completion:nil];
}

@end

Reference