(2)登陸後創建一個iOS應用,我們會得到一個APP ID,這個後面開發的時候要用到(應用沒有提交審核沒關系,不影響我們的測試使用)。
//TencentOpenapi
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiinterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
#import <TencentOpenAPI/sdkdef.h>
#import <TencentOpenAPI/TencentmessageObject.h>
#import <TencentOpenAPI/TencentOAuthObject.h>
(7)重寫 AppDelegate.swift 的兩個方法
//重寫openURL
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject])
-> Bool {
return TencentOAuth.HandleOpenURL(url)
}
//重寫handleOpenURL
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
return TencentOAuth.HandleOpenURL(url)
}
(8)iOS 9系統策略更新,限制了http協議的訪問。在“Info.plist”裡增加如下代碼:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
3,初始化TencentOAuth
我們在發送消息前還要創建iOS SDK API數據對象 TencentOAuth 並初始化其 appid,本樣例delegate可以設為空。
import UIKit
class ViewController: UIViewController{
var _tencentOAuth:TencentOAuth!
override func viewDidLoad() {
super.viewDidLoad()
_tencentOAuth = TencentOAuth.init(appId: "1105212016", andDelegate: nil)
}
@IBAction func sendMessage(sender: AnyObject) {
//消息分享相關代碼
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
let txtObj = QQApiTextObject(text: "歡迎訪問 111cn.net ")
let req = SendMessageToQQReq(content: txtObj)
QQApiInterface.sendReq(req)
2)純圖片分享:
let filePath = NSBundle.mainBundle().pathForResource("logo", ofType: "png")
let imgData = NSData(contentsOfFile:filePath!)
let imgObj = QQApiImageObject(data: imgData, previewImageData: imgData,
title: "111cn.net ", description: "航歌 - 做最好的開發者知識平台")
let req = SendMessageToQQReq(content: imgObj)
QQApiInterface.sendReq(req)
3)新聞分享:
let newsUrl = NSURL(string: "http://www.111cn.net ")
let title = "新聞標題"
let description = "新聞描述"
let previewImageUrl = NSURL(string: "http://www.111cn.net /blog/images/logo.png")
let newsObj = QQApiNewsObject(URL: newsUrl, title: title, description: description,
previewImageURL: previewImageUrl, targetContentType: QQApiURLTargetTypeNews)
let req = SendMessageToQQReq(content: newsObj)
QQApiInterface.sendReq(req)
(4)音樂分享:
let url = NSURL(string:
"http://y.qq.com/i/song.html?songid=432451&source=mobileQQ%23wechat_redirect")
let title = "歌曲名:不要說話"
let descriotion = "專輯名:不想放手歌手名:陳奕迅"
let previewImageUrl = NSURL(string:
"http://imgcache.qq.com/music/photo/mid_album_300/V/E/000J1pJ50cDCVE.jpg")
let audioObj = QQApiAudioObject(URL: url, title: title, description: descriotion,
previewImageURL: previewImageUrl, targetContentType: QQApiURLTargetTypeAudio)
let req = SendMessageToQQReq(content: audioObj)
QQApiInterface.sendReq(req)
(5)視頻分享:
let url = NSURL(string: "http://v.qq.com/cover/5/53x6bbyb07ebl3s/n0013r8esy6.html")
let previewImageUrl = NSURL(string: "http://www.111cn.net /blog/images/logo.png")
let videoObj = QQApiVideoObject(URL: url, title: "黑子的籃球", description: "第一季第一集",
previewImageURL: previewImageUrl, targetContentType: QQApiURLTargetTypeVideo)
let req = SendMessageToQQReq(content: videoObj)
QQApiInterface.sendReq(req)
原文來自:http://www.hangge.com/blog/cache/detail_1070.html