实例化UIActivity子类

时间:2018-03-28 06:19:55

标签: ios objective-c ios-app-extension uiactivity

当共享图像时,例如照片,我希望我的iOS应用程序扩展名出现在可用的应用程序列表中(邮件,消息...旁边),以便接收共享图片。阅读许多网站显示我必须将UIActivity子类化并将其添加到App Extension,我做了。

但是我不明白我应该如何实例化我的子类,它永远不会被调用(不显示NSLog,项目不在共享列表中)。

我应该如何指导" iOS要使用这个子类吗?

myActivity.m:

#import "myActivity.h"

@implementation myActivity
- (NSString *)activityType {

    // a unique identifier
    return @"com.myapp.uniqueIdentifier";
}

- (NSString *)activityTitle {

    // a title shown in the sharing menu
    return @"Custom Activity";
}

- (UIImage *)activityImage {

    // an image to go with our option
    return [UIImage imageNamed:@"MyImage"];
}

+ (UIActivityCategory)activityCategory {

    // which row our activity is shown in
    // top row is sharing, bottom row is action
    NSLog(@"UIActivityCategoryShare");
    return UIActivityCategoryShare;
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {

    // return YES for anything that our activity can deal with
    for (id item in activityItems) {

        // we can deal with strings and images
        if ([item isKindOfClass:[UIImage class]]) {
            return YES;
        }
    }
    // for everything else, return NO
    return NO;
}

- (void)prepareWithActivityItems:(NSArray *)activityItems {

    // anything we need to prepare, now's the chance
    // custom UI, long running calculations, etc

    // also: grab a reference to the objects our user wants to share/action
    self.activityItems = activityItems;
}

- (UIViewController *)activityViewController {

    // return a custom UI if we need it,
    // or the standard activity view controller if we don't
    return nil;
}

- (void)performActivity {

    // the main thing our activity does

    // act upon each item here
    for (id item in self.activityItems) {
        NSLog(@"YEY - someone wants to use our activity!");
        NSLog(@"They used this object: %@", [item description]);
    }

    // notify iOS that we're done here
    // return YES if we were successful, or NO if we were not
    [self activityDidFinish:YES];
}

@end

和myActivity.h:

#import <UIKit/UIKit.h>
@interface myActivity : UIActivity
@property (nonatomic, strong) NSArray *activityItems;

@end

0 个答案:

没有答案