iOS UIActivityViewController系统原生

2019-02-20  本文已影响0人  yyggzc521

下面我来介绍一下利用原生UIActivityViewController快速实现QQ和微信分享

  1. 创建要分享的数据内容,加在一个数组 ActivityItems里
    NSString *shareText = @"我是title";
   //图片应是加载完成后的image或本地的image,否则可能会出错
    UIImage *shareImage = [UIImage imageNamed:@"shareImage"];
    NSURL *shareUrl = [NSURL URLWithString:@"https://www.jianshu.com/u/15d37d620d5b"];
    NSArray *activityItemsArray = @[shareText,shareImage,shareUrl];
  1. 创建分享视图控制器,初始化UIActivityViewController
//第一个参数 分享的内容
//第二个参数 是一个UIActivity对象的数组,代表了应用程序支持的自定义服务。这个参数可以是nil 
- (instancetype)initWithActivityItems:(NSArray *)activityItems applicationActivities:(nullable NSArray<__kindof UIActivity *> *)applicationActivities
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems: activityItemsArray applicationActivities:nil];
    activityVC.modalInPopover = YES;
    //去除特定的分享功能 不需要展现的Activity类型
    activityVC.excludedActivityTypes = @[
                                         UIActivityTypePostToFacebook,
                                         UIActivityTypePostToTwitter,
                                         UIActivityTypePostToWeibo,
                                         UIActivityTypeMessage,
                                         UIActivityTypeMail,
                                         UIActivityTypePrint,
                                         UIActivityTypeCopyToPasteboard,
                                         UIActivityTypeAssignToContact,
                                         UIActivityTypeSaveToCameraRoll,
                                         UIActivityTypeAddToReadingList,
                                         UIActivityTypePostToFlickr,
                                         UIActivityTypePostToVimeo,
                                         UIActivityTypePostToTencentWeibo,
                                         UIActivityTypeAirDrop,
                                         UIActivityTypeOpenInIBooks
                                         ];

  1. 根据系统版本,设置点击回调
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {  // ios8.0 之后用此方法回调
        UIActivityViewControllerCompletionWithItemsHandler itemsBlock = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
            NSLog(@"activityType == %@",activityType);
            if (completed == YES) {
                NSLog(@"completed");
            } else {
                NSLog(@"cancel");
            }
        };
        activityVC.completionWithItemsHandler = itemsBlock;
    } else { // ios8.0 之前用此方法回调
        UIActivityViewControllerCompletionHandler handlerBlock = ^(UIActivityType __nullable activityType, BOOL completed){
            NSLog(@"activityType == %@",activityType);
            if (completed == YES) {
                NSLog(@"completed");
            } else {
                NSLog(@"cancel");
            }
        };
        activityVC.completionHandler = handlerBlock;
    }
  1. 根据设备类型使用相应方法展示
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {//iPad
        UIBarButtonItem *shareBarButtonItem = self.navigationItem.leftBarButtonItem;
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
        [popup presentPopoverFromBarButtonItem:shareBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
       
    } else {
       [self presentViewController:activityViewController animated:YES completion:nil];
    }
标题+图片+链接.jpeg

至此,简单的分享功能就实现完成了,不需要导入微信的SDK。是不是很简单。
自定义UIActivity,主要就是重写父类的方法,以后用到的话再详细介绍。

UIActivityViewController实现系统原生分享
iOS原生分享
UIActivityViewController的相关用法(三)--自定义分享方式(比第三方分享库轻便)
UIActivityViewController原生分享 微信文字 图片 链接,比较详细
利用UIActivityViewController实现分享视频,图片,链接
iOS通过UIActivityViewController实现原生分享功能

上一篇下一篇

猜你喜欢

热点阅读