利用剪切板,添加表情到whatsapp

时间:2021-07-15 | 标签: | 作者:Q8 | 来源:Jason_风筝网络

小提示:您能找到这篇{利用剪切板,添加表情到whatsapp}绝对不是偶然,我们能帮您找到潜在客户,解决您的困扰。如果您对本页介绍的利用剪切板,添加表情到whatsapp内容感兴趣,有相关需求意向欢迎拨打我们的服务热线,或留言咨询,我们将第一时间联系您!

< ">

< ">写在前面

< ">虽然在国内用whatsapp的人不多,但在香港等地方大部分还是用whatsapp,这一章我们来讨论讨论怎么添加表情到whatsapp,也可以看whatsapp的Guide

< ">它里面主要介绍的是利用它的lib来集成,有现成的案例,这里就不多说了。

< ">我们主要谈论下怎么利用剪切板来添加,也就是第二种方法。当然这添加的表情也是来自本地的,如果需要从server获取也可以,但相对来说会麻烦一点,但确实是可以的。

< ">图片的格式、大小等,请看guide,本文只讨论发送到whatsapp

< ">开始

< ">在Info.plist中添加,

<key>LSApplicationQueriesSchemes</key>

    <array>

    &游戏公测延期 危机公关nbsp;   <string>whatsapp</string>

    </array>

< ">我们都知道LSApplicationQueriesSchemes的作用是为了双方测试。加这个可以判断你的手机是否安装了whatsapp。

< ">判断安装,如果没有安装whatsapp return false;

func canSend() -> Bool {

        return UIApplication.shared.canOpenURL(URL(string: "whatsapp://")!)

}

< ">使用下面描述的结构将贴纸数据格式化为JSON对象,

< ">

{

  "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,再转成data string的形式

一次只能发送一个贴纸包

< ">步骤

我们需要先将数据复制到Pasteboard

然后再打开whatsapp://stickerPack,它会跳到whatsapp,之后whatsapp会自己从Pasteboard中获取sticker

< ">代码

< ">

import UIKit


struct Interoperability {

    // whatsapp guide 中说不要包含这个Id.

    private static let DefaultBundleIdentifier: String = "WA.WAStickersThirdParty"

    private static let PasteboardExpirationSeconds: TimeInterval = 60

    // 请保持这个.

    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://")!)

    }

    

    // 这个json 的格式就是上面的格式, 有一点值得说的是:tray_image / image_data 需要转成data string 来存储 

    // 就是要把你的image 转化成data,再转换成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 开始Pasteboard,有新的api

        if #available(iOS Argo智能路由10.0, *) {

            pasteboard.setItems([[PasteboardStickerPackDataType: dataToSend]], options: [UIPasteboardOption.localOnly: true, UIPasteboardOption.expirationDate: NSDate(timeIntervalSinceNow: PasteboardExpirationSeconds)])

        } else {

            pasteboard.setData(dataToSend, forPasteboardType: PasteboardStickerPackDataType)

        }

        DispatchQueue.main.async {

        &马拉松nbsp;   if canSend() {

                if #available(iOS 10.0, *) {

                    UIApplication.shared.open(WhatsAppURL, options: [:], completionHandler: nil)

                } else {

                    UIApplication.shared.openURL(WhatsAppURL)

                }

            }

        }

        return true

    }


}

< ">从server来

< ">如果表情是根据api get获得。一般表情包很小的,可以让server把表情包转换成data string,再派过来。以类似上面send方法中的json格式,然后也可以,这样的话server要做的事就会多一点。

< ">如果server不想转成data string,那可以让server先将表情包zip,call api get到后,再unzip.unzip后自己再转换成data string,这样也可以。

利用剪切板,添加表情到whatsapp

上一篇:实时音视频技术选型四大要素
下一篇:Instagram自动快拍创意和格式优化


版权声明:以上主题为“利用剪切板,添加表情到whatsapp"的内容可能是本站网友自行发布,或者来至于网络。如有侵权欢迎联系我们客服QQ处理,谢谢。
相关内容
推荐内容
扫码咨询
    利用剪切板,添加表情到whatsapp
    打开微信扫码或长按识别二维码

小提示:您应该对本页介绍的“利用剪切板,添加表情到whatsapp”相关内容感兴趣,若您有相关需求欢迎拨打我们的服务热线或留言咨询,我们尽快与您联系沟通利用剪切板,添加表情到whatsapp的相关事宜。

关键词:利用剪切板,添加表情到

关于 | 业务 | 案例 | 免责 | 隐私
客服邮箱:sales@1330.com.cn
电话:400-021-1330 | 客服QQ:865612759
沪ICP备12034177号 | 沪公网安备31010702002418号