首先聲明demo的GitHub地址:https://github.com/jaydenin/ReactNativeShare
創建項目和申請各個平臺的賬號要先弄好,這里就不一一說明了。
流程可以參考友盟的官網文檔http://dev.umeng.com/social/ios/quick-integration
1.1.1 下載友盟的SDK
需要分享到那個平臺就下載現在那個
1.1.2 接入SDK
打開xcode,把下載的 UMSocial 文件夾拉到自己工程中
添加項目配置
按下圖修改,可以搜索關鍵詞other linker
在Other Linker Flags加入
-ObjC
請直接復制
加入SDK依賴的系統庫
首先添加SDK的二個依賴
libsqlite3.tbd
CoreGraphics.framework
1.1.3 添加第三方平臺依賴庫
根據集成的不同平臺加入相關的依賴庫,未列出平臺則不需添加 添加方式:選中項目Target -> General -> Linked Frameworks and Libraries列表中進行添加 (上圖中繼續添加)
微信(完整版)-精簡版無需添加以下依賴庫
SystemConfiguration.framework
CoreTelephony.framework
libsqlite3.tbd
libc++.tbd
libz.tbd
QQ/QZone/TIM(完整版)-精簡版無需添加以下依賴庫
SystemConfiguration.framework
libc++.tbd
新浪微博(完整版)-精簡版無需添加以下依賴庫
SystemConfiguration.framework
CoreTelephony.framework
ImageIO.framework
libsqlite3.tbd
libz.tbd
CoreData.framework
Twitter平臺加入后需在Twitter目錄右鍵->Add files to "Twitter"->添加TwitterKit.framework/Resources/TwitterKitResources.bundle。
短信
MessageUI.framework
SafariServices.framework
VKontakte
CoreGraphics.framework
Security.framework
注意:添加時要看清楚是否已經添加過,請勿重復添加!!!!
1.2 第三方平臺配置
1.2.1 配置SSO白名單
在iOS9以上系統需要增加一個可跳轉App的白名單,即LSApplicationQueriesSchemes
否則將在SDK判斷是否跳轉時用到的canOpenURL時返回NO,進而只進行webview授權或授權/分享失敗
在項目中的info.plist中加入應用白名單,右鍵info.plist選擇source code打開(plist具體設置在Build Setting -> Packaging -> Info.plist File可獲取plist路徑) 請根據選擇的平臺對以下配置進行縮減:
<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、TIM 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>mqqopensdkapiV4</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>
<string>tim</string>
<string>timapi</string>
<string>timopensdkfriend</string>
<string>timwpa</string>
<string>timgamebindinggroup</string>
<string>timapiwallet</string>
<string>timOpensdkSSoLogin</string>
<string>wtlogintim</string>
<string>timopensdkgrouptribeshare</string>
<string>timopensdkapiV4</string>
<string>timgamebindinggroup</string>
<string>timopensdkdataline</string>
<string>wtlogintimV1</string>
<string>timapiV1</string>
<!-- 支付寶 URL Scheme 白名單-->
<string>alipay</string>
<string>alipayshare</string>
<!-- 釘釘 URL Scheme 白名單-->
<string>dingtalk</string>
<string>dingtalk-open</string>
<!--Linkedin URL Scheme 白名單-->
<string>linkedin</string>
<string>linkedin-sdk2</string>
<string>linkedin-sdk</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>
<!-- Kakao URL Scheme 白名單-->
<!-- 注:以下第一個參數需替換為自己的kakao appkey-->
<!-- 格式為 kakao + "kakao appkey"-->
<string>kakaofa63a0b2356e923f3edd6512d531f546</string>
<string>kakaokompassauth</string>
<string>storykompassauth</string>
<string>kakaolink</string>
<string>kakaotalk-4.5.0</string>
<string>kakaostory-2.9.0</string>
<!-- pinterest URL Scheme 白名單-->
<string>pinterestsdk.v1</string>
<!-- Tumblr URL Scheme 白名單-->
<string>tumblr</string>
<!-- 印象筆記 -->
<string>evernote</string>
<string>en</string>
<string>enx</string>
<string>evernotecid</string>
<string>evernotemsg</string>
<!-- 有道云筆記-->
<string>youdaonote</string>
<string>ynotedictfav</string>
<string>com.youdao.note.todayViewNote</string>
<string>ynotesharesdk</string>
<!-- Google+-->
<string>gplus</string>
<!-- Pocket-->
<string>pocket</string>
<string>readitlater</string>
<string>pocket-oauth-v1</string>
<string>fb131450656879143</string>
<string>en-readitlater-5776</string>
<string>com.ideashower.ReadItLaterPro3</string>
<string>com.ideashower.ReadItLaterPro</string>
<string>com.ideashower.ReadItLaterProAlpha</string>
<string>com.ideashower.ReadItLaterProEnterprise</string>
<!-- VKontakte-->
<string>vk</string>
<string>vk-share</string>
<string>vkauthorize</string>
</array>
比如下圖
1.2.1.1 異常情況
如果出現類似failed for URL: ""weibosdk://""-error,可能是以下情況:
在模擬器上可能出現此log,因為沒安裝對應app,類似還有微信、QQ等需要打開相應app的都會有這樣的系統log,此條日志可以忽略。
而如果出現error: "This app is not allowed to query for scheme xxx",說明白名單沒有配置,請檢查上述配置是否正確加入工程。
1.2.2 URL Scheme
URL Scheme是通過系統找到并跳轉對應app的設置,通過向項目中的info.plist文件中加入URL types可使用第三方平臺所注冊的appkey信息向系統注冊你的app,當跳轉到第三方應用授權或分享后,可直接跳轉回你的app。
添加URL Types可工程設置面板設置
如果要分享多個可以添加多個URL types
- 初始化設置
2.1 初始化U-Share及第三方平臺
應用啟動后進行U-Share和第三方平臺的初始化工作 以下代碼將所有平臺初始化示例放出,開發者根據平臺需要選取相應代碼,并替換為所屬注冊的appKey和appSecret。
在AppDelegate.m中設置如下代碼
注意并不是所有分享平臺都需要配置對應的Appkey,比如WhatsApp、印象筆記平臺會直接通過AirDrop方式分享,而短信和郵件會直接調用系統自帶的應用進行分享,這兩種分享方式均不需要配置對應的三方Appkey
#import <UMSocialCore/UMSocialCore.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* 打開調試日志 */
[[UMSocialManager defaultManager] openLog:YES];
/* 設置友盟appkey */
[[UMSocialManager defaultManager] setUmSocialAppkey:USHARE_DEMO_APPKEY];
[self configUSharePlatforms];
[self confitUShareSettings];
// Custom code
return YES;
}
- (void)confitUShareSettings
{
/*
* 打開圖片水印
*/
//[UMSocialGlobal shareInstance].isUsingWaterMark = YES;
/*
* 關閉強制驗證https,可允許http圖片分享,但需要在info.plist設置安全域名
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
*/
//[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}
- (void)configUSharePlatforms
{
/*
設置微信的appKey和appSecret
[微信平臺從U-Share 4/5升級說明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_1
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wxdc1e388c3822c80b" appSecret:@"3baf1193c85774b3fd9d18447d76cab0" redirectURL:nil];
/*
* 移除相應平臺的分享,如微信收藏
*/
//[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
/* 設置分享到QQ互聯的appID
* U-Share SDK為了兼容大部分平臺命名,統一用appKey和appSecret進行參數設置,而QQ平臺僅需將appID作為U-Share的appKey參數傳進即可。
100424468.no permission of union id
[QQ/QZone平臺集成說明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_3
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1105821097"/*設置QQ平臺的appID*/ appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
/*
設置新浪的appKey和appSecret
[新浪微博集成說明]http://dev.umeng.com/social/ios/%E8%BF%9B%E9%98%B6%E6%96%87%E6%A1%A3#1_2
*/
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"3921700954" appSecret:@"04b48b094faeb16683c32669824ebdad" redirectURL:@"https://sns.whalecloud.com/sina2/callback"];
/* 釘釘的appKey */
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_DingDing appKey:@"dingoalmlnohc0wggfedpk" appSecret:nil redirectURL:nil];
/* 支付寶的appKey */
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_AlipaySession appKey:@"2015111700822536" appSecret:nil redirectURL:nil];
/* 設置易信的appKey */
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_YixinSession appKey:@"yx35664bdff4db42c2b7be1e29390c1a06" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
/* 設置點點蟲(原來往)的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_LaiWangSession appKey:@"8112117817424282305" appSecret:@"9996ed5039e641658de7b83345fee6c9" redirectURL:@"http://mobile.umeng.com/social"];
/* 設置領英的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Linkedin appKey:@"81t5eiem37d2sc" appSecret:@"7dgUXPLH8kA8WHMV" redirectURL:@"https://api.linkedin.com/v1/people"];
/* 設置Twitter的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Twitter appKey:@"fB5tvRpna1CKK97xZUslbxiet" appSecret:@"YcbSvseLIwZ4hZg9YmgJPP5uWzd4zr6BpBKGZhf07zzh3oj62K" redirectURL:nil];
/* 設置Facebook的appKey和UrlString */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Facebook appKey:@"506027402887373" appSecret:nil redirectURL:nil];
/* 設置Pinterest的appKey */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Pinterest appKey:@"4864546872699668063" appSecret:nil redirectURL:nil];
/* dropbox的appKey */
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_DropBox appKey:@"k4pn9gdwygpy4av" appSecret:@"td28zkbyb9p49xu" redirectURL:@"https://mobile.umeng.com/social"];
/* vk的appkey */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_VKontakte appKey:@"5786123" appSecret:nil redirectURL:nil];
}
2.2 設置系統回調
// 支持所有iOS系統
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
//6.3的新的API調用,是為了兼容國外平臺(例如:新版facebookSDK,VK等)的調用[如果用6.2的api調用會沒有回調],對國內平臺沒有影響
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回調
}
return result;
}
到此基本上集成成功了,可以運行試一試是否報錯,如沒有可以繼續下一步rn的接入。
首先可以先在xcode新建二個文件
然后可以復制下面的代碼
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
// 導入RCTBridgeModule類,這個是react-native提供
#import <React/RCTBridgeModule.h>
// 遵守RCTBridgeModul協議
@interface share : NSObject<RCTBridgeModule>
@end
#import "share.h"
#import <React/RCTBridgeModule.h>
#import <UMSocialCore/UMSocialCore.h>
#import <UMSocialCore/UMSocialManager.h>
@implementation share
RCT_EXPORT_MODULE(share)
RCT_EXPORT_METHOD(share:(NSInteger )platformType title:(NSString*)title descr:(NSString*)descr
thumbURL:(NSString*)thumbURLl
webpageUrl:(NSString*)webpageUrl
callback:(RCTResponseSenderBlock)callback
)
{
//創建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//創建網頁內容對象
NSString* thumbURL = thumbURLl;
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:descr thumImage:thumbURL];
//設置網頁地址
shareObject.webpageUrl = webpageUrl;
//分享消息對象設置分享內容對象
messageObject.shareObject = shareObject;
dispatch_async(dispatch_get_main_queue(), ^{
//調用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
NSString * code = [NSString stringWithFormat:@"%ld",(long)error.code];
NSString *message = data;
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的數據
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
code = @"200";
message = resp.originalResponse;
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
callback( [[NSArray alloc] initWithObjects:code,message, nil]);
}];
});
}
@end
到此可以寫rn的代碼了,調用起來非常方便
只需要在你的分享的點擊方法中寫上(記得import NativeModules)
NativeModules.share.share(
i + 1, //分享的平臺
data.title,//分享的標題
data.subTitle,//分享的內容
data.logo,//分享圖片的URL
data.link,//分享的URL
(code,data) => {
if (code == '200'){
console.log('分享成功')
} else {
console.log('分享失敗 ' + code + ' ' + data)
}
}
)
其中分享的平臺platformType的類型可以參考SDK的文件
對于Android可以參考另外的博客:
http://blog.csdn.net/u013718120/article/details/75040805