movii / WechatShareDemo

Share to Wechat with Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

WechatShareDemo

Installation

Step 1

微信开放平台注册应用程序ID,获得AppID。

微信对新建的应用需要审核,审核需要7天,所以这里借用了WeDemo的AppID

Step 2

  1. 添加URL schemes为AppID,这样微信可以回调起你的app。

  2. 在info.plist中添加白名单,否则在没有安装微信的环境(比如模拟器)中会报错 -canOpenURL: failed for URL

Step 2

  1. 通过CocoaPods集成微信SDK

    pod 'WechatOpenSDK'
    
  2. 添加Objective-C Bridging Header

    确认在Building setting - Swift Compiler - Code Generation 中添加了bridging header的路径。比如"“YourApp/YourApp-Bridging-Header.h”

  3. 在YourApp-Bridging-Header.h中添加下面的代码

    #import “WXApi.h”

    然后不用import,就可以在Swift中使用相关方法了。

Usage

  1. 向微信终端程序注册第三方应用

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {        
            WXApi.registerApp("APP-ID")
            return true
    }
  2. 发送请求到微信

    过程:创建多媒体消息(WXMediaMessage)或者富文本(String)消息,然后创建SendMessageToWXReq请求,最后通过WXApi.send()方法向微信发起请求。

  3. 处理微信的回应

    实现onResp协议方法

Demo

// 分享文字
ShareManager.shared.sendText(text, inScene: WXSceneSession)
// 分享图片
ShareManager.shared.sendImage(imageData, inScene: WXSceneTimeline)
// 分享网页链接
let url = "https://httpbin.org/get"
ShareManager.shared.sendLink(url, text, inScene: WXSceneSession)	

About

Share to Wechat with Swift


Languages

Language:Swift 95.4%Language:Ruby 2.9%Language:Objective-C 1.7%