ShareSDK第三方登录、分享5分钟集成

代码总结,自用。(微信、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 
#import 

//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import 
#import 

//微信SDK头文件
#import "WXApi.h"

//新浪微博SDK头文件
#import "WeiboSDK.h"

3.info.plist文件配置

a.解除http限制,或根据ShareSDK文档配置解除不支持的https的网址
b.使用文本编辑器打开info.plist文件
c.在合适的位置加入以下内容

LSApplicationQueriesSchemes
    
        weibosdk2.5
        weibosdk
        sinaweibohdsso
        sinaweibosso
        sinaweibohd
        sinaweibo
        mqzone
        mqzoneopensdkapi
        mqzoneopensdk
        mqzoneopensdkapi19
        mqzoneopensdkapiV2
        mqqopensdkapiV3
        wtloginmqq2
        mqqOpensdkSSoLogin
        mqqopensdkapiV2
        mqqwpa
        wtloginmqq2
        mqqopensdkapiV3
        weixin
        wechat
        mqq
        mqqapi
    
CFBundleURLTypes
    
        
            CFBundleTypeRole
            Editor
            CFBundleURLSchemes
            
                QQ41DC8EC5
            
        
        
            CFBundleTypeRole
            Editor
            CFBundleURLSchemes
            
                wb61631250
            
        
        
            CFBundleTypeRole
            Editor
            CFBundleURLSchemes
            
                wx9a26e7f457e16694
            
        
        
            CFBundleTypeRole
            Editor
            CFBundleURLSchemes
            
                tencent1104973509
            
        
    

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.分享

//分享网络图片 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("分享失败")
        }
}

你可能感兴趣的:(ShareSDK第三方登录、分享5分钟集成)