UIActivityViewController实现分享

版本记录

版本号 时间
V1.0 2017.07.20

前言

在app中,都有分享的功能,我们可以直接接入各种想要分享的平台进行分享,还有就是可以利用第三方分享平台进行分享,主要的第三方分享平台有ShareSDK,还有就是百度的组件分享等,这里我们就说一下苹果UIActivityViewController的分享,感兴趣的可以看一下我的另一篇介绍苹果原生框架的分享。
1. 苹果原生框架的分享

功能需求

利用苹果的UIActivityViewController实现多平台的分享。


功能实现

在开始实现之前,我们先打开自己手机的系统相册,并且点击一个照片分享,如下图所示。

UIActivityViewController实现分享_第1张图片
相片
UIActivityViewController实现分享_第2张图片
相片分享

下面我们调起来这个分享组件,进行原生分享。

下面我们就看代码。

#import "JJShareVC.h"

@interface JJShareVC ()

@end

@implementation JJShareVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    [self shareAction];
}

- (void)shareAction
{
    // 要分享的图片
    UIImage *image = [UIImage imageNamed:@"plcaeholder"];
    
    // 要分享的文字
    NSString *str = @"开始直播";
    
    //地址分享
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1140440766"];
    
    // 将要分享的元素放到一个数组中
    NSArray *postItems = @[str,image, url];
    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];
    
    // 在展现 activityVC 时,必须根据当前的设备类型,使用适当的方法。在iPad上,必须通过popover来展现view controller。在iPhone和iPodtouch上,必须以模态的方式展现。
    if ([[UIDevice currentDevice].model isEqualToString:@"iPad"]) {
        UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVC];
        [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
    else {
        [self presentViewController:activityVC animated:YES completion:nil];
    }
}

@end


功能验证

下面我们就验证一下效果,直接上图了啊。

UIActivityViewController实现分享_第3张图片
效果1
UIActivityViewController实现分享_第4张图片
效果2
UIActivityViewController实现分享_第5张图片
效果3
UIActivityViewController实现分享_第6张图片
效果4
UIActivityViewController实现分享_第7张图片
效果5
UIActivityViewController实现分享_第8张图片
效果6

可见,可以通过系统的活动控制器完成了分享。

后记

苹果的原生分享到此就结束了,希望对大家有所帮助。

UIActivityViewController实现分享_第9张图片
镁铝

你可能感兴趣的:(UIActivityViewController实现分享)