- 在微信開(kāi)發(fā)者平臺(tái)為自己的應(yīng)用提交申請(qǐng),待審核通過(guò)
- 在官網(wǎng)下載sdk,下載地址
- 將SDK壓縮包中的 WechatAuthSDK.h,WXApi.h,WXApiObject.h 三個(gè)文件添加到項(xiàng)目中。
- 將SDK壓縮包中的 libWeChatSDK.a 復(fù)制到項(xiàng)目文件夾
- 新建橋接文件:WeixinShareTest-Bridging-Header.h,文件內(nèi)容:
@import UIKit;
#import "WXApiObject.h"
#import "WXApi.h"
并在Build Settings里加入橋接配置:

Paste_Image.png
.
在targets -> build settings -> Other Linker flag 添加兩項(xiàng):
-Objc
-all_load

Paste_Image.png
-
在TARGETS - General里導(dǎo)入庫(kù):
Paste_Image.png -
在Info -> URL Types里加入Identifier(值為weixin)和URL Schemes(值為微信開(kāi)放平臺(tái)里生成的AppID)
Paste_Image.png - 將下列內(nèi)容加入Info.plist(放在倒數(shù)第2行上面)
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
- 在AppDelegate實(shí)現(xiàn)WXApiDelegate協(xié)議:
class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate - 在 AppDelegate的application:didFinishLaunchingWithOptions:函數(shù)中向微信注冊(cè)id
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
WXApi.registerApp("wx123456789") //這里的值為微信開(kāi)放平臺(tái)里生成的AppID
return true
}
- 在AppDelegate里加入:
func application(application: UIApplication, handleOpenURL url: NSURL) -> Bool {
return WXApi.handleOpen(url as URL!, delegate: self)
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return WXApi.handleOpen(url as URL!, delegate: self)
}
- 實(shí)現(xiàn)分享操作:
//inScene可選的值有三個(gè):WXSceneTimeline(朋友圈)、WXSceneSession(聊天界面) 、WXSceneFavorite(收藏)
//分享文本
func sendText(text:String, inScene: WXScene)->Bool{
let req=SendMessageToWXReq()
req.text=text
req.bText=true
req.scene=Int32(inScene.rawValue)
return WXApi.send(req)
}
微信有很多分享方式,可以分享文本、鏈接、圖片、音頻、視頻、文件等,可以將資源分享到朋友圈、聊天界面、收藏等。具體實(shí)現(xiàn)方法都可以在參考微信SDK Demo中的WXApiRequestHandler.m中的方法:
class func sendText(_ text: String, in scene: WXScene) -> Bool{
}
class func sendImageData(_ imageData: Data, tagName: String, messageExt: String, action: String, thumbImage: UIImage, in scene: WXScene) -> Bool {
}
class func sendLinkURL(_ urlString: String, tagName: String, title: String, description: String, thumbImage: UIImage, in scene: WXScene) -> Bool {
}
class func sendMusicURL(_ musicURL: String, dataURL: String, title: String, description: String, thumbImage: UIImage, in scene: WXScene) -> Bool {
}
class func sendVideoURL(_ videoURL: String, title: String, description: String, thumbImage: UIImage, in scene: WXScene) -> Bool {
}
class func sendEmotionData(_ emotionData: Data, thumbImage: UIImage, in scene: WXScene) -> Bool {
}
class func sendFileData(_ fileData: Data, fileExtension extension : String, title: String, description: String, thumbImage: UIImage, in scene: WXScene) -> Bool {
}
class func sendAppContentData(_ data: Data, extInfo info: String, extURL url: String, title: String, description: String, messageExt: String, messageAction action: String, thumbImage: UIImage, in scene: WXScene) -> Bool {
}
class func addCards(toCardPackage cardIds: [Any], cardExts: [Any]) -> Bool {
}
class func sendAuthRequestScope(_ scope: String, state: String, openID: String, in viewController: UIViewController) -> Bool {
}
class func openProfile(withAppID appID: String, description: String, userName: String, extMsg extMessage: String) -> Bool {
}
class func jumpToBizWebview(withAppID appID: String, description: String, tousrname: String, extMsg: String) -> Bool {
}
class func chooseCard(_ appid: String, cardSign: String, nonceStr: String, signType: String, timestamp: UInt32) -> Bool {
}
class func openUrl(_ url: String) -> Bool {
}
class func openHB(withAppid appid: String, package: String, sign: String) -> Bool {
}

