美文网首页shareSDK 集成分享-登录
ShareSDK第三方登录、分享5分钟集成

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

作者: 梦里风吹过 | 来源:发表于2016-07-30 13:34 被阅读202次

代码总结,自用。(微信、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限制,或根据ShareSDK文档配置解除不支持的https的网址
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.分享

//分享网络图片 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分钟集成

    本文链接:https://www.haomeiwen.com/subject/rxftsttx.html