ios小白一个,最近公司让接直接接系统分享,遇到问题,一直调不起ios系统分享,看日志如下
Warning: Attempt to present <UIActivityViewController: 0x1058c1600> on <SystemShareController: 0x10504d160> whose view is not in the window hierarchy!
废话不多说,代码如下
-(void) startSystemShare {
NSLog(@"系统分享 系统分享 startSystemShare ");
// 1、设置分享的内容,并将内容添加到数组中
NSString *shareText = @"我的个人博客";
UIImage *shareImage = [UIImage imageNamed:@"Icon-58.png"];
NSURL *shareUrl = [NSURL URLWithString:@"http://blog.youkuaiyun.com/flyingkuikui"];
NSArray *activityItemsArray = @[shareText,shareImage,shareUrl];
// 自定义的CustomActivity,继承自UIActivity
CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle:@"wangsk" ActivityImage:[UIImage imageNamed:@"custom.png"] URL:[NSURL URLWithString:@"http://blog.youkuaiyun.com/flyingkuikui"] ActivityType:@"Custom"];
NSArray *activityArray = @[customActivity];
// 2、初始化控制器,添加分享内容至控制器
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItemsArray applicationActivities:activityArray];
activityVC.modalInPopover = YES;
// 3、设置回调
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
// ios8.0 之后用此方法回调
UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
NSLog(@"test 系统分享 activityType == %@",activityType);
if (completed == YES) {
NSLog(@"test 系统分享 completed");
}else{
NSLog(@"test 系统分享 cancel");
}
};
activityVC.completionWithItemsHandler = itemsBlock;
}else{
// ios8.0 之前用此方法回调
UIActivityViewControllerCompletionHandler handlerBlock = ^(UIActivityType __nullable activityType, BOOL completed){
NSLog(@"test 系统分享 activityType == %@",activityType);
if (completed == YES) {
NSLog(@" test 系统分享 completed");
}else{
NSLog(@"test 系统分享 cancel");
}
};
activityVC.completionHandler = handlerBlock;
}
// 4、调用控制器
[self presentViewController:activityVC animated:YES completion:nil];
}
也是各种谷歌百度
解决这个问题修改了一点点代码
// 4、调用控制器
AppController *app = (AppController *)[UIApplication sharedApplication].delegate;
[app.viewController presentViewController:activityVC animated:YES completion:nil];
然后ios系统分享就可以调出来了,接下来还要走系统分享的排序和筛选,干活去............
代码是借用这位前辈的
https://github.com/FlyingKuiKui/ShareTest