友盟分享開發(fā)文檔:
http://dev.umeng.com/social/ios/quick-integration?spm=0.0.0.0.ZOvbzv
最近編寫了一個(gè)友盟分享Demo,實(shí)現(xiàn)了友盟自帶分享UI和自定義分享UI兩個(gè)樣式將分享內(nèi)容分享到微信和QQ。下面具體講講分享的實(shí)現(xiàn)。(以下只實(shí)現(xiàn)分享到微信和QQ,如果需要分享到其他平臺(tái)思路差不多,這里就不多說了)
注冊(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、使用cocoa pods下載管理:在Podfile中寫入 pod 'UMengSocial', '~> 5.0',關(guān)于cocapods的安裝與使用,可以參考文檔:
http://www.cnblogs.com/huahua0809/p/4933536.html添加SDK依賴的系統(tǒng)庫文件
在Xcode中打開工程配置文件,選擇“summary”一欄。在“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-
配置URL schemes
在你的工程設(shè)置項(xiàng),targets 一欄下,選中自己的 target,在 Info->URL Types 中添加 URL Schemes,添加xcode的url scheme為微信應(yīng)用appId:
配置URL schemes 設(shè)置白名單
如果你的應(yīng)用使用了如SSO授權(quán)登錄或跳轉(zhuǎn)分享功能,在iOS9下就需要增加一個(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.itdecent.cn/users/238e769238c2/latest_articles"];
[UMSocialQQHandler setQQWithAppId:QQAppID appKey:QQAppKey url:@"http://www.itdecent.cn/users/238e769238c2/latest_articles"];
return YES;
}
- 系統(tǒng)自帶分享UI實(shí)現(xiàn)分享
[UMSocialSnsService presentSnsIconSheetView:self
appKey:@"570c660367e58e91600010a5"
shareText:@"花花的簡書"
shareImage:[UIImage imageNamed:@"icon.png"]
shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite,UMShareToQQ,UMShareToQzone,nil]
delegate:self];
效果圖:

- 自定義分享UI顯示分享
1、.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
2、.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
3、調(diào)用實(shí)現(xiàn)分享:
XHCustomShareView *shareView = [XHCustomShareView shareViewWithPresentedViewController:self items:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQzone] title:@"歡迎進(jìn)入花花的簡書" image:[UIImage imageNamed:@"icon.png"] urlResource:nil];
[[UIApplication sharedApplication].keyWindow addSubview:shareView];
效果圖:

