I create a simple share button to share some texts. Although the ShareSheet is working, I received unexpected errors messages in my console. I am a beginner level and I have no idea whether should I worry about the error message. Please let me know if there is a better solution for ShareSheet. The messages are
2021-11-22 19:36:03.677822-0500 ZSV5_IDChange[8282:264096] [LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of a UICollectionReusableView that is managed by a UICollectionView is not supported, and will result in incorrect self-sizing. View: <_UIActivityContentFooterView: 0x7feec8925ad0; baseClass = UICollectionReusableView; frame = (16 256; 358 52); layer = <CALayer: 0x6000038ec4c0>>
2021-11-22 19:36:03.688591-0500 ZSV5_IDChange[8282:264096] [default] -imageForImageDescriptor: can do IO please adopt -imageForDescriptor: for IO free drawing or -prepareImageForDescriptor: if IO is allowed. (This will become a fault soon.)
2021-11-22 19:36:03.859368-0500 ZSV5_IDChange[8282:264096] [UICollectionViewRecursion] cv == 0x7feec7065a00 Disabling recursion trigger logging
Also, I get UIApplication.shared.windows will deprecate in iOS 15. If it is true, I think it will be a smarter choice to use ShareSheet() instead of using private func shareText(text: String). Please have a look at my code before I overcomplicated my question.
Below is my simple code:
import Foundation
import SwiftUI
struct DataArray: Identifiable {
let id: Int
let cities: String
let name1: String
let name2: String
let isFavorite: Bool
}
public struct ListDataArray {
static let dot = [
DataArray(id: 1,
cities: "Baltimore"
name1: "John",
name2: "Mike",
isFavorite: False),
DataArray(id: 2,
cities: "Frederick"),
name1: "Joe",
name2: "Swift",
isFavorite: False),
DataArray(id: 3,
cities: "Catonsville"
name1: "Susan",
name2: "Oliver",
isFavorite: False),
// There will be a lot of data
]
}
struct TestShareSheet3: View {
var data: DataArray
@State private var showShareSheet: Bool = false
var body: some View {
VStack {
Button {
self.showShareSheet.toggle()
//shareText(text: "\(data.cities)\n\(data.name1) - \(data.name2)")
} label: {
Text("Share")
.foregroundColor(.blue)
.font(.title3)
.padding()
.background(Color.green)
.cornerRadius(10)
}
.sheet(isPresented: self.$showShareSheet) {
// On Dismiss
} content: {
ShareSheet(activityItems: ["\(data.cities)\n\(data.name1) - \(data.name2)"])
}
}
}
// This function also has the same issue
private func shareText(text: String) {
let av = UIActivityViewController(activityItems: [text], applicationActivities: nil)
UIApplication.shared.windows.first?.rootViewController?.present(av, animated: true, completion: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
av.popoverPresentationController?.sourceView = UIApplication.shared.windows.first
av.popoverPresentationController?.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2.1, y: UIScreen.main.bounds.height / 1.3, width: 200, height: 200)
}
}
}
struct Test2_Previews: PreviewProvider {
static var previews: some View {
// Testing purposes
TestShareSheet3(data: ListDataArray.dot[1])
}
}
struct ShareSheet: UIViewControllerRepresentable {
typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void
let activityItems: [Any]
let applicationActivities: [UIActivity]? = nil
let excludedActivityTypes: [UIActivity.ActivityType]? = nil
let callback: Callback? = nil
func makeUIViewController(context: Context) -> UIActivityViewController {
let controller = UIActivityViewController(
activityItems: activityItems, applicationActivities: applicationActivities)
controller.excludedActivityTypes = excludedActivityTypes
controller.completionWithItemsHandler = callback
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) { }
}