ShareSheet.swift (1749B)
1 /* 2 * This file is part of GNU Taler, ©2022-25 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import Foundation 9 import UIKit 10 import SymLog 11 12 // You can control the appearance of the link by providing view content. 13 // For example, you can use a Label to display a link with a custom icon: 14 // ShareLink(item: URL(string: "https://developer.apple.com/xcode/swiftui/")!) { 15 // Label("Share", image: "MyCustomShareIcon") 16 // } 17 // If you only wish to customize the link’s title, you can use one of the convenience 18 // initializers that takes a string and creates a Label for you: 19 // ShareLink("Share URL", item: URL(string: "https://developer.apple.com/xcode/swiftui/")!) 20 // The link can share any content that is Transferable. 21 // Many framework types, like URL, already conform to this protocol. 22 23 24 public class ShareSheet: ObservableObject { 25 26 @MainActor 27 static func shareSheet(textToShare: String, image: UIImage?) { 28 let plainString: String? 29 if #available(iOS 18.3, *) { 30 plainString = textToShare.removingPercentEncoding 31 } else { 32 plainString = textToShare 33 } 34 // var strings 35 var items: [Any] = [plainString as Any] 36 if let image { 37 items.append(image as Any) 38 } 39 let activityView = UIActivityViewController(activityItems: items, applicationActivities: nil) 40 41 let allScenes = UIApplication.shared.connectedScenes 42 let scene = allScenes.first { $0.activationState == .foregroundActive } 43 44 if let windowScene = scene as? UIWindowScene { 45 windowScene.keyWindow?.rootViewController?.present(activityView, animated: true, completion: nil) 46 } 47 } 48 49 init() { 50 } 51 }