社会化分享
现在很多应用都内置“社会化分享”功能,可以将看到的新闻、博客、广告等内容分享到微博、微信、QQ、空间等,为了拉取用户,提高应用的DAU,一般应用都有分享功能。下面咱们从苹果官方自带分享说起:其实从iOS6.0开始苹果官方就内置了Scocial.framework专门来实现社会化分享功能,利用这个框架开发者只需要几句代码就可以实现内容分享。下面就以一个分享到新浪微博的功能为例来演示Social框架的应用,整个过程分为:创建内容编辑控制器,设置分享内容(文本内容,图片等)设置发送(或取消)后的回调事件展示控制器。
程序代码如下:
//
// ViewController.m
// Social_自带社会化分享
//
// Created by Wangyuanyuan on 16/4/19.
// Copyright © 2016年 Wangyuanyuan. All rights reserved.
//
#import "ViewController.h"
#import <Social/Social.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//系统自带social.framewoke的使用步骤
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:@"测试" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
#pragma mark - 系统自带的分享按钮的响应方法
- (void)shareAction:(UIButton *)sender{
//第一步:判断平台是否可用(系统,注意此处)
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
NSLog(@"新浪微博不可用");
return;
}
//第二步:创建分享控制器
SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
//第三步:添加分享的文字
[composeVC setInitialText:@"梦想一定要有,说不定就实现了呢"];
//第3.5步:添加分享的图片
[composeVC addImage:[UIImage imageNamed:@"DLAM.jpg"]];
//第四步:弹出控制器进行分享
[self presentViewController:composeVC animated:YES completion:nil];
//第五步:设置监听发送结果
composeVC.completionHandler = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultDone) {
NSLog(@"用户发送成功");
} else{
NSLog(@"用户发送失败");
}
};
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行效果如下图: