Created
April 12, 2020 22:47
-
-
Save BandarHL/a0a3c5bd731c7f986388320d5235e8b0 to your computer and use it in GitHub Desktop.
Custom UIActivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomActivity.h | |
// RedditDownloader | |
// | |
// Created by BandarHelal on 18/08/1441 AH. | |
// | |
#import <UIKit/UIKit.h> | |
@interface CustomActivity : UIActivity | |
@property (nonatomic, strong) NSString *_title; | |
@property (nonatomic, strong) UIImage *_image; | |
@property (copy)void (^Action)(void); | |
- (instancetype)initWithTitle:(NSString *)Title image:(UIImage *)image performAction:(void(^)(void))Action; | |
@end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CustomActivity.m | |
// RedditDownloader | |
// | |
// Created by BandarHelal on 18/08/1441 AH. | |
// | |
#import "CustomActivity.h" | |
@implementation CustomActivity | |
- (instancetype)initWithTitle:(NSString *)Title image:(UIImage *)image performAction:(void(^)(void))Action { | |
self = [super init]; | |
if (self) { | |
self._title = Title; | |
self._image = image; | |
self.Action = Action; | |
} | |
return self; | |
} | |
- (NSString *)activityType { | |
return [NSString stringWithFormat:@"%@-BH", NSBundle.mainBundle.bundleIdentifier]; | |
} | |
- (NSString *)activityTitle { | |
return self._title; | |
} | |
- (UIImage *)activityImage { | |
return self._image; | |
} | |
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems { | |
return YES; | |
} | |
+ (UIActivityCategory)activityCategory { | |
return UIActivityCategoryAction; | |
} | |
- (void)performActivity { | |
self.Action(); | |
[self activityDidFinish:YES]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomActivity: UIActivity { | |
var _activityTitle: String | |
var _activityImage: UIImage? | |
var activityItems = [Any]() | |
var action: ([Any]) -> Void | |
init(title: String, image: UIImage?, performAction: @escaping ([Any]) -> Void) { | |
_activityTitle = title | |
_activityImage = image | |
action = performAction | |
super.init() | |
} | |
override var activityTitle: String? { | |
return _activityTitle | |
} | |
override var activityImage: UIImage? { | |
return _activityImage | |
} | |
override var activityType: UIActivity.ActivityType? { | |
return UIActivity.ActivityType(rawValue: "com.yoursite.yourapp.activity") | |
} | |
override class var activityCategory: UIActivity.Category { | |
return .action | |
} | |
override func canPerform(withActivityItems activityItems: [Any]) -> Bool { | |
return true | |
} | |
override func prepare(withActivityItems activityItems: [Any]) { | |
self.activityItems = activityItems | |
} | |
override func perform() { | |
action(activityItems) | |
activityDidFinish(true) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
obj-c: | |
CustomActivity *test = [[CustomActivity alloc] initWithTitle:@"test" image:[UIImage systemImageNamed:@"square.and.arrow.down.fill"] performAction:^{ | |
NSLog(@"Hello 1"); | |
}]; | |
CustomActivity *test2 = [[CustomActivity alloc] initWithTitle:@"test 2" image:[UIImage systemImageNamed:@"trash"] performAction:^{ | |
NSLog(@"Hello 2"); | |
}]; | |
UIActivityViewController *ActivityVC = [[UIActivityViewController alloc] initWithActivityItems:@[@"test"] applicationActivities:@[test, test2]]; | |
[self presentViewController:ActivityVC animated:true completion:nil]; | |
swift: | |
let test = CustomActivity(title: "test", image: UIImage(systemName: "square.and.arrow.down.fill")) { (_) in | |
print("Hello 1"); | |
} | |
let test2 = CustomActivity(title: "test 2", image: UIImage(systemName: "trash")) { (_) in | |
print("Hello 2"); | |
} | |
let ActivityVC = UIActivityViewController(activityItems: ["test"], applicationActivities: [test, test2]) | |
self.present(ActivityVC, animated: true, completion: nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment