系统分享(Obj-C)

1.导入头文件

#import <Social/Social.h>

2.演示效果,点击屏幕的时候,弹出分享(新浪微博)

// 点击屏幕,分享到新浪微博
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    // 1.创建控制器
    SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    
    // 2.modal展示
    [self presentViewController:composeVc animated:YES completion:nil];
}
default.png

系统提供的分享方式比较少,serviceType:

NSString *const SLServiceTypeFacebook;
NSString *const SLServiceTypeTwitter;
NSString *const SLServiceTypeSinaWeibo;
NSString *const SLServiceTypeTencentWeibo;

使用系统分享功能控制台可能会出现提示:plugin com.apple.share.SinaWeibo.post invalidated,忽略即可

还可以设置缺省文字、图片和Url:

    // 2.1 设置图片
    [composeVc addImage:[UIImage imageNamed:@"girl"]];
    // 2.2 设置Url
    [composeVc addURL:[NSURL URLWithString:@"http://www.jianshu.com/users/5ec5747435a2/latest_articles"]];
    // 2.3 设置缺省文字
    [composeVc setInitialText:@"InitialText:"];

效果:

set_properties.png

完整演示代码:

#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.
    

}

// 点击屏幕,分享到新浪微博
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    // 1.创建控制器
    SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    
    // 2.设置属性
    
    // 2.1 设置图片
    [composeVc addImage:[UIImage imageNamed:@"girl"]];
    // 2.2 设置Url
    [composeVc addURL:[NSURL URLWithString:@"http://www.jianshu.com/users/5ec5747435a2/latest_articles"]];
    // 2.3 设置缺省文字
    [composeVc setInitialText:@"InitialText:"];
    
    // 3.modal展示
    [self presentViewController:composeVc animated:YES completion:nil];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容