了解在共享 SwiftUI 时调用了哪些 Activity
Understanding Which Activity Gets Called in Sharing SwiftUI
在我的分享中 sheet 我看到了那些活动,但我想了解并获取哪些 activity 被选中来分享 url。
例如,如果选择了 WhatsApp,我想以某种方式获取 WhatsApp selected bool。
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) {
// nothing to do here
}
}
我正在尝试的是
func fetchActivity() {
if activityItems.description == "whatsapp" {
self.service.articleShare(article_id: articlePost.article_id, share_type: "whatsapp")
}
}
struct ShareSheet: UIViewControllerRepresentable {
@ObservedObject var service = ArticleService()
var articlePost: Article
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
controller.completionWithItemsHandler = { activity, success, items, error in
if !success{
print("cancelled")
return
}
if activity == UIActivity.ActivityType.postToTwitter {
print("twitter")
print(articlePost.article_id)
self.service.articleShare(article_id: articlePost.article_id, share_type: "twitter")
}
if activity == UIActivity.ActivityType.postToFacebook {
print("facebook")
self.service.articleShare(article_id: articlePost.article_id, share_type: "facebook")
}
if activity == UIActivity.ActivityType.mail {
print("mail")
print(articlePost.article_id)
self.service.articleShare(article_id: articlePost.article_id, share_type: "mail")
}
if activity == UIActivity.ActivityType.message {
}
}
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
// nothing to do here
}
}
在我的分享中 sheet 我看到了那些活动,但我想了解并获取哪些 activity 被选中来分享 url。
例如,如果选择了 WhatsApp,我想以某种方式获取 WhatsApp selected bool。
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) {
// nothing to do here
}
}
我正在尝试的是
func fetchActivity() {
if activityItems.description == "whatsapp" {
self.service.articleShare(article_id: articlePost.article_id, share_type: "whatsapp")
}
}
struct ShareSheet: UIViewControllerRepresentable {
@ObservedObject var service = ArticleService()
var articlePost: Article
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
controller.completionWithItemsHandler = { activity, success, items, error in
if !success{
print("cancelled")
return
}
if activity == UIActivity.ActivityType.postToTwitter {
print("twitter")
print(articlePost.article_id)
self.service.articleShare(article_id: articlePost.article_id, share_type: "twitter")
}
if activity == UIActivity.ActivityType.postToFacebook {
print("facebook")
self.service.articleShare(article_id: articlePost.article_id, share_type: "facebook")
}
if activity == UIActivity.ActivityType.mail {
print("mail")
print(articlePost.article_id)
self.service.articleShare(article_id: articlePost.article_id, share_type: "mail")
}
if activity == UIActivity.ActivityType.message {
}
}
return controller
}
func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
// nothing to do here
}
}