官方鏈接:http://www.umeng.com/social
嘛旅赢,為了做分享齿桃,建議多去搞幾個(gè)如微信、QQ煮盼、微博的開(kāi)發(fā)平臺(tái)賬號(hào)短纵。把那個(gè)啥AppId、AppKey/AppSecret之類的搞到手就行僵控。就是審核多香到,好麻煩的說(shuō),沒(méi)個(gè)幾天下不來(lái)。
頭文件
在x-code中是肯定要?jiǎng)?chuàng)建橋接頭文件的养渴,如下之類的雷绢,另外的自己找
#import "UMSocial.h"
#import "UMSocialQQHandler.h" //支持QQ
#import "UMSocialWechatHandler.h" //支持微信
#import "UMSocialSinaSSOHandler.h" //支持微博
添加的文件夾
你要分享到那個(gè)平臺(tái),就要添加對(duì)應(yīng)的文件理卑,友盟官方可以下載如YiXin(易信)翘紊、TencentOpenAPI(騰訊)、Wechat(微信)藐唠。手動(dòng)拖和Cocopodes都行
在x-code的target中的building settings里還要記得添加
$(SRCROOT)/“你的工程名”/Bridge-Header.h(橋接頭文件的名字帆疟,我寫的是這個(gè))
x-code的target中的Build Phases里的Link binary with libraries中還要記得添加剛才那些文件夾的底層依賴文件哦,當(dāng)然用Cocopodes安裝的同學(xué)已經(jīng)自動(dòng)添加了宇立。不知道有那些依賴文件的去友盟官方網(wǎng)站找踪宠。
代碼相關(guān)
1.在AppDelegate中的代碼
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
//設(shè)置友盟的AppKey,用于標(biāo)示應(yīng)用程序
UMSocialData.setAppKey("5779d71f67e58eb2fa001ffe")
//注冊(cè)QQ和QQ空間的Appkey和AppId,這兩個(gè)參數(shù)是在騰訊開(kāi)放平臺(tái)上申請(qǐng)獲得的,(需要在騰訊開(kāi)放平臺(tái)上注冊(cè)賬號(hào)妈嘹,填寫相關(guān)信息柳琢,個(gè)人需上傳個(gè)人手持身份證正反面照片,公司需上傳公司營(yíng)業(yè)執(zhí)照润脸,審核時(shí)間在7個(gè)工作日之內(nèi)),url指的是分享的url柬脸,一般填寫對(duì)應(yīng)應(yīng)用在AppStore里的鏈接或者公司官網(wǎng),默認(rèn)為友盟的官方網(wǎng)址
UMSocialQQHandler.setQQWithAppId("100424468", appKey: "5779d71f67e58eb2fa001ffe", url: "http:www.baidu.com")
//注冊(cè)微信和微信朋友圈的AppKey和AppSecret毙驯,這兩個(gè)參數(shù)是在微信開(kāi)放平臺(tái)上申請(qǐng)獲得的(需要在微信開(kāi)放平臺(tái)上注冊(cè)賬號(hào)倒堕,填寫相關(guān)信息,無(wú)論是個(gè)人還是公司爆价,都需要上傳對(duì)應(yīng)應(yīng)用的appIcon垦巴,審核時(shí)間大概在3~5個(gè)工作日內(nèi)),url同QQ的
UMSocialWechatHandler.setWXAppId("wxd930ea5d5a258f4f", appSecret: "db426a9829e4b49a0dcac7b4162da6b6", url: nil)
//注意:為了配合蘋果的審核政策铭段,需要對(duì)未安裝的客戶端進(jìn)行隱藏,主要針對(duì)財(cái)大氣粗的QQ和微信
UMSocialConfig.hiddenNotInstallPlatforms([UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline])
return true
}
配置系統(tǒng)回調(diào)骤宣,特別是和微信支付或者支付寶支付的時(shí)候需要區(qū)分
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
//調(diào)用回調(diào)
UMSocialSnsService.handleOpenURL(url)
return true
}
2.在ViewController中的代碼
//坑:實(shí)現(xiàn)分享時(shí),特別是自定義分享面板時(shí)稠项,需要添加CoreLocation框架
import CoreLocation
class ViewController: UIViewController,UMSocialUIDelegate
{
//屏幕寬度
let screenWidth = UIScreen.mainScreen().bounds.size.width
//屏幕高度
let screenHeight = UIScreen.mainScreen().bounds.size.height
//自定義分享面板
var customView = UIView()
override func viewDidLoad()
{
super.viewDidLoad()
//創(chuàng)建自定義分享面板
self.customView = UIView(frame: CGRectMake(0,screenHeight,screenWidth,200))
self.customView.backgroundColor = UIColor.lightGrayColor()
self.view.addSubview(self.customView)
//新浪
let sinaButton = UIButton(type: UIButtonType.Custom)
sinaButton.frame = CGRectMake(10, 20, 40, 40)
sinaButton.setImage(UIImage(named: "sina"), forState: UIControlState.Normal)
sinaButton.addTarget(self, action: "shareToSina", forControlEvents: UIControlEvents.TouchUpInside)
self.customView.addSubview(sinaButton)
}
@IBAction func customShareStyle(sender: AnyObject)
{
//顯示自定義分享面板
UIView.animateWithDuration(0.3) { () -> Void in
self.customView.frame = CGRectMake(0, self.screenHeight - 200, self.screenWidth, 200)
}
}
func shareToSina()
{
//自定義分享面板的時(shí)候需要用postCommentWithContent實(shí)現(xiàn)評(píng)論內(nèi)容的發(fā)布
//參數(shù)一:需要分享的平臺(tái)
//參數(shù)二:需要分享的內(nèi)容
//參數(shù)三:需要分享的圖片
//參數(shù)四:位置涯雅,一般不作處理
//參數(shù)五:分享資源
//參數(shù)六:作用的控制器
//參數(shù)七:分享完成之后的回調(diào)
let urlResource = UMSocialUrlResource(snsResourceType: UMSocialUrlResourceTypeImage, url: "http://www.baidu.com/img/bdlogo.gif")
UMSocialDataService.defaultDataService().postSNSWithTypes([UMShareToSina], content: "我是一個(gè)自定義樣式分享", image: nil, location: nil, urlResource: urlResource, presentedController: self) { (shareResponse: UMSocialResponseEntity?) -> Void in
if shareResponse?.responseCode == UMSResponseCodeSuccess {
print("分享成功")
} else {
print("分享失敗")
}
}
//在分享的同時(shí)隱藏分享面板
UIView.animateWithDuration(0.3) { () -> Void in
self.customView.frame = CGRectMake(0, self.screenHeight, self.screenWidth, 200)
}
}
//點(diǎn)擊屏幕隱藏自定義分享面板
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
UIView.animateWithDuration(0.3) {
self.customView.frame = CGRectMake(0, self.screenHeight, self.screenWidth, 200)
}
}
//默認(rèn)分享樣式
@IBAction func defaultShareStyle(sender: AnyObject) {
/*
//默認(rèn)快速實(shí)現(xiàn)分享
//參數(shù)一:作用的控制器對(duì)象
//參數(shù)二:appKey
//參數(shù)三:要分享的文字
//參數(shù)四:需要分享的圖片
//參數(shù)五:準(zhǔn)備分享到的第三方平臺(tái)
//參數(shù)六:代理對(duì)象
UMSocialSnsService.presentSnsIconSheetView(self, appKey: "5779d71f67e58eb2fa001ffe", shareText: "走過(guò)路過(guò)千萬(wàn)不要錯(cuò)過(guò)", shareImage: nil, shareToSnsNames: [UMShareToWechatTimeline,UMShareToWechatSession,UMShareToQzone,UMShareToQQ,UMShareToSina], delegate: nil)
*/
//默認(rèn)詳細(xì)分享
//設(shè)置分享標(biāo)題
UMSocialData.defaultData().extConfig.title = "分享標(biāo)題"
//設(shè)置分享url,以qq舉例
UMSocialData.defaultData().extConfig.qqData.url = "http://www.baidu.com"
//設(shè)置分享的內(nèi)容
UMSocialData.defaultData().extConfig.qqData.title = "分享到qq的內(nèi)容"
UMSocialSnsService.presentSnsIconSheetView(self, appKey: "5779d71f67e58eb2fa001ffe", shareText: "走過(guò)路過(guò)千萬(wàn)不要錯(cuò)過(guò)", shareImage: nil, shareToSnsNames: [UMShareToWechatTimeline,UMShareToWechatSession,UMShareToQzone,UMShareToQQ,UMShareToSina], delegate: self)
}
//實(shí)現(xiàn)回調(diào)方法,需要遵守協(xié)議UMSocialUIDelegate
func didFinishGetUMSocialDataInViewController(response: UMSocialResponseEntity!)->Void
{
if response.responseCode == UMSResponseCodeSuccess
{
print("分享成功")
}else
{
print("分享失敗")
}
}
3.白名單
也不知道為啥這么搞展运,反正要弄活逆。
在info.plist文件上點(diǎn)右鍵打開(kāi)菜單,選open as ->source code拗胜,在把官方文檔中的白名單中你需要的內(nèi)容拷過(guò)來(lái)就OK了蔗候。
哦,還要記得打開(kāi)plist中的網(wǎng)絡(luò)連接喲埂软。
總結(jié)
寫了這么多锈遥,還得結(jié)合著友盟官方文檔來(lái)看纫事,要不然還是抓瞎。記得上官網(wǎng)哦所灸。