Skip to content

Instantly share code, notes, and snippets.

@fmtonakai
Last active December 15, 2015 08:59
Show Gist options
  • Save fmtonakai/5234731 to your computer and use it in GitHub Desktop.
Save fmtonakai/5234731 to your computer and use it in GitHub Desktop.
typedef id (^ActivitySourceWrapperExecuteBlock)(UIActivityViewController* activityViewController,NSString* activityType);
@interface ActivitySourceWrapper : NSObject<UIActivityItemSource>
@property id placeholder;
-(id)initWithBlock:(ActivitySourceWrapperExecuteBlock)block;
-(id)initWithBlock:(ActivitySourceWrapperExecuteBlock)block withPlaceholder:(id)placeholder;
@end
@implementation ActivitySourceWrapper
{
ActivitySourceWrapperExecuteBlock _block;
}
-(id)initWithBlock:(ActivitySourceWrapperExecuteBlock)block
{
return [self initWithBlock:block withPlaceholder:nil];
}
-(id)initWithBlock:(ActivitySourceWrapperExecuteBlock)block withPlaceholder:(id)placeholder
{
self = [super init];
if(self) {
_block = [block copy];
if(placeholder) {
self.placeholder = placeholder;
}
}
return self;
}
-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
return _block(activityViewController,activityType);
}
-(id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
if (self.placeholder) {
return self.placeholder;
}
return @"";
}
@end
#import "ActivitySourceWrapper.h"
@implementation ActivitySourceWrapperTest
+(void)presentShareViewController:(UIViewController *)controller withText:(NSString *)text withUrl:(NSURL *)url
{
id shareTextActivitySource = [[ActivitySourceWrapper alloc] initWithBlock:^id(UIActivityViewController *activityViewController, NSString *activityType) {
NSString *shareText = text;
if([activityType isEqualToString:UIActivityTypePostToTwitter]) {
shareText = [shareText stringByAppendingString:@" #ハッシュタグ"];
}
return shareText;
}];
id shareURLyActivitySource = [[ActivitySourceWrapper alloc] initWithBlock:^id(UIActivityViewController *activityViewController, NSString *activityType) {
return url;
}];
NSArray *activitySourceArray = @[shareTextActivitySource, shareURLyActivitySource];
UIActivityViewController *actViewCtr = [[UIActivityViewController alloc]initWithActivityItems:activitySourceArray applicationActivities:nil];
[controller presentViewController:actViewCtr animated:YES completion:^{}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment