ios10分享到微信(swift版本)

  1. 在微信開(kāi)發(fā)者平臺(tái)為自己的應(yīng)用提交申請(qǐng),待審核通過(guò)
  2. 在官網(wǎng)下載sdk,下載地址
  3. 將SDK壓縮包中的 WechatAuthSDK.h,WXApi.h,WXApiObject.h 三個(gè)文件添加到項(xiàng)目中。
  4. 將SDK壓縮包中的 libWeChatSDK.a 復(fù)制到項(xiàng)目文件夾
  5. 新建橋接文件: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
  1. 在TARGETS - General里導(dǎo)入庫(kù):


    Paste_Image.png
  2. 在Info -> URL Types里加入Identifier(值為weixin)和URL Schemes(值為微信開(kāi)放平臺(tái)里生成的AppID)


    Paste_Image.png
  3. 將下列內(nèi)容加入Info.plist(放在倒數(shù)第2行上面)
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
  1. 在AppDelegate實(shí)現(xiàn)WXApiDelegate協(xié)議:
    class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate
  2. 在 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
    }
  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. 實(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 {
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容