Files
suixinkan_ios_new/suixinkan/Core/WeChat/WeChatShareSheet.swift
汉秋 a528d9b0d7 接入微信分享 SDK 并配置 Universal Link。
集成 WechatOpenSDK、www.zhifly.cn/suixinkan/ 通用链接与邀请页分享能力,附带 AASA 部署文件与单元测试。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-02 15:02:37 +08:00

106 lines
3.4 KiB
Swift
Raw Permalink 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.

//
// WeChatShareSheet.swift
// suixinkan
//
import SwiftUI
import UIKit
/// /
struct WeChatShareSheet: View {
let title: String
let description: String
let url: String
let thumbImage: UIImage?
let onResult: (String) -> Void
@Environment(\.dismiss) private var dismiss
@State private var isPresentingSystemShare = false
var body: some View {
NavigationStack {
List {
if WeChatShareService.isWeChatAvailable {
shareButton("微信好友", systemImage: "message.fill") {
share(scene: .session)
}
shareButton("朋友圈", systemImage: "person.2.fill") {
share(scene: .timeline)
}
}
shareButton("更多分享", systemImage: "square.and.arrow.up") {
isPresentingSystemShare = true
}
}
.navigationTitle("分享")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("取消") { dismiss() }
}
}
}
.sheet(isPresented: $isPresentingSystemShare) {
let text = shareText
ActivityShareView(activityItems: [text]) {
onResult("已打开系统分享")
dismiss()
}
}
}
private var shareText: String {
let trimmedURL = url.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedURL.isEmpty else { return title }
return "\(title)\n\(trimmedURL)"
}
private func shareButton(_ label: String, systemImage: String, action: @escaping () -> Void) -> some View {
Button(action: action) {
Label(label, systemImage: systemImage)
}
}
private func share(scene: WeChatShareScene) {
let payload = WeChatWebPageSharePayload(
title: title,
description: description,
url: url
)
let thumb = thumbImage ?? WeChatShareService.defaultThumbImage()
WeChatShareService.shareWebPage(payload, scene: scene, thumbImage: thumb) { result in
switch result {
case .success:
onResult("已分享到\(scene.title)")
case .cancelled:
onResult("已取消分享")
case .notInstalled:
isPresentingSystemShare = true
case .unsupported:
onResult("当前微信版本不支持分享")
case .invalidPayload(let message):
onResult(message)
case .sendFailed, .unknown:
onResult("分享失败,请稍后重试")
}
dismiss()
}
}
}
///
private struct ActivityShareView: UIViewControllerRepresentable {
let activityItems: [Any]
let onComplete: () -> Void
func makeUIViewController(context: Context) -> UIActivityViewController {
let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
controller.completionWithItemsHandler = { _, _, _, _ in
onComplete()
}
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {}
}