您的位置:

iOS分享详解

一、分享类型

iOS支持的分享类型非常丰富,包括文本、图片、链接、音频、视频、文件等。其中,常用的分享类型为文本、图片、链接。

文本分享代码如下:

UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[@"我是分享的文本"] applicationActivities:nil];
[self presentViewController:vc animated:YES completion:nil];

图片分享代码如下:

UIImage *image = [UIImage imageNamed:@"image.jpg"];
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[image] applicationActivities:nil];
[self presentViewController:vc animated:YES completion:nil];

链接分享代码如下:

NSURL *url = [NSURL URLWithString:@"http://www.example.com"];
UIActivityViewController *vc = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:nil];
[self presentViewController:vc animated:YES completion:nil];

二、分享平台

iOS支持的分享平台也非常多,包括微信、QQ、微博、短信、邮件等

分享到微信代码如下:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = @"分享标题";
    message.description = @"分享描述";
    [message setThumbImage:[UIImage imageNamed:@"image.jpg"]];
    WXWebpageObject *webpage = [WXWebpageObject object];
    webpage.webpageUrl = @"http://www.example.com";
    message.mediaObject = webpage;

    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneSession;

    [WXApi sendReq:req];
} else {
    // 未安装微信客户端
}

分享到QQ代码如下:

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
    QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:[NSURL URLWithString:@"http://www.example.com"] title:@"分享标题" description:@"分享描述" previewImageData:UIImageJPEGRepresentation([UIImage imageNamed:@"image.jpg"], 0.5)];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];

    if (sent != EQQAPISENDSUCESS) {
        // 分享失败
    }
} else {
    // 未安装QQ客户端
}

三、自定义分享界面

iOS提供了UIActivityViewController类来进行分享,但是该类的界面无法自定义,如果需要自定义分享界面,则需要自己编写分享界面,可以参考以下代码:

- (void)showCustomShareView {
    UIView *bgView = [[UIView alloc] initWithFrame:self.view.bounds];
    bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];

    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 200)];
    contentView.backgroundColor = [UIColor whiteColor];
    [bgView addSubview:contentView];

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, CGRectGetWidth(contentView.frame), 20)];
    titleLabel.text = @"分享到";
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [contentView addSubview:titleLabel];

    UIButton *shareButton1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 60, 60, 60)];
    [shareButton1 setImage:[UIImage imageNamed:@"icon1.png"] forState:UIControlStateNormal];
    [shareButton1 addTarget:self action:@selector(shareButton1Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:shareButton1];

    UIButton *shareButton2 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(contentView.frame) / 2 - 30, 60, 60, 60)];
    [shareButton2 setImage:[UIImage imageNamed:@"icon2.png"] forState:UIControlStateNormal];
    [shareButton2 addTarget:self action:@selector(shareButton2Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:shareButton2];

    UIButton *shareButton3 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(contentView.frame) - 80, 60, 60, 60)];
    [shareButton3 setImage:[UIImage imageNamed:@"icon3.png"] forState:UIControlStateNormal];
    [shareButton3 addTarget:self action:@selector(shareButton3Clicked:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:shareButton3];

    [self.view addSubview:bgView];
}

- (void)shareButton1Clicked:(UIButton *)sender {
    // 分享到第一个平台
}

- (void)shareButton2Clicked:(UIButton *)sender {
    // 分享到第二个平台
}

- (void)shareButton3Clicked:(UIButton *)sender {
    // 分享到第三个平台
}

四、注意事项

在进行分享时,需要注意以下事项:

1. 在分享前,需要检查对应的平台是否安装,如果没有安装,则需要提示用户安装对应的客户端。

2. 在分享图片和文件时,需要注意文件大小,过大的文件可能会导致分享失败。

3. 在分享到微信和QQ时,需要进行额外的注册和处理,具体可以参考官方文档。