为了方便使用,我封装了一个分享的工具类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

iOS 系统原生分享图片 文字 音乐 纯视频 网页的更多相关文章

  1. php表单提交 图片、音乐、视频、文字,四种类型共同提交到数据库

    这个问题一直困扰了我好几天,终于在今天让我给解决了,难以掩饰的激动. 其实在之前没有接触到这种问题,只是表单提交数据而已,再就是图片,四种类型同时提交还真是没遇到过,做了一个系统,其中有一个功能就是提 ...

  2. UIActivityViewController实现系统原生分享

    代码地址如下:http://www.demodashi.com/demo/11042.html 一.效果预览 二.接下来介绍UIActivityViewController,跟我动手做 1.创建要分享 ...

  3. HTML5:图片、音乐和视频

    图片.音乐和视频 一.图片 1.属性 属性 说明 alt 规定图像的替代文本. src 规定显示图像的 URL align 规定如何根据周围的文本来排列图像. border 定义图像周围的边框. he ...

  4. 微信朋友圈如何同时分享(图片+文字) Android版

    以下是:微信朋友圈SDK 分享图片的代码,但只能分享图片,不能分享文字,如何才能图片和文字同时分享?求各位大神指教! public class MainActivity extends Activit ...

  5. iOS系统原生 二维码的生成、扫描和读取(高清、彩色)

    由于近期工作中遇到了个需求:需要将一些固定的字段 在多个移动端进行相互传输,所以就想到了 二维码 这个神奇的东东! 现在的大街上.连个摊煎饼的大妈 都有自己的二维码来让大家进行扫码支付.可见现在的二维 ...

  6. iOS开发-- 利用AVPlayer播放远程音乐和视频

    一.简单的播放音乐和视频,播放视频的工具栏需要自己写 二.利用老师封装的框架实现视频播放 链接:http://pan.baidu.com/s/1hrEKlus 密码:8e7g

  7. 仿简书分享:UIActivityViewController系统原生分享

    接下来介绍UIActivityViewController: 1. 创建要分享的数据内容,加在一个数组 ActivityItems里. NSString *textToShare = @"我 ...

  8. iOS系统原生二维码条形码扫描

    本文讲述如何用系统自带的东东实现二维码扫描的功能:点击当前页面的某个按钮,创建扫描VIEW.细心的小伙伴可以发现 title被改变了,返回按钮被隐藏了.这个代码自己写就行了,与本文关系不大...绿色的 ...

  9. Android APP 分享图片文字到微信刚開始正常,后面就不弹出分享框了

    依照官方的流程走了一遍.一切顺利,分享成功了.本来以为能够大功告成了,结果睡了一觉,第二天要给客户演示了.才发现TMD坑爹了,不能分享了,第三方的分享栏弹不出来了.我一阵惊慌,还好非常快找到了解决的方 ...

随机推荐

  1. AlertDialog.Builder 显示为白色 蓝色字

    AlertDialog.Builder dialog = new AlertDialog.Builder( getActivity(),AlertDialog.THEME_HOLO_LIGHT);

  2. go环境变量及build文件

    package main /* windows go环境设置: # 参考:https://blog.csdn.net/quicmous/article/details/80360126 GOROOT ...

  3. linux dpm机制分析(下)【转】

    转自:http://blog.csdn.net/lixiaojie1012/article/details/23707901 1      设备注册到dpm_list路径 (Platform_devi ...

  4. (十六)strtok、strtok_s、strtok_r 字符串分割函数

    1.strtok函数 函数原型:char * strtok (char *str, const char * delimiters); 参数:str,待分割的字符串(c-string):delimit ...

  5. PXC 避免加入集群时发生SST

    环境 现有集群节点: 192.168.99.210:3101 新加入节点: 192.168.99.211:3101 通过xtrabackup备份还原实例,并通过同步方式追数据: 已有节点情况: roo ...

  6. Centos_Lvm_Create pv vg lv and mount

    re-scan new disks without restarting CentOS re-scan new disks(/dev/sdc): #ls /sys/class/scsi_host/ h ...

  7. sicily 数据结构 1014. Translation

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  8. C基础 工程中常用的排序

    引言 - 从最简单的插入排序开始 很久很久以前, 也许都曾学过那些常用的排序算法. 那时候觉得计算机算法还是有点像数学. 可是脑海里常思考同类问题, 那有什么用呢(屌丝实践派对装逼学院派的深情鄙视). ...

  9. JS面试题第一弹

    1.javascript的typeof返回哪些数据类型  alert(typeof [1, 2]); //object     alert(typeof 'leipeng'); //string   ...

  10. JavaScript跨域解决方法大全

    跨域的定义:JavaScript出于安全性考虑,同源策略机制对跨域访问做了限制.域仅仅是通过“URL的首部”字符串进行识别,“URL的首部”指window.location.protocol +win ...