我正在寻找一种干净的方式来分享我的应用程序使用电报API的自定义贴纸。我一直在搜索电报文档,了解如何通过电报以正确的方式发送自定义贴纸。我在这里找到了如何创建贴纸集-> https://core.telegram.org/bots/api#stickerset,以及如何使贴纸本身成为-> https://core.telegram.org/bots/api#stickerset
这是我准备stickerSet和贴纸的代码
func handleStickersForTelegram(completionHandler: @escaping (Bool) -> Void) {
StickerPackManager.queue.async {
var json: [String: Any] = [:]
json["name"] = self.name
json["title"] = self.publisher
json["contains_masks"] = false
if self.animated == true {
json["is_animated"] = true
}
var stickersArray: [[String: Any]] = []
for sticker in 1...self.stickers.count {
var stickerDict: [String: Any] = [:]
print("stickerTest", self.stickers.count )
stickerDict["file_id"] = "\(StickerBaseURL)/\(self.identifier)/\(sticker).webp"
stickerDict["set_name"] = self.name
stickerDict["file_unique_id"] = "\(self.identifier)/\(sticker).webp"
stickerDict["width"] = 96
stickerDict["height"] = 96
if self.animated == true {
stickerDict["is_animated"] = true
}
stickersArray.append(stickerDict)
}
json["stickers"] = stickersArray
let result = Interoperability.sendToTelegram(json: json)
DispatchQueue.main.async {
completionHandler(result)
}
}
}在调用Interoperability.sendToTelegram(json:json)之后,我试着把这个函数变成分享自定义贴纸到Whatsapp的方法,但是没有找到合适的信息,这是我的whatsapp分享函数。
struct Interoperability {
private static let DefaultBundleIdentifier: String = "MyBundleIdentifier"
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 func send(json: [String: Any]) -> Bool {
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
}
if #available(iOS 10.0, *) {
pasteboard.setItems([[PasteboardStickerPackDataType: dataToSend]], options: [UIPasteboard.OptionsKey.localOnly: true, UIPasteboard.OptionsKey.expirationDate: NSDate(timeIntervalSinceNow: PasteboardExpirationSeconds)])
} else {
pasteboard.addItems([[PasteboardStickerPackDataType: dataToSend]])
}
DispatchQueue.main.async {
if UIApplication.shared.canOpenURL(URL(string: "whatsapp://")!) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(WhatsAppURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(WhatsAppURL)
}
}
}
return true
}
}这是我用来制作电报的方法,但不起作用!我已经在上面的struct中添加了内容。
struct Interoperability {
private static let stickerSetDataType: String = "stickers.createStickerSet"
private static let testTelegram: URL = URL(string: "tg://StickerSet")!
static func sendToTelegram(json: [String: Any]) -> Bool {
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
guard let dataToSend = try? JSONSerialization.data(withJSONObject: json, options: []) else {
return false
}
pasteboard.addItems([[stickerSetDataType: dataToSend]])
DispatchQueue.main.async {
if UIApplication.shared.canOpenURL(URL(string: "tg://")!) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(testTelegramURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(testTelegramURL)
}
}
}
return true
}
}它打开了电报,但没有任何反应,没有贴纸,也没有任何迹象表明电报读取了我的贴纸。我不知道是我用错误的方式保存了UIPasteboard中的贴纸数据,还是这些信息是错误的。
private static let stickerSetDataType: String = "stickers.createStickerSet"
private static let testTelegram: URL = URL(string: "tg://StickerSet")!发布于 2021-03-02 19:04:53
Telegram应用程序不像whatsapp那样工作。您需要通过以下链接将您的贴纸包裹添加到电报上
https://www.makeuseof.com/tag/how-to-make-telegram-stickers/
一旦你把你的包裹添加到电报上,你就可以使用你的包裹名称,通过使用以下代码在电报上添加贴纸包裹
if let url = URL(string: "https://telegram.me/addstickers/ClassicAlice") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}https://stackoverflow.com/questions/66163986
复制相似问题