0

When sharing an image from, eg Photos, I want my iOS app extension to appear in the list of apps available (next to Mail, Messages...) in order to receive the shared picture. Reading many sites reveals I have to subclass UIActivity and add it to an App Extension, what I did.

However I don't understand how I should instantiate my subclass, and it never gets called (No NSLog is displayed, item not in the sharing list).

How should I "instruct" iOS to use this subclass ?

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

And myActivity.h:

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

@end
Laurent Crivello
  • 3,809
  • 6
  • 45
  • 89

0 Answers0