ios 自带UIActivityViewController分享

UIActivityViewController 使用大致使用步骤:

  • 1 设定分享的内容,比如:

      NSString *shareTitle = @"分享的标题";
      UIImage *shareImage = [UIImage imageNamed:@"me"];
      NSURL *shareUrl = [NSURL URLWithString:@"https://www.jianshu.com/u/acdcce712303"];
      NSArray *activityItems = @[shareTitle,
                                 shareImage,
                                 shareUrl]; // 必须要提供url 才会显示分享标签否则只显示图片
    
  • 2 创建分享的控制器

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] 
                                           initWithActivityItems:activityItems
                                           applicationActivities:nil];
    
  • 3 设定不想显示的平台和功能

    -(NSArray *)excludetypes{
      
      NSMutableArray *excludeTypesM =  [NSMutableArray arrayWithArray:@[//UIActivityTypePostToFacebook,
                                                                        UIActivityTypePostToTwitter,
                                                                        UIActivityTypePostToWeibo,
                                                                        UIActivityTypeMessage,
                                                                        UIActivityTypeMail,
                                                                        UIActivityTypePrint,
                                                                        UIActivityTypeCopyToPasteboard,
                                                                        UIActivityTypeAssignToContact,
                                                                        UIActivityTypeSaveToCameraRoll,
                                                                        UIActivityTypeAddToReadingList,
                                                                        UIActivityTypePostToFlickr,
                                                                        UIActivityTypePostToVimeo,
                                                                        UIActivityTypePostToTencentWeibo,
                                                                        UIActivityTypeAirDrop,
                                                                        UIActivityTypeOpenInIBooks]];
      
      if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) {
          [excludeTypesM addObject:UIActivityTypeMarkupAsPDF];
      }
      
      return excludeTypesM;
    }
    activityVC.excludedActivityTypes = [self excludetypes];
    
  • 4 设置操作回调,用户点击 菜单按钮后事件执行完成会回调这个block

    activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable   activityType, 
                                              BOOL completed,
                                              NSArray * _Nullable returnedItems,
                                              NSError * _Nullable activityError) {
          
          NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError);
      };
    
    
  • 5 显示分享菜单

[self presentViewController:activityVC animated:YES completion:nil];
Snip20180304_2.png

demo地址: https://github.com/TangChangTomYang/TestSystemShare.git

你可能感兴趣的:(ios 自带UIActivityViewController分享)