STShareTool - 常見的社會化分享,包含(QQ、QQ控件、微信好友、微信朋友圈、新浪微博)

jopen 8年前發布 | 17K 次閱讀 iOS開發 移動開發

STShareTool

常見的社會化分享,包含(QQ、QQ控件、微信好友、微信朋友圈、新浪微博)

沒有一次和三方打交道是愉快的,尤其是三方分享、三方登錄、三方支付、三方地圖等等。這些都是非常常用的組件,iOS9 出來以后,又對安全性做了要求,配置相當繁瑣,所以干脆封裝了,記錄一下流程。

申請 AppKey

第一步就是先去各個平臺上申請 AppKey。

QQ 開放平臺

http://open.qq.com

該平臺申請的包含 QQ 和 QQ 空間。暫時沒發現有什么需要注意的地方。

微信開放平臺

http://open.weixin.qq.com

該平臺申請的包含 微信好友 和 微信朋友圈。

微博開放平臺

http://open.weibo.com

呵呵呵呵呵呵,最坑的就是微博了好嗎。

  • 記著文案要符合要求:應用的作用、使用方法什么的。
  • 應用地址,如果還沒上線的話,可以先用默認 URL 占位:https://itunes.apple.com/cn/genre/yin-le/id34。
  • 設置安全域名為:sns.whalecloud.com。
  • 應用介紹圖片,記著要有【新浪微博】的標示(icon 什么的,表示該頁面有分享)。
  • 在應用信息->高級信息里面,將授權回調和取消授權回調設置為:http://sns.whalecloud.com/sina2/callback。

友盟

http://www.umeng.com

工程配置

導入庫

  • ImageIO.framework
  • libz.tbd
  • libstdc++.tbd
  • CoreTelephony.framework
  • libsqlite3.tbd
  • CoreGraphics.framework
  • SystemConfiguration.framework
  • libiconv.tbd
  • Security.framework

配置 Build Setting

找到 Other Linker Flags ,添加兩項 -all_load 和 -ObjC。要不然會報 wb…base64...的錯,運行就崩潰好嗎。

配置完,別忘了 Clean 一下,防止有緩存。

配置 URL Types

點擊工程名稱,找到 info 選項卡,找到最后的 URL Types 。

添加四個 URL Types。分別是 QQ 、微信、微博。( 對 QQ 要配置兩個,所以三個平臺要有四個 URL Types...

只用填寫 URL Schemes 就行,規則如下:

  • 微信:直接是微信的 AppId。
  • QQ1:【QQ + QQ 的 AppId 的十六進制(不足8位前面補0)】
  • QQ2:【tencent + AppId】
  • 微博:【wb + 微博的 AppKey】

配置好以后,大概是這樣的:

配置 info.plist

  • 設置 Bundle display name ,這會在應用見跳轉和分享的時候顯示。
  • 設置 App Transport Security Settings ,我設置的 Allow Arbitrary Loads 為 YES,當然你也可以逐個添加白名單。
  • 設置 LSApplicationQueriesSchemes。這個我就直接貼 Source Code 了
<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>weibosdk2.5</string>
        <string>weibosdk</string>
        <string>mqqapiwallet</string>
        <string>mqqwpa</string>
        <string>mqqbrowser</string>
        <string>wtloginmqq2</string>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqopensdkapiV3</string>
        <string>mqqapi</string>
        <string>mqq</string>
        <string>sinaweibo</string>
        <string>wechat</string>
        <string>mqzone</string>
        <string>weixin</string>
    </array>

友盟

通用的一些配置就到這了,下面是對本工具的介紹。

首先,我是在友盟基礎上進行封裝的。所以,要先去下載最新版的友盟 SDK

STShareTool 使用

首先,在 STShareTool.h 中配置各種 key

static NSString * const STShareImageKey = @"STShareImageKey"; ///< 分享圖片的 key (傳入的是字典,所以這個作為key)(image 是 UIImage)
static NSString * const STShareContentKey = @"STShareContentKey"; ///< 分享內容的 key (content 是 NSString 的)
static NSString * const STShareURLKey = @"STShareURLKey"; ///< 分享 url 的 key (url 是 NSString 的)

static NSString * const STShareTitle = @"SwiftGG"; ///< 分享的標題
static NSString * const STShareURL = @"http://www.swift.gg"; ///< 分享的url,實際沒用到,寫在這占位

static NSString * const STShareQQAppId = @"";
static NSString * const STShareQQAppKey = @"";

static NSString * const STShareWeiboAppKey = @"";
static NSString * const STShareWeiboAppSecret = @"";
static NSString * const STShareWeiboCallbackURL = @"http://sns.whalecloud.com/sina2/callback"; ///< 微博回調的 url,就是微博申請高級信息里面那個

static NSString * const STShareWechatAppId = @"";
static NSString * const STShareWechatAppSecret = @"";

static NSString * const STShareUMAppKey = @"";

使用的時候,只需要一行代碼就可以。(當然,還需要一行來配置要分享的內容...

NSDictionary *shareContent = @{STShareContentKey : @"SwiftGG 最帥",
                               STShareImageKey : [UIImage imageNamed:@"60"],
                               STShareURLKey : @"http://www.swift.gg"};
[STShareTool presentShareViewController:shareContent];

自定義界面

以上直接 presentShareViewController: 的是用的 UIActivityViewController 進行分享。如果想要自定義,那也 ok,所有方法都是現成的,你只需要自己寫一個界面即可。

界面搞定以后,調用 STShareTool 的以下方法即可進行各個平臺的分享:

+ (void)shareToQQ:(NSDictionary *)shareContent;
+ (void)shareToQZone:(NSDictionary *)shareContent;
+ (void)shareToWeChatSession:(NSDictionary *)shareContent;
+ (void)shareToWeChatTimeline:(NSDictionary *)shareContent;
+ (void)shareToWeibo:(NSDictionary *)shareContent;

對,你沒看錯,全都是類方法,直接用 STShareTool 進行調用,并且!命名都一毛一樣有木有,全都是 shareTo...,所以,可以用循環來 performSelector

最后

三方分享還有問題嗎?有問題提 issue 啊!

項目地址: https://github.com/saitjr/STShareTool

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!