Files
youle_app_ios_v2/ylgamehall/Source/Share/WechatShare.swift
T
joywayer 68c668a06c 4.B 截图分享补齐 msext 字段:WeChat description / QQ 引导框
【契约影响】docs/H5-Native-Contract.md §3.1[2]type==2 分支

WeChat 截图分享之前丢了 H5 description(msext 通过
sendImageData:MessageExt: 写到 WXMediaMessage.description,接收方在
微信聊天预览里能看到)。链接分享之前完全没传 thumb(msext 用 app
icon)。统一补齐:

- WeChatManager.ShareImage 加 messageExt / mediaTagName 字段;
  shareImage 内 message.description = messageExt + message.messageExt
  = messageExt + setThumbImage(thumb) + mediaTagName
- WeChatManager.ShareLink 把 thumbnailURL: String? 替换为 thumb:
  UIImage?;shareLink 内 setThumbImage(thumb) + mediaTagName。drop
  Sendable 因 UIImage 不可 Sendable(调用都在 MainActor 上无风险)
- WechatShare 双分支统一传 appIconImage() 做 thumb;截图分支 messageExt
  = content.desc + mediaTagName "进贤聚友棋牌游戏截图分享";链接分支
  mediaTagName "进贤聚友棋牌游戏分享"(字面对齐 msext WechatShareManager.m)

QQ 截图分享原本 4-fallback URL 链不靠谱(file_type=news 给截图场景实际
无意义)。简化为 Douyin 同款模式:截图 → UIPasteboard.image → 弹"立即
打开 QQ / 稍后分享"二选一 Alert。承认 ADR-006 无 QQ SDK 退化的现实,
但 UX 不再让用户摸不着北。
2026-06-24 07:53:14 +08:00

92 lines
3.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// WechatShare.swift
// ylgamehall
//
// 微信分享 SharePlatform 实现,1:1 对齐 daoqi msext WechatShareManager
// `shareWithContent:completion:`(仅两个分支):
// - type == "2" → 截图分享(getImageWithFullScreenshot + JPEG 0.6 + sendImageData
// - else → 链接分享(sendLinkURL + webpageUrl + title + desc
//
// 缩略图:app icon(与原 `[self getAppIconImage]` 同源策略,不用 sharelogo)。
//
// fire-and-forgetH5 端的 sharesuccess 已在 FriendsShareHandler 立即触发,
// 此处不再回传结果(与原 dispatch_after 1s completion(YES) 实质等价)。
//
import Foundation
import UIKit
@MainActor
public final class WechatShare: SharePlatform {
public static let shared = WechatShare()
public let name = "WeChat"
public var isInstalled: Bool {
WXApi.isWXAppInstalled()
}
public init() {}
public func share(_ content: ShareContent, scene: ShareScene) {
guard WXApi.isWXAppInstalled() else { return }
let wxScene: WeChatManager.ShareScene = (scene == .timeline) ? .timeline : .session
let thumb = Self.appIconImage()
Task { @MainActor in
do {
if content.type == "2" {
// 截图分享(msext WechatShareManager isScreenshotShare 分支)
let shot = try ImageProvider.captureScreenshot()
guard let imageData = shot.jpegData(compressionQuality: 0.6) else { return }
try await WeChatManager.shared.shareImage(
.init(
imageData: imageData,
thumb: thumb,
messageExt: content.desc,
mediaTagName: "\(Self.appTagPrefix)游戏截图分享"
),
scene: wxScene
)
} else {
// 链接分享(msext else 分支,含 type=="1" 与任何其它值)
try await WeChatManager.shared.shareLink(
.init(
url: content.webpageUrl,
title: content.title,
desc: content.desc,
thumb: thumb,
mediaTagName: "\(Self.appTagPrefix)游戏分享"
),
scene: wxScene
)
}
} catch {
// SDK 回包失败不向 H5 反馈(sharesuccess 已乐观发出,对齐原项目)
}
}
}
/// msext WechatShareManager 字面常量 "进贤聚友棋牌"(与 daoqi
/// SGDefineInfo.h gamehallname 同源),用于 WXMediaMessage.mediaTagName 拼接。
private static let appTagPrefix = "进贤聚友棋牌"
/// app icon 缩略图。读 Info.plist `CFBundleIcons` → `CFBundlePrimaryIcon`
/// → `CFBundleIconFiles` 的最后一个(最高分辨率),与原 msext
/// WechatShareManager `getAppIconImage` 等价。失败兜底返回 1x1 透明图。
private static func appIconImage() -> UIImage {
if let icons = Bundle.main.object(forInfoDictionaryKey: "CFBundleIcons") as? [String: Any],
let primary = icons["CFBundlePrimaryIcon"] as? [String: Any],
let files = primary["CFBundleIconFiles"] as? [String],
let last = files.last,
let icon = UIImage(named: last) {
return icon
}
if let icon = UIImage(named: "AppIcon") { return icon }
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 1, height: 1))
return renderer.image { _ in }
}
}