我正在使用UIActivityViewController来显示共享选项。我尝试共享的信息由两部分组成(纯字符串和 url(。共享正在通过所有扩展程序(如iMessages,Mail,Notes,Twitter,Facebook等(工作,但是当我尝试通过Slack共享时,该应用程序崩溃了。奇怪的是,只有在我不调试时才会发生崩溃,并且在调试时永远不会崩溃。
法典:
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;
}
我还注意到这种崩溃发生在Flipboard应用程序上,并且在Twitter应用程序上运行良好。
有人遇到过这个问题吗?如果是这样,您能让我知道问题是什么以及如何解决吗?
我最近遇到了听起来像同样的问题。当您在 activityItems/initWithActivityItems 数组中只有一个项目时,与 Slack 共享不会崩溃,但如果您有更多项目,它将崩溃。我不知道您是否遇到了该特定问题,但是解决方法是将URL和文本合并到一个字符串对象中,如果这适合您的用例。似乎是他们需要解决的 Slack 问题。
我有用。为 UIActivityItemSource 创建了一个类。我没有将 self 传递给 activityItems/initWithActivityItems 数组,我只是传入了 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