http://dev.umeng.com/social/ios/share/quick-integration
注意:蘋果審核政策需求,建議對未安裝的客戶端平台進行隱藏,在設置QQ。微信Appid之后調用方法:
[UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ, UMShareToQzone, UMShareToWechatSession, UMShareToWechatTimeline]];
這個接口只對默認分享平面平台有隱藏功能,自定義分享面板或者登陸按鈕需要自己處理
配置友盟:
1.添加友盟SDK(SDK介紹)
libUMSocial_Sdk_x.x.x.a | 庫文件 |
Header | 頭文件 |
SocialSDKXib | xib文件 |
en.lproj,zh-Hans.lproj | 英文和中文語言文件 |
UMSocialSDKResourcesNew.bundle | 資源圖片 |


#import "UMSocialWechatHandler.h"
2.添加啟動程序配置; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self initUmengSocial]; return YES; } #pragma mark #pragma mark-umeng社會化分享 -(void)initUmengSocial { [UMSocialData setAppKey:UMENG_APPKEY]; //這個方法包括了添加配置分享微信和朋友圈和微信收藏,同時也包括了sso免登錄;; [UMSocialWechatHandler setWXAppId:WeChat_AppId appSecret:WeChat_AppSecret url:@"http://www.baohuai.com"]; //這個方法包括了添加配置分享qq和qq空間,同時也包括了sso免登錄; [UMSocialQQHandler setQQWithAppId:QQ_AppId appKey:QQ_AppSecret url:@"http://www.umeng.com/social"]; //如果需要添加新浪微博sso免登錄:“sina.”+友盟appkey,例如“sina.51f8c20c56240b0de4007a06” 還要打開sso開關 [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"]; [UMSocialSinaSSOHandler openNewSinaSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"]; //對未安裝客戶端平台進行隱藏 [UMSocialConfig hiddenNotInstallPlatforms:@[UMShareToQQ, UMShareToQzone, UMShareToWechatSession, UMShareToWechatTimeline]]; }
3.文件配置:
如果添加手機qq或qq空間/微信或微信朋友圈/任意平台如新浪微博sso免登錄:需添加openURL
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [UMSocialSnsService handleOpenURL:url]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [UMSocialSnsService handleOpenURL:url]; }
若除了使用友盟SDK外,還用了其他SDK,需要重寫此回調方法的,可以參考下面的寫法: - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { BOOL result = [UMSocialSnsService handleOpenURL:url]; if (result == FALSE) { //調用其他SDK,例如新浪微博SDK等 } return result; }
4.給手機qq 和 qq空間 sso免登錄 添加配置 URL Types--URL schemes
#import "UMSocialSinaHandler.h" [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
4.4分享手機qq和qq空間

平台 | url scheme設置格式 |
---|---|
新浪微博 |
“sina.”+友盟appkey,例如“sina.51f8c20c56240b0de4007a06” (非微博原生sdk,現在只用新的)
|
騰訊微博 | “wb”+你在友盟后台綁定的騰訊微博App Key,例如“wb801345073”--(暫時因為沒有64位,所以不能用sso登錄); |
微信 | 微信應用appId(同appkey),例如“wxd9a39c7122aa6516”,微信詳細集成步驟參考微信集成方法 |
人人網 | “rm”+你的應用bundle id,例如“rmcom.umeng.SocialSDK” |
QQ、QQ空間 |
需要添加兩個URL schemes: 1. 手機qq:“QQ”+騰訊QQ互聯應用appId轉換成十六進制(不足8位前面補0),例如“QQ05FC5B14”。生成十六進制方法:
點擊鏈接,並在QQ互聯后台的URL schema中填入此字符串保持一致。2.qq空間:“tencent“+騰訊QQ互聯應用Id,例如“tencent100424468" ,QQ及Qzone詳細集成步驟參考
手機QQ集成方法。
(這2個分別對應qq 和 qq空間,因為默認只有sso登錄,所以必須要配置的)
|
來往 | Identifier填“Laiwang”,URL Schemes填來往AppId.注意使用來往SDK后,Xcode工程other linker flags需要添加-ObjC參數 |
易信 | 易信Appkey,例如“yx35664bdff4db42c2b7be1e29390c1a06” |
默認使用iOS自帶的Facebook分享framework,在iOS 6以上有效,若要使用我們提供的facebook分享需要設置“fb”+facebook AppID,例如“fb1440390216179601”,詳細集成方法見集成facebook |

#import "UMSocial.h" …… [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText:@"你要分享的文字" shareImage:[UIImage imageNamed:@"icon.png"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil] delegate:nil];

//實現回調方法(可選,delegate設置YES則實現回調): -(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response { //根據`responseCode`得到發送結果,如果分享成功 if(response.responseCode == UMSResponseCodeSuccess) { //得到分享到的微博平台名 NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]); } }
實際例子1--利用socialData分享到各個微博的擴展-各個平台各自實現方法,也是必須這么寫的方法: - (IBAction)shareAction:(UIBarButtonItem *)sender { UMSocialData *socialData = [UMSocialData defaultData]; socialData.extConfig.qqData.title = @"QQ分享標題"; socialData.extConfig.qqData.url = @"http://baidu.com"; socialData.extConfig.qzoneData.title = @"Qzone分享title"; socialData.extConfig.qzoneData.url = @"http://baidu.com"; socialData.extConfig.wechatSessionData.title = @"微信好友標題"; socialData.extConfig.wechatSessionData.url = @"http://baidu.com"; socialData.extConfig.wechatTimelineData.title = @"微信朋友圈title"; socialData.extConfig.wechatTimelineData.url = @"http://baidu.com"; [UMSocialSnsService presentSnsIconSheetView:self appKey:nil shareText:@"我在做軟件測試,朋友你無需理會" shareImage:[UIImage imageNamed:@"add_icon"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToEmail,UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSms,nil] delegate:nil]; }
- (IBAction)shareAction:(UIBarButtonItem *)sender { if (!_shareInfo) { return; } UMSocialData *socialData = [UMSocialData defaultData]; socialData.extConfig.qqData.title = _shareInfo.title; socialData.extConfig.qqData.url =_shareInfo.url; socialData.extConfig.qzoneData.title = _shareInfo.title; socialData.extConfig.qzoneData.url = _shareInfo.url; socialData.extConfig.wechatSessionData.title = _shareInfo.title; socialData.extConfig.wechatSessionData.url = _shareInfo.url; socialData.extConfig.wechatTimelineData.title = _shareInfo.title; socialData.extConfig.wechatTimelineData.url = _shareInfo.url; //網絡圖片url的分享,如果有url圖片,則本地圖片shareImage無效了,可以設置nil; [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:_shareInfo.ico]; [UMSocialSnsService presentSnsIconSheetView:self appKey:nil shareText:_shareInfo.content shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToEmail,UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToSms,nil] delegate:nil]; }
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
//根據`responseCode`得到發送結果,如果分享成功
if(response.responseCode == UMSResponseCodeSuccess)
{
//得到分享到的微博平台名
NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
}
}
接口說明:如果已經授權,則直接跳轉到分享編輯頁,否則將進行授權操作然后再跳轉到分享編輯頁.相比presentSnsIconSheetView方法,該接口不需要彈出分享面板。該接口支持所有平台的分享(QQ好友,微信會有選擇好友的界面)。
#import "UMSocial.h" …… //注意:分享到微信好友、微信朋友圈、微信收藏、QQ空間、QQ好友、來往好友、來往朋友圈、易信好友、易信朋友圈、Facebook、Twitter、Instagram等平台需要參考各自的集成方法。 (1)舉例:新浪微博 [[UMSocialControllerService defaultControllerService] setShareText:@"分享內嵌文字" shareImage:[UIImage imageNamed:@"icon"] socialUIDelegate:self]; //設置分享內容和回調對象 [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES); 注意:若彈出橫屏的頁面,必須要在出現編輯頁面前設置 [UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskLandscape]; 實現回調方法(可選): -(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response { //根據`responseCode`得到發送結果,如果分享成功 if(response.responseCode == UMSResponseCodeSuccess) { //得到分享到的微博平台名 NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]); } }
(2)微信: //使用UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite分別代表微信好友、微信朋友圈、微信收藏 [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession] content:@"分享內嵌文字" image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){ if (response.responseCode == UMSResponseCodeSuccess) { NSLog(@"分享成功!"); } }];
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"微信好友",@"微信朋友圈", nil]; [sheet showFromToolbar:self.navigationController.toolbar]; -------------------- -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex != actionSheet.cancelButtonIndex) { [UMSocialData defaultData].extConfig.title = self.modelInfo.i_title; [[UMSocialData defaultData].urlResource setResourceType:UMSocialUrlResourceTypeImage url:self.modelInfo.i_picURL]; [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb; NSString *str = [NSString stringWithFormat:@"http://m.baohuai.com/VideoShare.aspx?postID=%@",self.modelInfo.i_postID]; [UMSocialData defaultData].extConfig.wechatSessionData.url = str; [[UMSocialControllerService defaultControllerService]setShareText:self.modelInfo.i_content shareImage:nil socialUIDelegate:self]; if (buttonIndex ==0) { [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES); } if (buttonIndex ==1) { [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatTimeline].snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES); } } } -(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response { if (response.responseCode == UMSResponseCodeSuccess) { } }
圖:
打開各個微博平台的授權頁面 #import "UMSocial.h" …… //`snsName` 代表各個支持雲端分享的平台名,有`UMShareToSina`,`UMShareToTencent`等五個。 UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina]; snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response) { NSLog(@"response is %@",response); }); 授權之后獲取各個微博賬號數據,可以在上面的回調Block方法內實現下面的代碼, { //如果是授權到新浪微博,SSO之后如果想獲取用戶的昵稱、頭像等需要再次獲取一次賬戶信息 if ([platformName isEqualToString:UMShareToSina]) { [[UMSocialDataService defaultDataService] requestSocialAccountWithCompletion:^(UMSocialResponseEntity *accountResponse){ NSLog(@"SinaWeibo's user name is %@",[[[accountResponse.data objectForKey:@"accounts"] objectForKey:UMShareToSina] objectForKey:@"username"]); }]; } //這里可以獲取到騰訊微博openid,Qzone的token等 /* else if ([platformName isEqualToString:UMShareToTencent]) { [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToTencent completion:^(UMSocialResponseEntity *respose){ NSLog(@"get openid response is %@",respose); }]; } */ }
6.2免登陸集成法(如果不集成以下方法,則無法返回)如下:返回逗你玩
在AppDelegate文件集成相應的開關方法: #import "UMSocial.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //設置友盟社會化組件appkey [UMSocialData setAppKey:UmengAppkey]; //設置微信AppId、appSecret,分享url (sso登錄和添加微信的配置方法是同一個) [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"]; //設置手機QQ 的AppId,Appkey,和分享URL。(sso登錄和添加QQ和Qzone的配置方法是同一個) 需要#import "UMSocialQQHandler.h" [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"]; //打開新浪微博的SSO開關,設置新浪微博回調地址,這里必須要和你在新浪微博后台設置的回調地址一致。若在新浪后台設置我們的回調地址,“http://sns.whalecloud.com/sina2/callback”,這里可以傳nil ,需要 #import "UMSocialSinaHandler.h" [UMSocialSinaHandler openSSOWithRedirectURL:@"http://sns.whalecloud.com/sina2/callback"]; --------------------------------------------- 暫時不能使用; //打開騰訊微博SSO開關,設置回調地址,需要 #import "UMSocialTencentWeiboHandler.h" [UMSocialTencentWeiboHandler openSSOWithRedirectUrl:@"http://sns.whalecloud.com/tencent2/callback"]; 暫時不能使用; //打開人人網SSO開關,需要 #import "UMSocialRenrenHandler.h" [UMSocialRenrenHandler openSSO]; //設置易信Appkey和分享url地址,注意需要引用頭文件 #import UMSocialYixinHandler.h [UMSocialYixinHandler setYixinAppKey:@"yx35664bdff4db42c2b7be1e29390c1a06" url:@"http://www.umeng.com/social"]; //設置來往AppId,appscret,顯示來源名稱和url地址,注意需要引用頭文件 #import "UMSocialLaiwangHandler.h" [UMSocialLaiwangHandler setLaiwangAppId:@"8112117817424282305" appSecret:@"9996ed5039e641658de7b83345fee6c9" appDescription:@"友盟社會化組件" urlStirng:@"http://www.umeng.com/social"]; //設置Facebook,AppID和分享url,需要#import "UMSocialFacebookHandler.h" //默認使用iOS自帶的Facebook分享framework,在iOS 6以上有效。若要使用我們提供的facebook分享需要使用此開關: [UMSocialFacebookHandler setFacebookAppID:@"1440390216179601" shareFacebookWithURL:@"http://www.umeng.com/social"]; //默認使用iOS自帶的Twitter分享framework,在iOS 6以上有效。若要使用我們提供的twitter分享需要使用此開關: [UMSocialTwitterHandler openTwitter]; }