<h1>1.本機(jī)的分享
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//判斷功能是否可用
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
return;
}
//創(chuàng)建分享控制器
SLComposeViewController *vc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
// 設(shè)置蚊子
[vc setInitialText:@"我是蚊子"];
// 設(shè)置圖片
[vc addImage:[UIImage imageNamed:@"sdf"]];
// 設(shè)置網(wǎng)址
[vc addURL:[NSURL URLWithString:@"www.123"]];
//模態(tài)彈出
[self presentViewController:vc animated:YES completion:nil];
}
注意舒憾!只能用兩個(gè)微博
<h1>2.友盟分享
友盟分享的開發(fā)文檔:
http://dev.umeng.com/social/ios/quick-integration?spm=0.0.0.0.ZOvbvz
下載sdk時(shí)候的選項(xiàng)
最近編寫了一個(gè)友盟分享的Demo幽邓,實(shí)現(xiàn)了友盟自帶分享 UI 和自定義分享 UI 兩個(gè)樣式將分享的內(nèi)容分享到微信和 QQ婿禽。下面是具體的實(shí)現(xiàn)過程:
首先注冊(cè)賬號(hào)獲取相應(yīng)的配置信息
友盟開發(fā)者賬號(hào) ------ 獲取 appkey
微信開發(fā)者賬號(hào)------獲取 appid 和 APPSecret
QQ 開發(fā)者賬號(hào)------獲取 appid 和 appkey
下載友盟分享的 SDK
有兩種方式:
1、官網(wǎng)下載 SDK资昧,下載完成之后導(dǎo)入到項(xiàng)目中。
2、使用 cocoapods 集成友盟:在 Podfile中寫入 pod 'UMengSocial',可以參考文檔:http://www.cnblogs.com/huahua0809/p/4933536.html-
添加 SDK 依賴的系統(tǒng)庫文件
在 Xcode 中打開工程配置文件馆揉,選擇“summary”一欄,選擇“Linked Frameworks and Libraries”一欄抖拦,點(diǎn)擊“+”圖標(biāo)添加下面八個(gè)庫文件升酣,分別是:- Security.framework
- libiconv.dylib
- SystemConfiguration.framework
- CoreGraphics.Framework
- libsqlite3.dylib
- CoreTelephony.framework
- libstdc++.dylib
- libz.dylib
這只是其中的一部分,不同的平臺(tái)依賴的系統(tǒng)庫文件不同态罪,根據(jù)官方文檔添加即可
配置 URL schemes
在你的工程設(shè)置項(xiàng)噩茄,targets一欄下,選中自己的 target复颈,在 info->URL Types 根據(jù)官方文檔添加 URL Schemes設(shè)置白名單
如果你的應(yīng)用使用了如 SSO 授權(quán)登錄或者跳轉(zhuǎn)分享功能绩聘,在 iOS 9下需要增加一個(gè)可跳轉(zhuǎn)的白名單,指定對(duì)應(yīng)跳轉(zhuǎn) APP 的 URL Scheme耗啦,否則將在第三方平臺(tái)判斷是否跳轉(zhuǎn)時(shí)用到的 canOpenURL 時(shí)返回 NO凿菩,進(jìn)而只進(jìn)行 webview 授權(quán)或者授權(quán)/分享失敗
<key>LSApplicationQueriesSchemes</key><array>
<!-- 微信 URL Scheme 白名單-->
<string>wechat</string>
<string>weixin</string>
<!-- 新浪微博 URL Scheme 白名單-->
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<!-- QQ、Qzone URL Scheme 白名單-->
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
<!-- 人人 URL Scheme 白名單-->
<string>renrenios</string>
<string>renrenapi</string>
<string>renren</string>
<string>renreniphone</string>
<!-- 來往 URL Scheme 白名單-->
<string>laiwangsso</string>
<!-- 易信 URL Scheme 白名單-->
<string>yixin</string>
<string>yixinopenapi</string>
<!-- instagram URL Scheme 白名單-->
<string>instagram</string>
<!-- whatsapp URL Scheme 白名單-->
<string>whatsapp</string>
<!-- line URL Scheme 白名單-->
<string>line</string>
<!-- Facebook URL Scheme 白名單-->
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string></array>
代碼實(shí)現(xiàn)
- 在AppDelegate內(nèi)設(shè)置友盟AppKey
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[UMSocialData setAppKey:UMAppKey];
[UMSocialWechatHandler setWXAppId:WeChatAppId appSecret:WeChatAppSecret url:@"http://www.reibang.com/users/238e769238c2/latest_articles"];
[UMSocialQQHandler setQQWithAppId:QQAppID appKey:QQAppKey url:@"http://www.reibang.com/users/238e769238c2/latest_articles"];
return YES;
}
- 系統(tǒng)自帶分享UI實(shí)現(xiàn)分享
[UMSocialSnsService presentSnsIconSheetView:self
appKey:@"570c660367e58e91600010a5"
shareText:@"花花的簡(jiǎn)書"
shareImage:[UIImage imageNamed:@"icon.png"]
shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite,UMShareToQQ,UMShareToQzone,nil]
delegate:self];
效果展示
- 自定義分享UI顯示分享
- .h文件代碼:
#import <UIKit/UIKit.h>
#import "UMSocial.h"
@interface XHCustomShareView : UIView
+(instancetype)shareViewWithPresentedViewController:(UIViewController *)controller items:(NSArray *)items title:(NSString *)title image:(UIImage *)image urlResource:(NSString *)url;
@end
- .m文件代碼:
#import "XHCustomShareView.h"
#define KHeight self.frame.size.height
#define kWidth self.frame.size.width
#define ShareViewHeight 155
@interface XHCustomShareView()
/** 分享內(nèi)容屬性 */
@property (nonatomic,strong)NSArray *items;
@property (nonatomic,copy)NSString *title;
@property (nonatomic,copy)UIImage *image;
@property (nonatomic,copy)NSString *url;
@property (nonatomic,strong)UIViewController *controller;
/** UI屬性 */
@property (nonatomic,strong)UIView *buttomView;
@property (nonatomic,strong)UIView *contentView;
@end
@implementation XHCustomShareView
#pragma mark - 自定義初始化類方法
+(instancetype)shareViewWithPresentedViewController:(UIViewController *)controller items:(NSArray *)items title:(NSString *)title image:(UIImage *)image urlResource:(NSString *)url{
XHCustomShareView *view = [[XHCustomShareView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
view.title = title;
view.url = url;
view.image = image;
view.items = items;
view.controller = controller;
[view createShareView];
return view;
}
#pragma mark - 創(chuàng)建分享UI
-(void)createShareView{
/*------------------ 添加蒙板 -------------------*/
_buttomView = [[UIView alloc] initWithFrame:self.bounds];
_buttomView.backgroundColor = [UIColor blackColor];
_buttomView.alpha = 0.0;
[_buttomView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissShareView)]];
[self addSubview:_buttomView];
/*------------------------------------------------*/
/*---------- 內(nèi)容view芹彬,包括分享itemsView以及取消按鈕 ----------*/
_contentView = [[UIView alloc] initWithFrame:CGRectMake(0, KHeight, kWidth, ShareViewHeight)];
_contentView.backgroundColor = [UIColor clearColor];
[self addSubview:_contentView];
//分享ItemsView
UIScrollView *shareItemView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 0, kWidth-20, 100)];
shareItemView.backgroundColor = [UIColor whiteColor];
shareItemView.layer.cornerRadius = 10;
shareItemView.layer.masksToBounds = YES;
shareItemView.showsVerticalScrollIndicator = NO;
shareItemView.showsHorizontalScrollIndicator = NO;
[_contentView addSubview:shareItemView];
if (_items.count) {
CGFloat itemWidth = 50;
CGFloat itemHeight = 50;
CGFloat pading = _items.count<5 ? (shareItemView.width-itemHeight*_items.count)/(_items.count+1) : 30;
for (int i=0; i<_items.count; i++) {
UIButton * itemBtn = [[UIButton alloc] initWithFrame:CGRectMake((itemWidth+pading)*i+pading, 15, itemWidth, itemHeight)];
itemBtn.tag = i;
NSString *itemName = [self getTitleAndImageWithItem:itemBtn];
[itemBtn addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
[shareItemView addSubview:itemBtn];
UILabel *itemLabel = [[UILabel alloc] initWithFrame:CGRectMake(itemBtn.left, itemBtn.bottom+5, itemWidth, 20)];
itemLabel.text = itemName;
itemLabel.font = [UIFont systemFontOfSize:12];
itemLabel.textAlignment = NSTextAlignmentCenter;
[shareItemView addSubview:itemLabel];
}
shareItemView.contentSize = CGSizeMake((itemWidth+pading)*_items.count+pading, 0);
}
//取消按鈕
UIButton *cancelbtn = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(shareItemView.frame)+10, kWidth-20, 40)];
cancelbtn.layer.cornerRadius = 10;
cancelbtn.layer.masksToBounds = YES;
[cancelbtn setTitle:@"取消" forState:UIControlStateNormal];
cancelbtn.backgroundColor = [UIColor whiteColor];
[cancelbtn addTarget:self action:@selector(dismissShareView) forControlEvents:UIControlEventTouchUpInside];
[cancelbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_contentView addSubview:cancelbtn];
[UIView animateWithDuration:0.3 animations:^{
_buttomView.alpha = 0.3;
_contentView.frame = CGRectMake(0, KHeight-ShareViewHeight, kWidth, ShareViewHeight);
}];
/*----------------------------------------------------------*/
}
#pragma mark - 設(shè)置item圖片以及獲取title
-(NSString *)getTitleAndImageWithItem:(UIButton *)itemBtn{
NSString *itemTitle = @"";
NSString *itemImage = @"";
NSString *type = _items[itemBtn.tag];
if ([type isEqualToString:UMShareToQzone]) {
itemTitle = @"QQ空間";
itemImage = @"share_qzone";
}
else if([type isEqualToString:UMShareToQQ])
{
itemTitle = @"QQ";
itemImage = @"";
}
else if([type isEqualToString:UMShareToWechatSession])
{
itemTitle = @"微信好友";
itemImage = @"share_weixin";
}
else if([type isEqualToString:UMShareToWechatTimeline])
{
itemTitle = @"朋友圈";
itemImage = @"share_circle";
}
else if([type isEqualToString:UMShareToWechatFavorite])
{
itemTitle = @"微信收藏";
itemImage = @"";
}
else if([type isEqualToString:UMShareToSina])
{
itemTitle = @"新浪";
itemImage = @"share_sina";
}
else{
NSLog(@"其他設(shè)備自行添加");
}
[itemBtn setImage:[UIImage imageNamed:itemImage] forState:UIControlStateNormal];
return itemTitle;
}
#pragma mark - 點(diǎn)擊item進(jìn)行分享操作
-(void)itemClick:(UIButton *)btn{
NSInteger index = btn.tag;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[_items[index]] content:_title image:_image location:nil urlResource:nil presentedController:_controller completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功蓄髓!");
}
}];
}
#pragma mark - 點(diǎn)擊 取消或者蒙板 消除分享View
-(void)dismissShareView{
[UIView animateWithDuration:0.3 animations:^{
_buttomView.alpha = 0.0;
_contentView.frame = CGRectMake(0, KHeight, kWidth, ShareViewHeight);
} completion:^(BOOL finished) {
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self removeFromSuperview];
}];
}
@end
- 調(diào)用實(shí)現(xiàn)分享
XHCustomShareView *shareView = [XHCustomShareView shareViewWithPresentedViewController:self items:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQzone] title:@"張三兒" image:[UIImage imageNamed:@"icon.png"] urlResource:nil];
[[UIApplication sharedApplication].keyWindow addSubview:shareView];
![](http://upload-images.jianshu.io/upload_images/2092665-de42f7138183b4a9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)