兩種實現(xiàn)方法?
? ? ?1.集成新浪SDK實現(xiàn)如果你的app需要支持新浪微博登錄等其他功能,建議使用方法1
? ? ?2.使用ios系統(tǒng)原生的分享方式,比較簡單,但是需要在iphone設(shè)置里添加賬號密碼,有時候不好添加
第一種 方式:
前提:在新浪微博開放平臺,注冊app獲取到appid和serect
注冊的時候授權(quán)回調(diào)頁: ios最好填寫默認的https://api.weibo.com/oauth2/default.html
1.進入官網(wǎng)鏈接到github下載SDK
2.打開SDK文檔參考
3.libWebSDK整個文件拖入到工程中
4.Other-Linker-flags 里邊設(shè)置-Objc
5.添加需要支持的庫
QuartzCore.framework ? ? ?ImageIO.framework ? ??SystemConfiguration.framework
Security.framework ? ?CoreTelephony.framework ? ?CoreText.framework ? CoreGraphics.framework ? ?libz.dylib ? ? libsqlite3.dylib
6.在info里邊配置白名單 還有http設(shè)置 ? ATS ?為yes ?
7.配置urlScheme 填寫wb+”appKey”
8.設(shè)置橋接文件 ?導(dǎo)入頭文件
#import "WeiboSDK.h"
9.上代碼了
一般分享最多3個 ?類型 ?文本 ?圖片 ?鏈接
大概的步驟如下: 但是具體的判斷大家自己做一下
在app代理里邊
注冊
WeiboSDK.registerApp(appkey)
設(shè)置weibo代理
func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
return WeiboSDK.handleOpen(url, delegate: self)
}
遵守代理協(xié)議
class AppDelegate: UIResponder, UIApplicationDelegate, WeiboSDKDelegate { ?}
實現(xiàn)代理方法
func didReceiveWeiboRequest(_ request: WBBaseRequest!) { ? }
func didReceiveWeiboResponse(_ response: WBBaseResponse!) { ?}
封裝一個類專門來寫分享的代碼
//判斷是否安裝 ?沒有安裝就提醒安裝
WeiboSDK.isWeiboAppInstalled()
//整個消息類
letmsg =WBMessageObject()
//文本類型
msg.text=""
//圖片類型
letimg =WBImageObject()
//大小不能超過10M
img.imageData=UIImageJPEGRepresentation(UIImage(named:"QQ")!,1)
//網(wǎng)頁
letweb =WBWebpageObject()
//不能為空且長度不能超過255
web.webpageUrl=""
//不能為空長度小于255,可與url一樣,只要是唯一就可以了
web.objectID=""
web.description=""
web.title=""
//大小小于32k
web.thumbnailData=UIImageJPEGRepresentation(UIImage(named:"QQ")!,1)
msg.imageObject= img
msg.mediaObject= web
//請求類
if let req =WBSendMessageToWeiboRequest.request(withMessage: msg)as?WBSendMessageToWeiboRequest{
//發(fā)送請求
WeiboSDK.send(req)
}
第二種 方式:
//判斷是否安裝了新浪并綁定了賬號密碼
if !SLComposeViewController.isAvailable(forServiceType: SLServiceTypeSinaWeibo) { return ? ?}
//創(chuàng)建控制器
guard let wbshare = SLComposeViewController(forServiceType: SLServiceTypeSinaWeibo) else { return }
//正文不可以大于120字
var txt = text
if txt.count > 115 {
let endIdx = text.endIndex
let inx = text.index(endIdx, offsetBy:115 - text.count)
txt = text.substring(to: inx) + "..."
}
//設(shè)置正文
wbshare.setInitialText(txt)
//設(shè)置圖片
wbshare.add(img)
//設(shè)置url
if let url = URL(string: urlStr) {
wbshare.add(url)
}
//moda分享
present(wbshare, animated: true, completion: nil)
//監(jiān)聽結(jié)果
wbshare.completionHandler = { result in
if result == .cancelled {
print("取消分享")
}else if result == .done {
print("分享成功")
}
}