代碼總結镇眷,自用。(微信翎嫡、QQ欠动、新浪微博)
1.CocoaPods導入
#ShareSDK
pod 'ShareSDK3'
pod 'MOBFoundation'
pod 'ShareSDK3/ShareSDKUI'
pod 'ShareSDK3/ShareSDKPlatforms/QQ'
pod 'ShareSDK3/ShareSDKPlatforms/SinaWeibo'
pod 'ShareSDK3/ShareSDKPlatforms/WeChat'
2.橋接文件導入頭文件
//ShareSDK
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//騰訊開放平臺(對應QQ和QQ空間)SDK頭文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信SDK頭文件
#import "WXApi.h"
//新浪微博SDK頭文件
#import "WeiboSDK.h"
3.info.plist文件配置
a.解除http限制,或根據(jù)ShareSDK文檔配置解除不支持的https的網(wǎng)址
b.使用文本編輯器打開info.plist文件
c.在合適的位置加入以下內容
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weibosdk2.5</string>
<string>weibosdk</string>
<string>sinaweibohdsso</string>
<string>sinaweibosso</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>mqzone</string>
<string>mqzoneopensdkapi</string>
<string>mqzoneopensdk</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>wtloginmqq2</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqopensdkapiV2</string>
<string>mqqwpa</string>
<string>wtloginmqq2</string>
<string>mqqopensdkapiV3</string>
<string>weixin</string>
<string>wechat</string>
<string>mqq</string>
<string>mqqapi</string>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>QQ41DC8EC5</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>wb61631250</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>wx9a26e7f457e16694</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>tencent1104973509</string>
</array>
</dict>
</array>
4.AppDelegate extension
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
setShare()
return true
}
//MARK: 注冊
extension AppDelegate{
//注冊分享
func setShare() {
ShareSDK.registerApp("e01aa96e9188", activePlatforms: [SSDKPlatformType.TypeSinaWeibo.rawValue,SSDKPlatformType.TypeQQ.rawValue,SSDKPlatformType.SubTypeWechatSession.rawValue,SSDKPlatformType.SubTypeWechatTimeline.rawValue], onImport: { (platformType) in
switch platformType{
case SSDKPlatformType.TypeSinaWeibo:
ShareSDKConnector.connectWeibo(WeiboSDK.classForCoder())
case SSDKPlatformType.TypeQQ:
ShareSDKConnector.connectQQ(QQApiInterface.classForCoder(), tencentOAuthClass: TencentOAuth.classForCoder())
case SSDKPlatformType.TypeWechat:
ShareSDKConnector.connectWeChat(WXApi.classForCoder())
default:
break
}
}) { (platformType, appInfo) in
switch platformType{
case SSDKPlatformType.TypeSinaWeibo:
appInfo.SSDKSetupSinaWeiboByAppKey("61631250", appSecret: "***", redirectUri: "http://www.tetimes.com/appdownload/", authType: SSDKAuthTypeBoth)
case SSDKPlatformType.TypeWechat:
appInfo.SSDKSetupWeChatByAppId("wx9a26e7f457e16694", appSecret: "***")
case SSDKPlatformType.TypeQQ:
appInfo.SSDKSetupQQByAppId("1104973509", appKey: "***", authType: SSDKAuthTypeBoth)
default:
break
}
}
}
}
5.第三方登錄
thirdLoginWihtType(.TypeQQ)
thirdLoginWihtType(.TypeSinaWeibo)
thirdLoginWihtType(.TypeWechat)
func thirdLoginWihtType(type:SSDKPlatformType) {
ShareSDK.getUserInfo(type) { (state, user, error) in
if state == SSDKResponseState.Success{
// print(user)
print(user.uid)
print(user.nickname)
print(user.icon)
}else{
print(error.localizedDescription)
MBProgressHUD.showText("登錄失敗")
}
}
}
6.分享
//分享網(wǎng)絡圖片 images = ["http://image.png"]
//分享圖片為本地圖片如下
let shareParams = NSMutableDictionary()
shareParams.SSDKSetupShareParamsByText("內容", images: UIImage(named: "icon.png"), url: NSURL(string: HOSTNAME + AboutUs), title: "標題", type: .Auto)
ShareSDK.showShareActionSheet(nil, items: nil, shareParams: shareParams) { (state, platformType, userData, contentEntity, error, end) in
if state == .Success{
print("成功成功")
}else{
print("分享失敗")
}
}