寫(xiě)在前面
雖然在國(guó)內(nèi)用whatsapp 的人不多, 但在香港等地方大部分還是用whatsapp,這一章我們來(lái)討論討論怎么添加表情到whatsapp, 也可以看whatsapp 的Guide
它里面主要介紹的是利用它的lib來(lái)集成,有現(xiàn)成的案例,這里就不多說(shuō)了.
我們主要談?wù)撓略趺蠢眉羟邪鍋?lái)添加,也就是第二種方法. 當(dāng)然這添加的表情也是來(lái)自本地的,如果需要從server 獲取也可以,但相對(duì)來(lái)說(shuō)會(huì)麻煩一點(diǎn),但確實(shí)是可以的.
- 圖片的格式,大小等,請(qǐng)看guide, 本文只討論發(fā)送到whatsapp
開(kāi)始
- 在Info.plist中添加,
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
我們都知道LSApplicationQueriesSchemes的作用是為了雙方測(cè)試.加這個(gè)可以判斷你的手機(jī)是否安裝了whatsapp
判斷安裝,如果沒(méi)有安裝whatsapp return false;
func canSend() -> Bool {
return UIApplication.shared.canOpenURL(URL(string: "whatsapp://")!)
}
使用下面描述的結(jié)構(gòu)將貼紙數(shù)據(jù)格式化為JSON對(duì)象,
{
"ios_app_store_link" : "String",
"android_play_store_link" : "String",
"identifier" : "String",
"name" : "String",
"publisher" : "String",
"tray_image" : "String", (Base64 representation of the PNG, not WebP, data of the tray image)
"stickers" : [
{
"image_data" : "String", (Base64 representation of the WebP, not PNG, data of the sticker image)
"emojis" : ["String", "String"] (Array of emoji strings. Maximum of 3 emoji)
}
]
}
- 重要
tray_image使用PNG,而image_data使用WebP, 再轉(zhuǎn)成data string 的形式
一次只能發(fā)送一個(gè)貼紙包
- 步驟
我們需要先將數(shù)據(jù)復(fù)制到Pasteboard
然后再打開(kāi)whatsapp://stickerPack, 它會(huì)跳到whatsapp ,之后whatsapp會(huì)自己從Pasteboard中獲取sticker
代碼
import UIKit
struct Interoperability {
// whatsapp guide 中說(shuō)不要包含這個(gè)Id.
private static let DefaultBundleIdentifier: String = "WA.WAStickersThirdParty"
private static let PasteboardExpirationSeconds: TimeInterval = 60
// 請(qǐng)保持這個(gè).
private static let PasteboardStickerPackDataType: String = "net.whatsapp.third-party.sticker-pack"
private static let WhatsAppURL: URL = URL(string: "whatsapp://stickerPack")!
static var iOSAppStoreLink: String = "https://itunes.apple.com....";
static var AndroidStoreLink: String = "https://play.google.com/....";
static func canSend() -> Bool {
return UIApplication.shared.canOpenURL(URL(string: "whatsapp://")!)
}
// 這個(gè)json 的格式就是上面的格式, 有一點(diǎn)值得說(shuō)的是:tray_image / image_data 需要轉(zhuǎn)成data string 來(lái)存儲(chǔ)
// 就是要把你的image 轉(zhuǎn)化成data,再轉(zhuǎn)換成String.
static func send(json: [String: Any]) -> Bool {
// 判斷id 是否合法
if let bundleIdentifier = Bundle.main.bundleIdentifier {
if bundleIdentifier.contains(DefaultBundleIdentifier) {
fatalError("Your bundle identifier must not include the default one.");
}
}
let pasteboard: UIPasteboard = UIPasteboard.general
var jsonWithAppStoreLink: [String: Any] = json
jsonWithAppStoreLink["ios_app_store_link"] = iOSAppStoreLink
jsonWithAppStoreLink["android_play_store_link"] = AndroidStoreLink
guard let dataToSend = try? JSONSerialization.data(withJSONObject: jsonWithAppStoreLink, options: []) else {
return false
}
// 從iOS 10 開(kāi)始Pasteboard,有新的api
if #available(iOS 10.0, *) {
pasteboard.setItems([[PasteboardStickerPackDataType: dataToSend]], options: [UIPasteboardOption.localOnly: true, UIPasteboardOption.expirationDate: NSDate(timeIntervalSinceNow: PasteboardExpirationSeconds)])
} else {
pasteboard.setData(dataToSend, forPasteboardType: PasteboardStickerPackDataType)
}
DispatchQueue.main.async {
if canSend() {
if #available(iOS 10.0, *) {
UIApplication.shared.open(WhatsAppURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(WhatsAppURL)
}
}
}
return true
}
}
從server 來(lái)
- 如果表情是根據(jù)api get 獲得. 一般表情包很小的, 可以讓server 把表情包轉(zhuǎn)換成data string , 再派過(guò)來(lái).以類似上面send 方法中的json 格式. 然后也可以, 這樣的話server要做的事就會(huì)多一點(diǎn).
- 如果server 不想轉(zhuǎn)成data string . 那可以讓server先將表情包zip, call api get 到后, 再unzip. unzip 后自己再轉(zhuǎn)換成data string . 這樣也可以.
-- 如果對(duì)你有幫助的話, 請(qǐng)留個(gè) "喜歡"