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里加入桥接配置:


ios10分享到微信(swift版本)_第1张图片
Paste_Image.png

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

-Objc
-all_load
ios10分享到微信(swift版本)_第2张图片
Paste_Image.png
  1. 在TARGETS - General里导入库:


    ios10分享到微信(swift版本)_第3张图片
    Paste_Image.png
  2. 在Info -> URL Types里加入Identifier(值为weixin)和URL Schemes(值为微信开放平台里生成的AppID)


    ios10分享到微信(swift版本)_第4张图片
    Paste_Image.png
  3. 将下列内容加入Info.plist(放在倒数第2行上面)
LSApplicationQueriesSchemes

weixin

NSAppTransportSecurity

NSAllowsArbitraryLoads


  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 {
}


  • 参考--Mandarava的文章

你可能感兴趣的:(ios10分享到微信(swift版本))