To realize the sharing of information to QQ space or send messages to friends, we can use a Third-party SDK (such as: Friends, SHARESDK). You can also use the SDK provided by the Tencent official. This article describes the configuration and use of the latter.
1, Tencent Open Platform registration
(1) First, we should register an account on the Tencent Open Platform (http://open.qq.com).
(2) After landing to create an iOS application, we will get an app ID, which will be used later development (the application does not submit audit does not affect our testing use).
2,sdk Download and Configuration
(1) Download ios_sdk to local, download address: Http://wiki.open.qq.com/wiki/mobile/SDK Download
( 2 Add TencentOpenApi_IOS_Bundle.bundle and TencentOpenAPI.framework in the compressed package to the project
(3) in build Phases -> link Binary with libraries , click the plus sign to add the following dependency library to the project: LIBZ.TBD, LIBSTDC++.TBD, Coretelephony.framework, Libsqlite3.tbd, Coregraphics.framework, Systemconfiguration.framework, LIBICONV.TBD, Security.framework.
(4) in the build Settings -> linking configuration area, add to the "other Linker Flags" configuration item The property value "-fobjc-arc"
(5) Adds a URL schemes to the "info"-> "url Types". The new schemes name is: Tencent + AppID
(6) Manually create the bridge header file bridge.h to contain the objective-that need to be referenced C header file, and set into the compilation parameters. The header file contains the following
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) Two methods of rewriting Appdelegate.swift
Rewrite OpenURL
Func application (app:uiapplication, OpenURL url:nsurl, Options: [String:anyobject])
-> Bool {
return Tencentoauth.handleopenurl (URL)
}
Rewrite Handleopenurl
Func application (application:uiapplication, Handleopenurl url:nsurl)-> Bool {
return Tencentoauth.handleopenurl (URL)
}
(8) IOS 9 System Policy updates that restrict access to HTTP protocols. Add the following code in "Info.plist":
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
3, Initialize Tencentoauth
We also create the iOS SDK API data Object Tencentoauth and initialize its AppID before sending the message, and this example delegate can be set to null.
Import Uikit
class viewcontroller:uiviewcontroller{
& nbsp; var _tencentoauth:tencentoauth!
override func Viewdidload () {
super.viewdidload ()
_tencentoauth = Tencentoauth.init (appId: "1105212016", Anddelegate:nil)
}
@IBAction func SendMessage (sender:anyobject) {
//message sharing related code
}
override func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
4, to share QQ friends and QQ space
IOS SDK Support in the user installed mobile phone QQ, will jump to mobile phone QQ to share (we can choose to share to QQ friends or QQ space and other places), otherwise call the browser page to share.
After sharing will be prompted, continue to stay in QQ or return to the original app.
(1) Plain text sharing:
Let Txtobj = qqapitextobject (text: "Welcome to 111cn.net")
Let req = Sendmessagetoqqreq (content:txtobj)
Qqapiinterface.sendreq (req)
2) Pure picture sharing:
Let FilePath = Nsbundle.mainbundle (). Pathforresource ("logo", OfType: "PNG")
Let Imgdata = NSData (contentsoffile:filepath!)
Let imgobj = Qqapiimageobject (Data:imgdata, Previewimagedata:imgdata,
Title: "111cn.net", Description: "Sailing song-do the best developer Knowledge Platform")
Let req = Sendmessagetoqqreq (content:imgobj)
Qqapiinterface.sendreq (req)
3) News Sharing:
Let Newsurl = Nsurl (string: "Http://www.111cn.net")
Let title = "News Headlines"
Let description = "News 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) Music sharing:
Let URL = Nsurl (string:
"Http://y.qq.com/i/song.html?songid=432451&source=mobileQQ%23wechat_redirect")
Let title = "Song name: Don't Talk"
Let Descriotion = "album Name: Don't want to give up the singer name: Eason Chan"
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) video sharing:
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:" Sunspot Basketball ", Description:" First season first episode ",
& Nbsp;previewimageurl:previewimageurl, Targetcontenttype:qqapiurltargettypevideo)
Let req = SendMessageToQQReq ( Content:videoobj)
Qqapiinterface.sendreq (req)
Original from: http://www.hangge.com/blog/cache/detail_1070.html