官方鏈接:http://www.umeng.com/social
嘛,為了做分享,建議多去搞幾個如微信、QQ、微博的開發平臺賬號。把那個啥AppId、AppKey/AppSecret之類的搞到手就行。就是審核多,好麻煩的說,沒個幾天下不來。
頭文件
在x-code中是肯定要創建橋接頭文件的,如下之類的,另外的自己找
#import "UMSocial.h"
#import "UMSocialQQHandler.h" //支持QQ
#import "UMSocialWechatHandler.h" //支持微信
#import "UMSocialSinaSSOHandler.h" //支持微博
添加的文件夾
你要分享到那個平臺,就要添加對應的文件,友盟官方可以下載如YiXin(易信)、TencentOpenAPI(騰訊)、Wechat(微信)。手動拖和Cocopodes都行
在x-code的target中的building settings里還要記得添加
$(SRCROOT)/“你的工程名”/Bridge-Header.h(橋接頭文件的名字,我寫的是這個)
x-code的target中的Build Phases里的Link binary with libraries中還要記得添加剛才那些文件夾的底層依賴文件哦,當然用Cocopodes安裝的同學已經自動添加了。不知道有那些依賴文件的去友盟官方網站找。
代碼相關
1.在AppDelegate中的代碼
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
//設置友盟的AppKey,用于標示應用程序
UMSocialData.setAppKey("5779d71f67e58eb2fa001ffe")
//注冊QQ和QQ空間的Appkey和AppId,這兩個參數是在騰訊開放平臺上申請獲得的,(需要在騰訊開放平臺上注冊賬號,填寫相關信息,個人需上傳個人手持身份證正反面照片,公司需上傳公司營業執照,審核時間在7個工作日之內),url指的是分享的url,一般填寫對應應用在AppStore里的鏈接或者公司官網,默認為友盟的官方網址
UMSocialQQHandler.setQQWithAppId("100424468", appKey: "5779d71f67e58eb2fa001ffe", url: "http:www.baidu.com")
//注冊微信和微信朋友圈的AppKey和AppSecret,這兩個參數是在微信開放平臺上申請獲得的(需要在微信開放平臺上注冊賬號,填寫相關信息,無論是個人還是公司,都需要上傳對應應用的appIcon,審核時間大概在3~5個工作日內),url同QQ的
UMSocialWechatHandler.setWXAppId("wxd930ea5d5a258f4f", appSecret: "db426a9829e4b49a0dcac7b4162da6b6", url: nil)
//注意:為了配合蘋果的審核政策,需要對未安裝的客戶端進行隱藏,主要針對財大氣粗的QQ和微信
UMSocialConfig.hiddenNotInstallPlatforms([UMShareToQQ,UMShareToQzone,UMShareToWechatSession,UMShareToWechatTimeline])
return true
}
配置系統回調,特別是和微信支付或者支付寶支付的時候需要區分
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
{
//調用回調
UMSocialSnsService.handleOpenURL(url)
return true
}
2.在ViewController中的代碼
//坑:實現分享時,特別是自定義分享面板時,需要添加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()
//創建自定義分享面板
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()
{
//自定義分享面板的時候需要用postCommentWithContent實現評論內容的發布
//參數一:需要分享的平臺
//參數二:需要分享的內容
//參數三:需要分享的圖片
//參數四:位置,一般不作處理
//參數五:分享資源
//參數六:作用的控制器
//參數七:分享完成之后的回調
let urlResource = UMSocialUrlResource(snsResourceType: UMSocialUrlResourceTypeImage, url: "http://www.baidu.com/img/bdlogo.gif")
UMSocialDataService.defaultDataService().postSNSWithTypes([UMShareToSina], content: "我是一個自定義樣式分享", image: nil, location: nil, urlResource: urlResource, presentedController: self) { (shareResponse: UMSocialResponseEntity?) -> Void in
if shareResponse?.responseCode == UMSResponseCodeSuccess {
print("分享成功")
} else {
print("分享失敗")
}
}
//在分享的同時隱藏分享面板
UIView.animateWithDuration(0.3) { () -> Void in
self.customView.frame = CGRectMake(0, self.screenHeight, self.screenWidth, 200)
}
}
//點擊屏幕隱藏自定義分享面板
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
UIView.animateWithDuration(0.3) {
self.customView.frame = CGRectMake(0, self.screenHeight, self.screenWidth, 200)
}
}
//默認分享樣式
@IBAction func defaultShareStyle(sender: AnyObject) {
/*
//默認快速實現分享
//參數一:作用的控制器對象
//參數二:appKey
//參數三:要分享的文字
//參數四:需要分享的圖片
//參數五:準備分享到的第三方平臺
//參數六:代理對象
UMSocialSnsService.presentSnsIconSheetView(self, appKey: "5779d71f67e58eb2fa001ffe", shareText: "走過路過千萬不要錯過", shareImage: nil, shareToSnsNames: [UMShareToWechatTimeline,UMShareToWechatSession,UMShareToQzone,UMShareToQQ,UMShareToSina], delegate: nil)
*/
//默認詳細分享
//設置分享標題
UMSocialData.defaultData().extConfig.title = "分享標題"
//設置分享url,以qq舉例
UMSocialData.defaultData().extConfig.qqData.url = "http://www.baidu.com"
//設置分享的內容
UMSocialData.defaultData().extConfig.qqData.title = "分享到qq的內容"
UMSocialSnsService.presentSnsIconSheetView(self, appKey: "5779d71f67e58eb2fa001ffe", shareText: "走過路過千萬不要錯過", shareImage: nil, shareToSnsNames: [UMShareToWechatTimeline,UMShareToWechatSession,UMShareToQzone,UMShareToQQ,UMShareToSina], delegate: self)
}
//實現回調方法,需要遵守協議UMSocialUIDelegate
func didFinishGetUMSocialDataInViewController(response: UMSocialResponseEntity!)->Void
{
if response.responseCode == UMSResponseCodeSuccess
{
print("分享成功")
}else
{
print("分享失敗")
}
}
3.白名單
也不知道為啥這么搞,反正要弄。
在info.plist文件上點右鍵打開菜單,選open as ->source code,在把官方文檔中的白名單中你需要的內容拷過來就OK了。
哦,還要記得打開plist中的網絡連接喲。
總結
寫了這么多,還得結合著友盟官方文檔來看,要不然還是抓瞎。記得上官網哦。