ios10分享到微信(swift版本)

  1. 在微信开发者平台为自己的应用提交申请,待审核通过
  2. 在官网下载sdk,下载地址
  3. 将SDK压缩包中的 WechatAuthSDK.h,WXApi.h,WXApiObject.h 三个文件添加到项目中。
  4. 将SDK压缩包中的 libWeChatSDK.a 复制到项目文件夹
  5. 新建桥接文件:WeixinShareTest-Bridging-Header.h,文件内容:
@import UIKit; 
#import "WXApiObject.h"
#import "WXApi.h"

并在Build Settings里加入桥接配置:

Paste_Image.png

.
在targets -> build settings -> Other Linker flag 添加两项:

-Objc
-all_load
Paste_Image.png
  1. 在TARGETS - General里导入库:

    Paste_Image.png
  2. 在Info -> URL Types里加入Identifier(值为weixin)和URL Schemes(值为微信开放平台里生成的AppID)

    Paste_Image.png
  3. 将下列内容加入Info.plist(放在倒数第2行上面)
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
  1. 在AppDelegate实现WXApiDelegate协议:class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate
  2. 在 AppDelegate的application:didFinishLaunchingWithOptions:函数中向微信注册id
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        WXApi.registerApp("wx123456789") //这里的值为微信开放平台里生成的AppID
        return true
    }
  1. 在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)
    }
  1. 实现分享操作:
//inScene可选的值有三个: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)
    }

微信有很多分享方式,可以分享文本、链接、图片、音频、视频、文件等,可以将资源分享到朋友圈、聊天界面、收藏等。具体实现方法都可以在参考微信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 {
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值