swift3.0集成友盟分享sdk_6.0.3(二)

本文档详细介绍了如何在Swift3.0中集成友盟分享SDK的步骤,包括设置appKey和appSecret,以及申请微信、QQ、微博等社交平台的授权。在集成过程中,需要注意在AppDelegate.swift和Info.plist中同步更新URL Types,以确保正确配置。作者还展示了使用友盟SDK进行音乐、视频分享的测试流程,并提供了测试账号的示例。

经过(一)的讲解,大家应该可以按照友盟提供的测试账号可以集成友盟分享了,友盟目前集合了18APP27种分享,可以授权的有10App:微信、QQ、新浪微博、腾讯微博、人人网、豆瓣、FacebookTwitterLinkedin领英、Kakao
(二)主要在(一)基础上加了音乐、视频分享,替换了友盟的部分测试账号为自己申请的appKeyappSecret,注意替换时不仅要替换AppDelegate.swift中的application didFinishLaunchingWithOptions的代码,还要同时替换Info中的URL Types,两者是配套使用的!

作者首先申请了友盟的Appkey 5832b93b99f0c74ef10019c8,在didFinishLaunchingWithOptions的代码中替换,同时对微博微信 QQ这三个主流的Open平台进行了申请

微博

1577306477

b1f0cd8cd2140c407a8c20c319155df8

QQ

1105839186

CKFSBH6Fw21RN5pu

微信

wxd795d58c78ac222b

779c58188ca57046f76353ea1e84412c

代码如下

//
//  AppDelegate.swift
//  umengtest
//
//  Created by targetcloud on 2016/11/21.
//  Copyright © 2016年 targetcloud. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        UMSocialManager.default().openLog(true)
        UMSocialManager.default().umSocialAppkey = "57b432afe0f55a9832001a0a"
//        UMSocialManager.default().umSocialAppkey = "5832b93b99f0c74ef10019c8"
        print("UMeng social version:\(UMSocialGlobal.umSocialSDKVersion())" );
        
        UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: "wxdc1e388c3822c80b", appSecret: "3baf1193c85774b3fd9d18447d76cab0", redirectURL: "http://mobile.umeng.com/social")
//        UMSocialManager.default().setPlaform(UMSocialPlatformType.wechatSession, appKey: "wxd795d58c78ac222b", appSecret: "779c58188ca57046f76353ea1e84412c", redirectURL: "http://mobile.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: "100424468", appSecret: nil, redirectURL: "http://mobile.umeng.com/social")
//        UMSocialManager.default().setPlaform(UMSocialPlatformType.QQ, appKey: "1105839186", appSecret: nil, redirectURL: "http://mobile.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.alipaySession, appKey: "2015111700822536", appSecret: nil, redirectURL: "http://mobile.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.yixinSession, appKey: "yx35664bdff4db42c2b7be1e29390c1a06", appSecret: nil, redirectURL: "http://mobile.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.laiWangSession, appKey: "8112117817424282305", appSecret: "9996ed5039e641658de7b83345fee6c9", redirectURL: "http://mobile.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.sina, appKey: "3921700954", appSecret: "04b48b094faeb16683c32669824ebdad", redirectURL: "http://sns.whalecloud.com/sina2/callback")
//        UMSocialManager.default().setPlaform(UMSocialPlatformType.sina, appKey: "1577306477", appSecret: "b1f0cd8cd2140c407a8c20c319155df8", redirectURL: "http://sns.whalecloud.com/sina2/callback")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.linkedin, appKey: "81t5eiem37d2sc", appSecret: "7dgUXPLH8kA8WHMV", redirectURL: "https://api.linkedin.com/v1/people")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.twitter, appKey: "fB5tvRpna1CKK97xZUslbxiet", appSecret: "YcbSvseLIwZ4hZg9YmgJPP5uWzd4zr6BpBKGZhf07zzh3oj62K", redirectURL: nil)
        UMSocialManager.default().setPlaform(UMSocialPlatformType.facebook, appKey: "506027402887373", appSecret: nil, redirectURL: "http://www.umeng.com/social")
        UMSocialManager.default().setPlaform(UMSocialPlatformType.pinterest, appKey: "4864546872699668063", appSecret: nil, redirectURL: nil)

//        UMSocialManager.default().removePlatformProvider(withPlatformTypes: [UMSocialPlatformType.wechatFavorite,UMSocialPlatformType.yixinTimeLine,UMSocialPlatformType.laiWangTimeLine,UMSocialPlatformType.qzone])
        return true
    }
    
    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        let result = UMSocialManager.default().handleOpen(url)
        if !result{
            
        }
        return result
    }
    
    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        let result = UMSocialManager.default().handleOpen(url)
        if !result{
            
        }
        return result
    }
}

//
//  ViewController.swift
//  umengtest
//
//  Created by targetcloud on 2016/11/21.
//  Copyright © 2016年 targetcloud. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let shareButton:UIButton = UIButton(frame: CGRect(x: 50, y: 50, width: 200, height: 50))
//        shareButton.center = CGPoint(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 2)
        shareButton.setTitle("Swift图文分享", for: UIControlState())
        shareButton.addTarget(self, action: #selector(shareAction_pic), for: UIControlEvents.touchUpInside)
        shareButton.setTitleColor(UIColor.blue, for: UIControlState())
        shareButton.backgroundColor = UIColor.clear
        
        let sharewww : UIButton = UIButton(frame: CGRect(x: 50, y: 100, width: 200, height: 50))
        sharewww.setTitle("Swift网页分享", for: UIControlState())
        sharewww.addTarget(self, action: #selector(shareAction_www), for: UIControlEvents.touchUpInside)
        sharewww.setTitleColor(UIColor.blue, for: UIControlState())
        sharewww.backgroundColor = UIColor.clear
        
        let sharetxt : UIButton = UIButton(frame: CGRect(x: 50, y: 150, width: 200, height: 50))
        sharetxt.setTitle("Swift文字分享", for: UIControlState())
        sharetxt.addTarget(self, action: #selector(shareAction_txt), for: UIControlEvents.touchUpInside)
        sharetxt.setTitleColor(UIColor.blue, for: UIControlState())
        sharetxt.backgroundColor = UIColor.clear
        
        let shareMusic : UIButton = UIButton(frame: CGRect(x: 50, y: 200, width: 200, height: 50))
        shareMusic.setTitle("Swift音乐分享", for: UIControlState())
        shareMusic.addTarget(self, action: #selector(shareAction_music), for: UIControlEvents.touchUpInside)
        shareMusic.setTitleColor(UIColor.blue, for: UIControlState())
        shareMusic.backgroundColor = UIColor.clear
        
        let shareVideo : UIButton = UIButton(frame: CGRect(x: 50, y: 250, width: 200, height: 50))
        shareVideo.setTitle("Swift视频分享", for: UIControlState())
        shareVideo.addTarget(self, action: #selector(shareAction_video), for: UIControlEvents.touchUpInside)
        shareVideo.setTitleColor(UIColor.blue, for: UIControlState())
        shareVideo.backgroundColor = UIColor.clear
        
        self.view.addSubview(shareButton)
        self.view.addSubview(sharewww)
        self.view.addSubview(sharetxt)
        self.view.addSubview(shareMusic)
        self.view.addSubview(shareVideo)
    }

    func shareAction_pic (){
        UMSocialUIManager.showShareMenuViewInWindow {[weak self] (shreMenuView, platformType) in
            let messageObject:UMSocialMessageObject = UMSocialMessageObject.init()
            messageObject.text = "友盟分享测试(图片),swift3.0 Xcode8.1 umeng6.0.3 作者:targetcloud"
            let shareObject:UMShareImageObject = UMShareImageObject.init()
            shareObject.title = "图片分享"
            shareObject.descr = "这里是图片分享测试,作者:targetcloud"
            shareObject.thumbImage = UIImage.init(named: "icon")
            shareObject.shareImage = "http://dev.umeng.com/images/tab2_1.png"
            messageObject.shareObject = shareObject;
            UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: self, completion: { (shareResponse, error) in
                if error != nil {
                    print("Share Fail with error :%@", error)
                }else{
                    self?.getUserInfoForPlatform(platformType: platformType)
                    print("Share succeed")
                }
            })
        }
    }
    
    func shareAction_www (){
        UMSocialUIManager.showShareMenuViewInWindow {[weak self] (shreMenuView, platformType) in
            let messageObject:UMSocialMessageObject = UMSocialMessageObject.init()
            messageObject.text = "友盟分享测试(网页),swift3.0 Xcode8.1 umeng6.0.3 作者:targetcloud"
            let shareObject:UMShareWebpageObject = UMShareWebpageObject.init()
            shareObject.title = "网页分享"
            shareObject.descr = "这里是网页分享测试,作者:targetcloud"
            shareObject.thumbImage = UIImage.init(named: "icon")
            shareObject.webpageUrl = "http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html"
            messageObject.shareObject = shareObject;
            UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: self, completion: { (shareResponse, error) in
                if error != nil {
                    print("Share Fail with error :%@", error)
                }else{
                    self?.getUserInfoForPlatform(platformType: platformType)
                    print("Share succeed")
                }
            })
        }
    }
    
    func shareAction_txt (){
        UMSocialUIManager.showShareMenuViewInWindow {[weak self] (shareSelectionView:UMShareMenuSelectionView?, platformType:UMSocialPlatformType) in
            let messageObject : UMSocialMessageObject = UMSocialMessageObject.init()
            messageObject.text = "友盟分享测试(文本),swift3.0 Xcode8.1 umeng6.0.3 作者:targetcloud"
            UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: self, completion: { (data:Any?, error:Error?) in
                if error != nil {
                    print("************Share fail with error *********",error)
                }else{
                    UMSocialManager.default().getUserInfo(with: platformType, currentViewController: self, completion: { (result:Any?, error:Error?) in
                        if let userinfo  = result as? UMSocialUserInfoResponse{
                            let message = " name: \(userinfo.name)\n icon: \(userinfo.iconurl)\n gender: \(userinfo.gender)\n"
                            print(message)
                        }
                    })
                    if let resp:UMSocialShareResponse = data as? UMSocialShareResponse  {
                        print("response message is ",resp.message ?? "")
                        print("response originalResponse data is ",resp.originalResponse ?? "")
                    }
                    print("response data is ",data)
                }
                self?.alertWithError(error: error)
            })
        }
    }
    
    func shareAction_music (){
        UMSocialUIManager.showShareMenuViewInWindow {[weak self] (shareSelectionView:UMShareMenuSelectionView?, platformType:UMSocialPlatformType) in
            let messageObject : UMSocialMessageObject = UMSocialMessageObject.init()
            messageObject.text = "友盟分享测试(音乐),swift3.0 Xcode8.1 umeng6.0.3 作者:targetcloud"
            let shareObject:UMShareMusicObject = UMShareMusicObject.init()
            shareObject.title = "音乐分享"
            shareObject.descr = "这里是音乐分享测试,作者:targetcloud"
            shareObject.thumbImage = UIImage.init(named: "icon")
            shareObject.musicUrl = "http://c.y.qq.com/v8/playsong.html?songid=108782194&source=yqq#wechat_redirect"
            messageObject.shareObject = shareObject;
            UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: self, completion: { (data:Any?, error:Error?) in
                if error != nil {
                    print("************Share fail with error *********",error)
                }else{
                    if let resp:UMSocialShareResponse = data as? UMSocialShareResponse  {
                        print("response message is ",resp.message ?? "")
                        print("response originalResponse data is ",resp.originalResponse ?? "")
                    }
                    print("response data is ",data)
                }
                self?.alertWithError(error: error)
            })
        }
    }
    
    func shareAction_video (){
        UMSocialUIManager.showShareMenuViewInWindow {[weak self] (shareSelectionView:UMShareMenuSelectionView?, platformType:UMSocialPlatformType) in
            let messageObject : UMSocialMessageObject = UMSocialMessageObject.init()
            messageObject.text = "友盟分享测试(视频),swift3.0 Xcode8.1 umeng6.0.3 作者:targetcloud"
            let shareObject:UMShareVideoObject = UMShareVideoObject.init()
            shareObject.title = "视频分享"
            shareObject.descr = "这里是视频分享测试,作者:targetcloud"
            shareObject.thumbImage = UIImage.init(named: "icon")
            shareObject.videoUrl = "http://v.youku.com/v_show/id_XMTgyMjI1MjQxNg==.html?f=18252872#paction"
            messageObject.shareObject = shareObject;
            UMSocialManager.default().share(to: platformType, messageObject: messageObject, currentViewController: self, completion: { (data:Any?, error:Error?) in
                if error != nil {
                    print("************Share fail with error *********",error)
                }else{
                    if let resp:UMSocialShareResponse = data as? UMSocialShareResponse  {
                        print("response message is ",resp.message ?? "")
                        print("response originalResponse data is ",resp.originalResponse ?? "")
                    }
                    print("response data is ",data)
                }
                self?.alertWithError(error: error)
            })
        }
    }
    
    func alertWithError(error:Error?){
        var result = ""
        if (error == nil) {
            result = "恭喜你,分享成功!邀请好友一起玩吧!"
        }else{
            if(error != nil){
                result = "分享失败,此功能正在努力开通中..."
            }else{
                result = "额,分享失败了!"
            }
        }
        let alert = UIAlertView.init(title: "越分享,越快乐!", message: result, delegate: nil, cancelButtonTitle: NSLocalizedString("确定", comment: "确定"))
        alert.show()
    }
    
    func getUserInfoForPlatform(platformType:UMSocialPlatformType){
        UMSocialManager.default().getUserInfo(with: platformType, currentViewController: self, completion: { (result:Any?, error:Error?) in
            if let userinfo  = result as? UMSocialUserInfoResponse {
                let message = " name: \(userinfo.name)\n icon: \(userinfo.iconurl)\n gender: \(userinfo.gender)\n"
                print(message)
                let alert = UIAlertView.init(title: "UserInfo", message: message, delegate: nil, cancelButtonTitle: NSLocalizedString("确定", comment: "确定"))
                alert.show()
            }
        })
    }

}




集成步骤不再重复,请按照(一)是肯定可以成功集成的

运行效果如下,图片(共20张):先是按友盟的账号进行微信聊天(1234 微信朋友圈(567)收藏(89)三个功能测试的,

接着测试了QQ101112 QQ 空间(1314),接着换为自己的账号进行微博测试(1516),微信(171819)、微信朋友圈(20






















附在线进制转换

http://tool.oschina.net/hexconvert/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值