iOS系统自带分享功能

时间:2023-03-09 23:57:23
iOS系统自带分享功能

很多APP中都带有社交分享功能,通过用户的分享,让更多地人去了解和使用这个APP,目前社交分享是移动互联网应用程序推广的最重要手段之一,国内较或的分享平台有微信,IOS6后苹果集成的新浪微博,还有IOS7后集成的腾讯微博。 在IOS中,实现社交分享可以自己编写各个平台的分享代码,但代码量较多,也可以利用IOS自带的Social.framework,更可以利用第三方的分享框架,如友盟,ShareSDK等。接下来先介绍一个系统自带的分享功能。 打开设备或模拟器的偏好设置,可以看到如下 iOS系统自带分享功能

系统需要先登录到微博
iOS系统自带分享功能

项目中需要导入一个系统自带头文件 #import 我们在屏幕的点击事件中来实现分享到微博

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 首先判断新浪分享是否可用
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        return;
    }
    // 创建控制器,并设置ServiceType
    SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    // 添加要分享的图片
    [composeVC addImage:[UIImage imageNamed:@"Snip20150429_9"]];
    // 添加要分享的文字
    [composeVC setInitialText:@"share my CSDN Blog"];
    // 添加要分享的url
    [composeVC addURL:[NSURL URLWithString:@"http://blog.csdn.net/u011058732"]];
    // 弹出分享控制器
    [self presentViewController:composeVC animated:YES completion:nil];
    // 监听用户点击事件
    composeVC.completionHandler = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultDone) {
            NSLog(@"点击了发送");
        }
        else if (result == SLComposeViewControllerResultCancelled)
        {
            NSLog(@"点击了取消");
        }
    };
}

当点击屏幕的时候可以看到如下
iOS系统自带分享功能
点击post即可将内容分享出去。当然该分享方式具有一定的局限性,所以一般我们都会使用第三方框架。