iOS中facebook與twitter分享問題總結
阿新 • • 發佈:2018-12-31
移動分享是一個很普遍常見的問題,其中針對國外的app經常用到的無非這兩個。
以下是解決方案。
1.我也極力推薦的一個方案,ShareSDK,這個是中國自己的一個第三方整合的分享模組,裡面的功能很強大,包括目前所有的客戶端的分享。只要在官網上下載sharesdk的包,然後閱讀相關的文件,依樣畫葫蘆就行了。把包匯入到工程裡面,注意路徑的配置,然後註冊facebook和twitter的APIkey,然後是在ShareSDK官網上註冊appKey進行繫結。最後是程式碼裡面
1.1在appdelegate.m檔案裡面註冊
#pragma mark -- SSO授權本地初始化SDK
+ (void)initializePlatWithSSO
{
[ShareSDK registerApp:@"ebc7ea75b14"];
//twitter
[ShareSDK connectTwitterWithConsumerKey:@"WhdXQJq8TVnNtU9eyoCxQ"
consumerSecret:@"YY1975oq70MGojGXKuTxbgINXSrikfMfQDhSLr04"
redirectUri:@"http://website.com/"];
//facebook
[ShareSDK connectFacebookWithAppKey:@"703090989701898"
appSecret:@"59186b061caf72a34c7e5860cb65b745"];
}
然後- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [ShareSDK handleOpenURL:url
sourceApplication:sourceApplication
annotation:annotation
wxDelegate:self];
}
在要使用的類裡面,引入標頭檔案。
- (void)shareBySimpleway:(ShareType)type andContent:(NSString *)shareContent
{
// NSString *imagePath = [[NSBundle mainBundle] pathForResource:IMAGE_NAME ofType:IMAGE_EXT];
id<ISSShareOptions> simpleShareOptions =
[ShareSDK simpleShareOptionsWithTitle:RBTLocalizedString(@"25_setting_contentShare", @"內容分享")
shareViewDelegate:nil ];
id obj = nil;
if(self.musicType == ShareMusicByFacebook)
{
obj = [ShareSDK imageWithUrl:self.shareImgStr];
}
if (self.musicType == ShareMusicByTwitter || m_shareType == ShareTypeClient || m_shareType == ShareTypePlayList) {
obj = nil;
}
id<ISSContent> publishContent = [ShareSDK content:shareContent defaultContent:@"Greetings!"
image:obj
title:@"ShareSDK"
url:@"http://www.sharesdk.cn"
description:@"Music shared!"
mediaType:SSPublishContentMediaTypeNews];
[ShareSDK showShareViewWithType:type container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:simpleShareOptions result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (error != nil)
{
FXTRACE(FXT_DEBUG_LVL, @"Share", @"errorcode:%d errorDes:%@", [error errorCode], [error errorDescription]);
[Utils showToast:[error errorDescription]];
}
}];
}
基本完成了,ShareMusicByTwitter和facebook是自定義的,原版的是ShareTypeFacebook和ShareTypeTwitter.
方式2:webview的形式這種很簡單,
分享到facebook:
self.musicShareStr = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?m2w&s=100&p[title]=%@&p[summary]=%@&p[url]=%@&p[images][0]=%@",self.shareName,@"",urlString,self.shareImgStr];
分享到twitter:
self.musicShareStr = [NSString stringWithFormat:@"https://twitter.com/intent/tweet?&text=%@&url=%@",self.smsContent,urlStr1];
這種形式記住要清除快取:參考 http://wbqingheng.blog.163.com/blog/static/179383760201402073948573/
方式3:使用Facebook官方sdk分享
就是一般的方式了,有官方的sdk包,在plist檔案配置FacebookAppID和FacebookDisplayName,然後在appdelegate裡面配置
在didfinishload裡面加入 [FBAppEvents activateApp];
[FBAppCall handleDidBecomeActive];
在- (void)applicationWillTerminate:(UIApplication *)application
{
// FBSample logic
// if the app is going away, we close the session object
[FBSession.activeSession close];
}
在使用的類裡面配置:
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:[self setFacebookShareContent]
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{
if (error)
{
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
FXTRACE(FXT_ERROR_LVL, @"Share", @"Share to Facebook error:%@",
error.description);
[Utils showToast:RBTLocalizedString(@"25_setting_share_failed", @"failed")];
}
else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
// User cancelled.
FXTRACE(FXT_INFO_LVL, @"Share", @"User cancel shareing");
}
else
{
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:@"post_id"])
{
// User cancelled.
FXTRACE(FXT_INFO_LVL, @"Share", @"User cancel shareing");
} else {
// User clicked the Share button
FXTRACE(FXT_INFO_LVL, @"Share", @"Share success, story id:%@",
[urlParams valueForKey:@"post_id"]);
[Utils showToast:RBTLocalizedString(@"25_setting_share_success", @"success")];
}
}
}
}];
- (NSMutableDictionary *)setFacebookShareContent
{
ConfigManager *configMg = [ConfigManager sharedInstance];
NSMutableDictionary *shareFacebookDic=nil;
if ([self.titleS isEqualToString:NSLocalizedString(@"Share", @"")])
{
shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:configMg.shareClientAppToFbTextStr, @"name",
configMg.shareClientAppToFbUrlStr,@"link", nil];
}
else
{
shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:configMg.shareClientAppTextStr, @"name", nil];
}
if (!shareFacebookDic)
{
shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:RBTLocalizedString(@"default_share_Social", @"ShareFacebook"), @"name", nil];
}
return shareFacebookDic;
}
要配置這個分享的dic。
基本就這三種方式。
以下是解決方案。
1.我也極力推薦的一個方案,ShareSDK,這個是中國自己的一個第三方整合的分享模組,裡面的功能很強大,包括目前所有的客戶端的分享。只要在官網上下載sharesdk的包,然後閱讀相關的文件,依樣畫葫蘆就行了。把包匯入到工程裡面,注意路徑的配置,然後註冊facebook和twitter的APIkey,然後是在ShareSDK官網上註冊appKey進行繫結。最後是程式碼裡面
1.1在appdelegate.m檔案裡面註冊
#pragma mark -- SSO授權本地初始化SDK
+ (void)initializePlatWithSSO
{
[ShareSDK registerApp:@"ebc7ea75b14"];
[ShareSDK connectTwitterWithConsumerKey:@"WhdXQJq8TVnNtU9eyoCxQ"
consumerSecret:@"YY1975oq70MGojGXKuTxbgINXSrikfMfQDhSLr04"
redirectUri:@"http://website.com/"];
[ShareSDK connectFacebookWithAppKey:@"703090989701898"
appSecret:@"59186b061caf72a34c7e5860cb65b745"];
}
然後- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [ShareSDK handleOpenURL:url
sourceApplication:sourceApplication
annotation:annotation
wxDelegate:self];
}
在要使用的類裡面,引入標頭檔案。
- (void)shareBySimpleway:(ShareType)type andContent:(NSString *)shareContent
{
// NSString *imagePath = [[NSBundle mainBundle] pathForResource:IMAGE_NAME ofType:IMAGE_EXT];
id<ISSShareOptions> simpleShareOptions =
[ShareSDK simpleShareOptionsWithTitle:RBTLocalizedString(@"25_setting_contentShare", @"內容分享")
shareViewDelegate:nil ];
id obj = nil;
if(self.musicType == ShareMusicByFacebook)
{
obj = [ShareSDK imageWithUrl:self.shareImgStr];
}
if (self.musicType == ShareMusicByTwitter || m_shareType == ShareTypeClient || m_shareType == ShareTypePlayList) {
obj = nil;
}
id<ISSContent> publishContent = [ShareSDK content:shareContent defaultContent:@"Greetings!"
image:obj
title:@"ShareSDK"
url:@"http://www.sharesdk.cn"
description:@"Music shared!"
mediaType:SSPublishContentMediaTypeNews];
[ShareSDK showShareViewWithType:type container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:simpleShareOptions result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (error != nil)
{
FXTRACE(FXT_DEBUG_LVL, @"Share", @"errorcode:%d errorDes:%@", [error errorCode], [error errorDescription]);
[Utils showToast:[error errorDescription]];
}
}];
}
基本完成了,ShareMusicByTwitter和facebook是自定義的,原版的是ShareTypeFacebook和ShareTypeTwitter.
方式2:webview的形式這種很簡單,
分享到facebook:
self.musicShareStr = [NSString stringWithFormat:@"http://www.facebook.com/sharer.php?m2w&s=100&p[title]=%@&p[summary]=%@&p[url]=%@&p[images][0]=%@",self.shareName,@"",urlString,self.shareImgStr];
分享到twitter:
self.musicShareStr = [NSString stringWithFormat:@"https://twitter.com/intent/tweet?&text=%@&url=%@",self.smsContent,urlStr1];
這種形式記住要清除快取:參考
方式3:使用Facebook官方sdk分享
就是一般的方式了,有官方的sdk包,在plist檔案配置FacebookAppID和FacebookDisplayName,然後在appdelegate裡面配置
在didfinishload裡面加入 [FBAppEvents activateApp];
[FBAppCall handleDidBecomeActive];
在- (void)applicationWillTerminate:(UIApplication *)application
{
// FBSample logic
// if the app is going away, we close the session object
[FBSession.activeSession close];
}
在使用的類裡面配置:
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:[self setFacebookShareContent]
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{
if (error)
{
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
FXTRACE(FXT_ERROR_LVL, @"Share", @"Share to Facebook error:%@",
error.description);
[Utils showToast:RBTLocalizedString(@"25_setting_share_failed", @"failed")];
}
else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
// User cancelled.
FXTRACE(FXT_INFO_LVL, @"Share", @"User cancel shareing");
}
else
{
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:@"post_id"])
{
// User cancelled.
FXTRACE(FXT_INFO_LVL, @"Share", @"User cancel shareing");
} else {
// User clicked the Share button
FXTRACE(FXT_INFO_LVL, @"Share", @"Share success, story id:%@",
[urlParams valueForKey:@"post_id"]);
[Utils showToast:RBTLocalizedString(@"25_setting_share_success", @"success")];
}
}
}
}];
- (NSMutableDictionary *)setFacebookShareContent
{
ConfigManager *configMg = [ConfigManager sharedInstance];
NSMutableDictionary *shareFacebookDic=nil;
if ([self.titleS isEqualToString:NSLocalizedString(@"Share", @"")])
{
shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:configMg.shareClientAppToFbTextStr, @"name",
configMg.shareClientAppToFbUrlStr,@"link", nil];
}
else
{
shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:configMg.shareClientAppTextStr, @"name", nil];
}
if (!shareFacebookDic)
{
shareFacebookDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:RBTLocalizedString(@"default_share_Social", @"ShareFacebook"), @"name", nil];
}
return shareFacebookDic;
}
要配置這個分享的dic。
基本就這三種方式。