Phase 4.B:SharePlatform 框架 + SharePanel 三选一 UI + ShareCenter 分发

按 msext gameController.m:575-597 / SharePanel.m 模式落地分享框架。

新增 Source/Share/:
  - SharePlatform.swift:
    * SharePlatform 协议(name / isInstalled / share async → ShareResult)
    * ShareContent struct(nonisolated Sendable,6 字段 sharefriend/sharetype/
      type/webpageUrl/title/desc)
    * ShareResult enum (success / cancelled / notInstalled / failed)
    * ShareScene enum (friend / timeline)
  - SharePanel.swift:
    * @MainActor UIView,半透明黑底覆盖全屏 + 底部圆角内容视图滑入
    * 三按钮(微信绿 / QQ 蓝 / 抖音黑)+ label 等间距居中布局
    * 已安装平台按钮高亮,未装置灰
    * 点击空白区 dismiss + completion(.cancelled)
    * 点击按钮 dismiss + 触发对应 SharePlatform.share + completion(result)
    * 0.25s 滑入/滑出动画
    * 与 msext SharePanel.m 等价行为
  - ShareCenter.swift:
    * sharefriend == "1" → SharePanel.show 三选一
    * sharefriend == "2" → WechatShare.share scene: .timeline 朋友圈
    * 与 msext gameController.m:585 行为 1:1
  - WechatShare.swift:stub(Phase 4.E 等微信 SDK 接入)
  - QQShare.swift:stub + canOpenURL("mqqapi://") 检测(Phase 4.C 升级
    URL Scheme 真实调起)
  - DouyinShare.swift:stub + canOpenURL("snssdk1128://") 检测(Phase 4.D 升级
    URL Scheme + Photos)

FriendsShareHandler 升级:
  - 入参解析为 ShareContent
  - 调 ShareCenter.dispatch
  - completion 内根据 result 触发 sharesuccess 反向 callback:
    * success / cancelled → bridge.call("sharesuccess", {success:"2", type:sharefriend})
    * notInstalled / failed → 不发(H5 业务期 timeout 后自行 fallback,msext 乐观策略)

ShareContent 标 nonisolated 解 Swift 6 actor 隔离(项目默认 MainActor isolation
让 struct 跨 actor 受限)。

BuildProject 通过

Plan §5 Phase 4.2 勾选

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
joywayer
2026-06-22 20:02:34 +08:00
co-authored by Claude Opus 4.7
parent b071ced657
commit 2ad4563d16
8 changed files with 436 additions and 23 deletions
@@ -2,37 +2,63 @@
// FriendsShareHandler.swift
// ylgamehall
//
// H5 Native handler stubfriendsSharetypeUrlToptitleDescript
// H5 Native handlerfriendsSharetypeUrlToptitleDescript
// docs/H5-Native-Contract.md §3.1 2 + §3.2 11sharesuccess
//
// Phase 4.A stub cb Phase 4.B SDK + Phase 4.C
// QQ URL Scheme
//
//
// 1. sharefriend (1= / 2=) + sharetype (1= / 2= /
// 3= §3.1 2) + type (1= / 2= / 3=) + url +
// title + description
// 2. ShareCenter.dispatch(req)
// - sharetype = 3 NoopSharePlatform
// - WeChatSharePhase 4.B SDK
// msext gameController.m:575-597
// 1. ShareContent
// 2. ShareCenter.dispatch
// - sharefriend == "1" SharePanel / QQ /
// - sharefriend == "2" WechatShare
// 3. bridge.call("sharesuccess", { success: "2", type: <sharefriend > })
//
// QQ ** SDK** URL Schememqq://share/to_fri
// mqqapi://share/to_qzone daoqi/msext QQShareManager.m fallback
// Phase 4.C
// SDK
// - Phase 4.E SDK WechatShare stub
// - QQ + SDK URL SchemePhase 4.C/4.D stub
//
import Foundation
@MainActor
public enum FriendsShareHandler {
public static func register(on bridge: any BridgeProtocol) {
// 2friendsSharetypeUrlToptitleDescript
// sharefriend / sharetype / type / url / title / description
// sharefriend / sharetype / type / webpageUrl / title / description
// cb: "sharefriend"msext 沿
// sharesuccess callbackPhase 4.B/4.C
bridge.register("friendsSharetypeUrlToptitleDescript") { _, callback in
// callbacksharesuccess({ success: "2", type: sharefriend })
bridge.register("friendsSharetypeUrlToptitleDescript") { [weak bridge] data, callback in
callback?(.string("sharefriend"))
guard let obj = data?.asObject else { return }
let content = ShareContent(
sharefriend: obj["sharefriend"]?.asString ?? "1",
sharetype: obj["sharetype"]?.asString ?? "",
type: obj["type"]?.asString ?? "1",
webpageUrl: obj["webpageUrl"]?.asString ?? "",
title: obj["title"]?.asString ?? "",
desc: obj["description"]?.asString ?? ""
)
let sharefriend = content.sharefriend
await MainActor.run {
ShareCenter.dispatch(content) { result in
// success / cancelled / notInstalled / failed msext
// sharesuccessURL Scheme
switch result {
case .success, .cancelled:
bridge?.call("sharesuccess",
data: .object([
"success": .string("2"),
"type": .string(sharefriend)
]),
callback: nil)
case .notInstalled, .failed:
// sharesuccessH5 timeout fallback
break
}
}
}
}
}
}
+40
View File
@@ -0,0 +1,40 @@
//
// DouyinShare.swift
// ylgamehall
//
// SharePlatform stubPhase 4.D
//
// Phase 4.B stub success
// Phase 4.D msext DouyinShareManager.m URL Scheme + Photos
// snssdk1128://share/video +
//
import UIKit
@MainActor
public final class DouyinShare: SharePlatform {
public static let shared = DouyinShare()
public let name = "Douyin"
/// app canOpenURL("snssdk1128://")
public var isInstalled: Bool {
guard let url = URL(string: "snssdk1128://") else { return false }
return UIApplication.shared.canOpenURL(url)
}
public init() {}
public func share(_ content: ShareContent, scene: ShareScene) async -> ShareResult {
guard isInstalled else { return .notInstalled(platform: name) }
// Phase 4.B stub successPhase 4.D
// Phase 4.D
// - type == 2 UIGraphicsImageRenderer PHPhotoLibrary
// - type == URLSession
// - snssdk1128://share/video snssdk1128://camera
// - Info.plist NSPhotoLibraryAddUsageDescription
// - success
return .success
}
}
+39
View File
@@ -0,0 +1,39 @@
//
// QQShare.swift
// ylgamehall
//
// QQ SharePlatform stubPhase 4.C
//
// Phase 4.B stub success SharePanel
// Phase 4.C msext QQShareManager.m:716-771 simpleShareToQQFriend
// URL Scheme fallbackmqqapi://share/to_fri?...
//
import UIKit
@MainActor
public final class QQShare: SharePlatform {
public static let shared = QQShare()
public let name = "QQ"
/// QQ app canOpenURL("mqqapi://")
public var isInstalled: Bool {
guard let url = URL(string: "mqqapi://") else { return false }
return UIApplication.shared.canOpenURL(url)
}
public init() {}
public func share(_ content: ShareContent, scene: ShareScene) async -> ShareResult {
guard isInstalled else { return .notInstalled(platform: name) }
// Phase 4.B stub successPhase 4.C URL Scheme
// Phase 4.C
// - mqqapi://share/to_fri?version=1&cflag=0&req_type=1
// &url=...&title=...&description=...
// - canOpenURL + open
// - successURL Scheme msext
return .success
}
}
+40
View File
@@ -0,0 +1,40 @@
//
// ShareCenter.swift
// ylgamehall
//
// H5 friendsShare msext gameController.m:575-597
// - sharefriend == "1"/ SharePanel /QQ/
// - sharefriend == "2" WechatShare timeline
//
// Design §8.2 ShareKit + SharePlatform
//
import Foundation
@MainActor
public enum ShareCenter {
/// FriendsShareHandler handler
/// completion UI await cb msext
public static func dispatch(_ content: ShareContent, completion: @escaping (ShareResult) -> Void) {
switch content.sharefriend {
case "1":
// / SharePanel
SharePanel.show(content: content, completion: completion)
case "2":
// WechatSharetimeline
Task { @MainActor in
let result = await WechatShare.shared.share(content, scene: .timeline)
completion(result)
}
default:
// sharefriendmsext 沿
Task { @MainActor in
let result = await WechatShare.shared.share(content, scene: .timeline)
completion(result)
}
}
}
}
+166
View File
@@ -0,0 +1,166 @@
//
// SharePanel.swift
// ylgamehall
//
// / QQ /
//
// msext SharePanel.m
// -
// - 0.25s
// - + label
// - dismiss SharePlatform.share
//
// Phase 4.B UI + SharePlatform stub
// Phase 4.C/4.D SharePlatform URL Scheme
//
import UIKit
import os.log
@MainActor
public final class SharePanel: UIView {
private static let log = Logger(subsystem: "ylgamehall", category: "SharePanel")
private static let panelHeight: CGFloat = 200
private static let buttonSize: CGFloat = 60
private static let animationDuration: TimeInterval = 0.25
private let content: ShareContent
private let completion: (ShareResult) -> Void
private let contentView = UIView()
public init(content: ShareContent, completion: @escaping (ShareResult) -> Void) {
self.content = content
self.completion = completion
super.init(frame: UIScreen.main.bounds)
backgroundColor = UIColor(white: 0, alpha: 0)
setupUI()
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) is not supported")
}
// MARK: -
///
public static func show(content: ShareContent, completion: @escaping (ShareResult) -> Void) {
guard let window = topWindow() else {
completion(.failed(reason: "no key window"))
return
}
let panel = SharePanel(content: content, completion: completion)
window.addSubview(panel)
panel.animateIn()
}
private static func topWindow() -> UIWindow? {
UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap(\.windows)
.first { $0.isKeyWindow }
}
// MARK: - UI
private func setupUI() {
//
let tap = UITapGestureRecognizer(target: self, action: #selector(handleBackgroundTap(_:)))
addGestureRecognizer(tap)
// Y
let screen = UIScreen.main.bounds
contentView.frame = CGRect(x: 0, y: screen.height, width: screen.width, height: Self.panelHeight)
contentView.backgroundColor = .white
let mask = CAShapeLayer()
mask.path = UIBezierPath(
roundedRect: contentView.bounds,
byRoundingCorners: [.topLeft, .topRight],
cornerRadii: CGSize(width: 10, height: 10)
).cgPath
contentView.layer.mask = mask
addSubview(contentView)
// + label
let buttonsContainer = UIView(frame: CGRect(x: 0, y: 30, width: screen.width, height: 110))
contentView.addSubview(buttonsContainer)
let buttons: [(title: String, platform: SharePlatform, color: UIColor)] = [
("微信", WechatShare.shared, UIColor(red: 0.06, green: 0.69, blue: 0.36, alpha: 1)),
("QQ", QQShare.shared, UIColor(red: 0.12, green: 0.51, blue: 0.97, alpha: 1)),
("抖音", DouyinShare.shared, .black)
]
let totalButtonsWidth = Self.buttonSize * CGFloat(buttons.count)
let spacing = (screen.width - totalButtonsWidth) / CGFloat(buttons.count + 1)
for (index, b) in buttons.enumerated() {
let x = spacing + CGFloat(index) * (Self.buttonSize + spacing)
let button = UIButton(type: .system)
button.frame = CGRect(x: x, y: 0, width: Self.buttonSize, height: Self.buttonSize)
button.backgroundColor = b.platform.isInstalled ? b.color : .lightGray
button.layer.cornerRadius = Self.buttonSize / 2
button.setTitle(b.title.prefix(1).description, for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 22, weight: .medium)
button.tag = index
button.addTarget(self, action: #selector(handleButtonTap(_:)), for: .touchUpInside)
buttonsContainer.addSubview(button)
let label = UILabel(frame: CGRect(x: x, y: Self.buttonSize + 8, width: Self.buttonSize, height: 20))
label.text = b.title
label.textAlignment = .center
label.font = .systemFont(ofSize: 13)
label.textColor = .darkGray
buttonsContainer.addSubview(label)
}
}
// MARK: -
private func animateIn() {
UIView.animate(withDuration: Self.animationDuration) {
self.backgroundColor = UIColor(white: 0, alpha: 0.5)
var frame = self.contentView.frame
frame.origin.y = UIScreen.main.bounds.height - Self.panelHeight
self.contentView.frame = frame
}
}
private func dismiss(then action: (() -> Void)? = nil) {
UIView.animate(withDuration: Self.animationDuration, animations: {
self.backgroundColor = UIColor(white: 0, alpha: 0)
var frame = self.contentView.frame
frame.origin.y = UIScreen.main.bounds.height
self.contentView.frame = frame
}, completion: { _ in
self.removeFromSuperview()
action?()
})
}
// MARK: -
@objc private func handleBackgroundTap(_ gesture: UITapGestureRecognizer) {
let point = gesture.location(in: self)
if !contentView.frame.contains(point) {
dismiss { [self] in completion(.cancelled) }
}
}
@objc private func handleButtonTap(_ sender: UIButton) {
let platforms: [SharePlatform] = [WechatShare.shared, QQShare.shared, DouyinShare.shared]
let platform = platforms[sender.tag]
let scene: ShareScene = content.sharefriend == "2" ? .timeline : .friend
let snapshotContent = content
dismiss {
Task { @MainActor in
let result = await platform.share(snapshotContent, scene: scene)
Self.log.debug("share via \(platform.name, privacy: .public) result=\(String(describing: result), privacy: .public)")
self.completion(result)
}
}
}
}
@@ -0,0 +1,70 @@
//
// SharePlatform.swift
// ylgamehall
//
// + ShareContent + ShareResult
//
// / QQ / SharePlatform ShareCenter
// sharefriend / conformance
//
// docs/H5-Native-Implementation-Design.md §8.2 ShareKit
//
import Foundation
import UIKit
/// H5 Native friendsShare Swift
/// SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor struct actor
/// nonisolated handler @Sendable async
nonisolated public struct ShareContent: Sendable {
/// "1""2"
public let sharefriend: String
/// msext 1 / 2 / 3 SharePanel
/// §3.1 2
public let sharetype: String
/// 1 / 2 / = URL
public let type: String
/// URL URL
public let webpageUrl: String
public let title: String
public let desc: String
public init(
sharefriend: String,
sharetype: String,
type: String,
webpageUrl: String,
title: String,
desc: String
) {
self.sharefriend = sharefriend
self.sharetype = sharetype
self.type = type
self.webpageUrl = webpageUrl
self.title = title
self.desc = desc
}
}
public enum ShareResult: Sendable {
case success
case cancelled
case notInstalled(platform: String)
case failed(reason: String)
}
/// / QQ /
public enum ShareScene: Sendable {
case friend // / QQ
case timeline // / QQ
}
@MainActor
public protocol SharePlatform: AnyObject {
/// log /
var name: String { get }
/// app SharePanel
var isInstalled: Bool { get }
/// content.type / /
func share(_ content: ShareContent, scene: ShareScene) async -> ShareResult
}
+31
View File
@@ -0,0 +1,31 @@
//
// WechatShare.swift
// ylgamehall
//
// SharePlatform stub
//
// Phase 4.E stub SDK + AppID + Universal Links
// Design §8.2 ShareKit + §8.5 / QQ
//
import Foundation
@MainActor
public final class WechatShare: SharePlatform {
public static let shared = WechatShare()
public let name = "WeChat"
/// Stub: SDK true SharePanel
/// SDK `WXApi.isWXAppInstalled()`
public var isInstalled: Bool { true }
public init() {}
public func share(_ content: ShareContent, scene: ShareScene) async -> ShareResult {
// Phase 4.E stub success
// WXApi.send + SendMessageToWXResp + state
return .success
}
}