为了方便使用,我封装了一个分享的工具类LFSystemShareUtil。工程要引Social.framework。

LFSystemShareUtil.h

#import <Foundation/Foundation.h>
#import <Social/Social.h> typedef NS_ENUM(NSInteger, LFSystemShareType) {
LFSystemShareWeChat,//微信
LFSystemShareQQ,//腾讯QQ
LFSystemShareSina,//新浪微博
}; typedef NS_ENUM(NSInteger, LFSystemShareState) {
LFSystemShareStateCancel,//取消
LFSystemShareStateDone,//完成
LFSystemShareStateNone,//未安装
}; @interface LFSystemShareUtil : NSObject /**
直接分享到某平台 @param type 平台
@param controller 弹出分享界面的控制器
@param items 可以仅分享图@[UIImage],可以放多张;或者仅分享纯视频、音乐@[NSURL];或者一个带文字和缩略图的网页@[NSURL,NSString,UIImage],等等,总之把要分享的东西放到数组即可
@param shareResult LFSystemShareState
*/
+ (void)shareWithType:(LFSystemShareType)type controller:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(LFSystemShareState state))shareResult; /**
通过选择平台的控制面板分享 @param controller 弹出分享界面的控制器
@param items 可以仅分享图@[UIImage],可以放多张;或者仅分享纯视频、音乐@[NSURL];或者一个带文字和缩略图的网页@[NSURL,NSString,UIImage],等等,总之把要分享的东西放到数组即可
@param shareResult 结果回调
*/
+ (void)shareWithController:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError))shareResult; @end

LFSystemShareUtil.m

#import "LFSystemShareUtil.h"

@implementation LFSystemShareUtil

+ (void)shareWithType:(LFSystemShareType)type controller:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(LFSystemShareState state))shareResult {
NSString *serviceType = @"";
switch (type){
case LFSystemShareWeChat:
serviceType = @"com.tencent.xin.sharetimeline";
break;
case LFSystemShareQQ:
serviceType = @"com.tencent.mqq.ShareExtension";
break;
case LFSystemShareSina:
serviceType = @"com.apple.share.SinaWeibo.post";
break;
default:
break;
} /*
<NSExtension: 0x1741735c0> {id = com.apple.share.Flickr.post}",
"<NSExtension: 0x174173740> {id = com.taobao.taobao4iphone.ShareExtension}",
"<NSExtension: 0x174173a40> {id = com.apple.reminders.RemindersEditorExtension}",
"<NSExtension: 0x174173bc0> {id = com.apple.share.Vimeo.post}",
"<NSExtension: 0x174173ec0> {id = com.apple.share.Twitter.post}",
"<NSExtension: 0x174174040> {id = com.apple.mobileslideshow.StreamShareService}",
"<NSExtension: 0x1741741c0> {id = com.apple.Health.HealthShareExtension}",
"<NSExtension: 0x1741744c0> {id = com.apple.mobilenotes.SharingExtension}",
"<NSExtension: 0x174174640> {id = com.alipay.iphoneclient.ExtensionSchemeShare}",
"<NSExtension: 0x174174880> {id = com.apple.share.Facebook.post}",
"<NSExtension: 0x174174a00> {id = com.apple.share.TencentWeibo.post}
*/ /*
"<NSExtension: 0x174174340> {id = com.tencent.xin.sharetimeline}", //微信
"<NSExtension: 0x174173d40> {id = com.tencent.mqq.ShareExtension}", //QQ
"<NSExtension: 0x1741738c0> {id = com.apple.share.SinaWeibo.post}", //微博
*/ if ([SLComposeViewController isAvailableForServiceType:serviceType]) {
SLComposeViewController *composeVC = [SLComposeViewController composeViewControllerForServiceType:serviceType];
for ( id obj in items){
if ([obj isKindOfClass:[UIImage class]]){
[composeVC addImage:(UIImage *)obj];
}else if ([obj isKindOfClass:[NSURL class]]){
[composeVC addURL:(NSURL *)obj];
} else if ([obj isKindOfClass:[NSString class]]) {
[composeVC setInitialText:(NSString *)obj];
}
} // 弹出分享控制器
composeVC.completionHandler = ^(SLComposeViewControllerResult result) {
if (shareResult) {
shareResult((LFSystemShareState)result);
}
};
[controller presentViewController:composeVC animated:YES completion:nil];
} else {
if (shareResult) {
shareResult(LFSystemShareStateNone);
}
}
} + (void)shareWithController:(UIViewController *)controller andItems:(NSArray *)items completionHandler:(void(^)(NSString *activityType,BOOL completed,NSArray *returnedItems,NSError *activityError))shareResult {
UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
//不出现在活动项目
activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard,UIActivityTypeAssignToContact,UIActivityTypeSaveToCameraRoll,UIActivityTypeAddToReadingList];
activityVC.completionWithItemsHandler = shareResult;
[controller presentViewController:activityVC animated:YES completion:nil];
} @end

最新文章

  1. one_person年轻的程序员
  2. node.js 资料
  3. 2016年12月4日 星期日 --出埃及记 Exodus 20:25
  4. JS如何判断IE和火狐与Chrome浏览器
  5. src 和 href 的区别
  6. CQRS架构设计及其实现
  7. Message Queuing(MSMQ)
  8. BZOJ_2440_[中山市选2011]完全平方数_容斥原理+线性筛
  9. python doc格式转文本格式
  10. redis-trib.rb命令详解
  11. Python_函数的初识、函数的返回值、函数的参数
  12. MI200e电力线通讯
  13. linux内核分析第二四学习报告
  14. sql语句Order by 报错列名不明确
  15. 线程 day40
  16. XCode9的新变化
  17. 禁用IE缓存
  18. 获取html元素的XPath路径
  19. MongoDB 安装成为Windows服务
  20. springmvc+hibernate4事务管理配置

热门文章

  1. exit()与_exit()区别
  2. Linux内核空间内存申请函数kmalloc、kzalloc、vmalloc的区别【转】
  3. Linux 入门记录:十四、网络基础
  4. linux加载指定目录的so文件
  5. Jmeter跨线程组传递变量
  6. caffe Python API 之激活函数ReLU
  7. UNIX shell 学习笔记 一 : 几个shell的规则语法对比
  8. Unknown character set: &#39;utf8mb4&#39;
  9. tornado write render redirect IP
  10. jquery获取元素索引值index()的例子