0

我正在尝试在我的应用程序中创建一个共享按钮。SwiftUi 上的应用,Xcode 版本 13.2.1,iPhone 15.2

如果我使用此代码:

let url = URL(string: "https://example.com")
let activityController = UIActivityViewController(activityItems: [url!], applicationActivities: nil)                        
UIApplication.shared.windows.first?.rootViewController!.present(activityController, animated: true, completion: nil)

这是错误:

[Presentation] Attempt to present <UIActivityViewController: 0x7feb1600e600> on < TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier _: 0x7feb16a05430> (from < TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier _: 0x7feb16a05430>) which is already presenting < TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView : 0x7feb05f142e0>.

我知道它已经在展示,但我不明白你如何绕过它。

如果我使用以下组合:

如果我添加一个.presentedViewController?并获得此组合,共享按钮有效,但仅在 iPhone 上并且控制台中有很多错误,在 iPad 上应用程序完全崩溃。

UIApplication.shared.windows.first?.rootViewController!.presentedViewController?.present(activityController, animated: true, completion: nil)

你能告诉我如何解决这个问题吗?

4

1 回答 1

0

我能够使用这个答案解决问题。也许它对某人有用,就像对我一样。

通过更改一行

source.present(activityVC, animated: true)

对此

source.presentedViewController?.present(activityVC, animated: true)
于 2022-01-30T15:02:52.557 回答