不使用第三方分享框架实现分享功能

在以前的项目中,实现分享功能需要先去申请下载友盟的社会化组件,里面的QQ分享还有微信分享还要区申请繁琐的key,容易出现各种麻烦,如果碰到友善的友盟客服,他会耐心的给你解答,要是碰到态度不好的,人家直接不鸟你(我就碰到这种情况,半天恢复我,而且态度不敢恭维)。现在使用苹果原生的social.framework也能实现简单的分享功能了。social.framework目前只能实现在Facebook、Twitter、新浪微博、腾讯微博等平台实现分享。在使用前需要导入social.framework框架,并且在具体的ViewController中导入头文件。下面是我自己的初步体验代码:

#import "ViewController.h"
#import <Social/Social.h>
@interface ViewController ()
{
    //Facebook分享按钮
    UIButton *faceBtn;
    //Twitter分享按钮
    UIButton *twitterBtn;
    //新浪微博分享按钮
    UIButton *sinaBtn;
    //腾讯微博分享按钮
    UIButton *tenBtn;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    faceBtn=[[UIButton alloc]initWithFrame:CGRectMake(110, 100, 100, 44)];
    [faceBtn setBackgroundColor:[UIColor blueColor]];
    [faceBtn setTitle:@"faceBook" forState:UIControlStateNormal];
    [faceBtn addTarget:self action:@selector(faceBtnClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    twitterBtn=[[UIButton alloc]initWithFrame:CGRectMake(110, 200, 100, 44)];
    [twitterBtn setBackgroundColor:[UIColor blueColor]];
    [twitterBtn setTitle:@"twitter" forState:UIControlStateNormal];
    [twitterBtn addTarget:self action:@selector(twitterBtnClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    sinaBtn=[[UIButton alloc]initWithFrame:CGRectMake(110, 300, 100, 44)];
    [sinaBtn setBackgroundColor:[UIColor blueColor]];
    [sinaBtn setTitle:@"sina" forState:UIControlStateNormal];
    [sinaBtn addTarget:self action:@selector(sinaBtnClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    tenBtn=[[UIButton alloc]initWithFrame:CGRectMake(110, 400, 100, 44)];
    [tenBtn setBackgroundColor:[UIColor blueColor]];
    [tenBtn setTitle:@"ten" forState:UIControlStateNormal];
    [tenBtn addTarget:self action:@selector(tenBtnClickedAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:faceBtn];
    [self.view addSubview:twitterBtn];
    [self.view addSubview:sinaBtn];
    [self.view addSubview:tenBtn];
    // Do any additional setup after loading the view, typically from a nib.
}
/**
 *  点击事件的实现方法
 */
#pragma mark---facebook按钮被点击
-(void)faceBtnClickedAction{
    NSLog(@"share use facebook");
    //响应点击事件之前要先判断,用户对facebook的配置是否完成
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        //弹出框可以是系统默认的,也可以是自定义的[[SLComposeViewController alloc]initWithNibName:@"" bundle:nil]
        SLComposeViewController *compose=[[SLComposeViewController alloc]init];
        compose=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        //分享的文字
        [compose setInitialText:@"我是天朝良民!\n"];
        //分享的链接
        [compose addURL:[NSURL URLWithString:@"http://www.mipow.com"]];
        //分享的图片
        [compose addImage:[UIImage imageNamed:@"app.png"]];
        //界面跳转
        [self presentViewController:compose animated:YES completion:^{
            NSLog(@"facebook 分享成功");
        }];
    }else{
        NSLog(@"请配置好你的Facebook");
    }
}

#pragma mark---twitter按钮被点击
-(void)twitterBtnClickedAction{
    NSLog(@"share use twitter");
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
        SLComposeViewController *compose=[[SLComposeViewController alloc]init];
        compose=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        //分享的文字
        [compose setInitialText:@"天朝生活富足!\n"];
        //分享的链接
        //分享的图片
        //界面跳转
        [self presentViewController:compose animated:YES completion:^{
            NSLog(@"twitter 分享成功");
        }];
    }else{
        NSLog(@"请配置好你的twitter");
    }
}

#pragma mark---sinaBtn按钮被点击
-(void)sinaBtnClickedAction{
    NSLog(@"share use sina");
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        SLComposeViewController *compose=[[SLComposeViewController alloc]init];
        compose=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
        //分享的文字
        [compose setInitialText:@"天朝生活富足!\n"];
        //分享的链接
        //分享的图片
        //界面跳转
        [self presentViewController:compose animated:YES completion:^{
            NSLog(@"sina weibo 分享成功");
        }];
    }else{
        NSLog(@"请配置好你的sina weibo");
    }
}

#pragma mark---tenBtn被点击
-(void)tenBtnClickedAction{
    NSLog(@"share use ten");
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTencentWeibo]) {
        SLComposeViewController *compose=[[SLComposeViewController alloc]init];
        compose=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTencentWeibo];
        //分享的文字
        [compose setInitialText:@"天朝生活富足!\n"];
        //分享的链接
        //分享的图片
        //界面跳转
        [self presentViewController:compose animated:YES completion:^{
            NSLog(@"tencent weibo 分享成功");
        }];
    }else{
        NSLog(@"请配置好你的tencent weibo");
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值