ios Attempt to present * on * whose view is not in the window hierarchy!

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.csdn.net/flyingkuikui"];
    NSArray *activityItemsArray = @[shareText,shareImage,shareUrl];


    // 自定义的CustomActivity,继承自UIActivity
    CustomActivity *customActivity = [[CustomActivity alloc]initWithTitle:@"wangsk" ActivityImage:[UIImage imageNamed:@"custom.png"] URL:[NSURL URLWithString:@"http://blog.csdn.net/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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值