ios - App Crash while trying to share information through slack using UIActivityViewController


Keywords:ios 


Question: 

I am using an UIActivityViewController to show share options. The information I am trying to share consists of two pieces (a plain string and an url). The share is working through all the extensions like iMessages, Mail, Notes, Twitter, Facebook etc, but the app is crashing when I try to share through Slack. The weird thing is that the crash only happens if I am not debugging and it never crashes while debugging.

Code:

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[title, self] applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];

# UIActivityItemSource methods
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return url;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    return url;
}

- (NSString*)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return title;
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController dataTypeIdentifierForActivityType:(NSString *)activityType {
    return @"com.test.url";
}

- (UIImage *)activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(NSString *)activityType suggestedSize:(CGSize)size {
    return image;
}

I also noticed that this crash is happening on the Flipboard app and it's working fine on the Twitter app.

Did anyone run into this issue? If so can you please let me know what's the issue and how this can be fixed?


2 Answers: 

I've recently run into what sounds like the same issue. Sharing with Slack won't crash when you only have one item in the activityItems/initWithActivityItems array, but if you have more than that, it will crash. I don't know if you are experiencing that particular problem, but a workaround is to combine the url and text into one string object, if that will fit your use case. Seems like a problem with Slack that they need to address.

 

This worked for me. Created a class for UIActivityItemSource. Instead of passing in self to the activityItems/initWithActivityItems array, I just passed in the itemSource.

ActivityItemSource *itemSource = [[ActivityItemSource alloc] initWithTitle:title url:url thumbnail:thumbnail];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[title, itemSource] applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];

ActivityItemSource.m

@interface ActivityItemSource() <UIActivityItemSource>

@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSURL *url;
@property (strong, nonatomic) UIImage *thumbnail;
@end

@implementation ActivityItemSource

- (instancetype)initWithTitle:(NSString *)title url:(NSURL *)url thumbnail:(UIImage *)thumbnail {
    self = [self init];
    if (self) {
        self.title = title;
        self.url = url;
        self.thumbnail = thumbnail;
    }

    return self;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return self.url;
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
    return self.url;
}

- (NSString*)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return self.title;
}

- (UIImage *) activityViewController:(UIActivityViewController *)activityViewController thumbnailImageForActivityType:(NSString *)activityType suggestedSize:(CGSize)size {
    return self.thumbnail;
}

@end