系统分享(Obj-C)

1.导入头文件

#import 

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

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

    // 1.创建控制器
    SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    
    // 2.modal展示
    [self presentViewController:composeVc animated:YES completion:nil];
}
系统分享(Obj-C)_第1张图片
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:"];

效果:

系统分享(Obj-C)_第2张图片
set_properties.png

完整演示代码:

#import "ViewController.h"
#import 

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    

}

// 点击屏幕,分享到新浪微博
- (void)touchesBegan:(NSSet *)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];
}

你可能感兴趣的:(系统分享(Obj-C))