,因爲你需要從其他的UIViewController提出一個UIViewController您收到此錯誤。所以
self.presentViewController(...)
不會工作,因爲自我(SKScene)不是UIViewController。從SKScene介紹,你會不得不說這
view?.window?.rootViewController?.presentViewController(fShare!, animated: true, completion: nil)
我建議你不要使用那些API了。更好地使用UIActivityViewController來滿足您的共享需求。這樣,您只需在應用程序中使用一個共享按鈕,即可共享各種服務(電子郵件,Twitter,Facebook,iMessage,WhatsApp等)。
創建一個新的Swift文件並添加此代碼。
enum ShareMenu {
static func open(text: String, image: UIImage?, appStoreURL: String?, from viewController: UIViewController?) {
guard let viewController = viewController, let view = viewController.view else { return }
// Activity items
var activityItems = [Any]()
// Text
activityItems.append(text)
// Image
if let image = image {
activityItems.append(image)
}
/// App url
if let appStoreURL = appStoreURL {
let items = ActivityControllerItems(appStoreURL: appStoreURL)
activityItems.append(items)
}
// Activity controller
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
// iPad settings
if UIDevice.current.userInterfaceIdiom == .pad {
activityController.modalPresentationStyle = .popover
activityController.popoverPresentationController?.sourceView = view
activityController.popoverPresentationController?.sourceRect = CGRect(x: view.bounds.midX, y: view.bounds.midY, width: 0, height: 0)
activityController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.init(rawValue: 0)
}
// Excluded activity types
activityController.excludedActivityTypes = [
.airDrop,
.print,
.assignToContact,
.addToReadingList,
]
// Present
DispatchQueue.main.async {
viewController.present(activityController, animated: true)
}
// Completion handler
activityController.completionWithItemsHandler = { (activity, success, items, error) in
guard success else {
if let error = error {
print(error.localizedDescription)
}
return
}
// do something if needed
}
}
}
// MARK: - Activity Controller Items
/**
ActivityControllerItems
*/
private final class ActivityControllerItems: NSObject {
// MARK: - Properties
/// App name
fileprivate let appName = Bundle.main.infoDictionary?["CFBundleName"] as? String ?? "-"
/// App store web url
fileprivate let appStoreURL: String
// MARK: - Init
/// Init
fileprivate init(appStoreURL: String) {
self.appStoreURL = appStoreURL
super.init()
}
}
// MARK: - UIActivityItemSource
/// UIActivityItemSource
extension ActivityControllerItems: UIActivityItemSource {
/// Getting data items
/// Placeholder item
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}
/// Item for actity type
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
return URL(string: appStoreURL) ?? appName
}
/// Provide info about data items
/// Subject field for services such as email
func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
return appName
}
}
當按下分享按鈕比你可以把它像這樣
ShareMenu.open(
text: "Can you beat my score?",
image: UIImage(...), // set to nil if unused
appStoreURL: "your iTunes app store URL", // set to nil if unused
from: view?.window?.rootViewController
)
記住,圖像和appStoreURL不會出現遍地開花,這取決於共享服務。
您也可以從場景中使用你的分數值,並將它添加到文本e.g
ShareMenu.open(
text: "Can you beat my score \(self.score)?",
...
)
希望這有助於
請發佈您正在使用的代碼(相關部分)。但通常,您可以在適當的視圖控制器中指定視圖控制器相關的方法,並在您想要調用這些方法時從場景發佈通知。 – Whirlwind
來自'else if'塊的代碼應該替換爲可以發佈通知的代碼。它也應該被移動到一個合適的視圖控制器,該視圖控制器有一個必需的成員方法並監聽所提到的通知。 – Whirlwind
我不確定我是否遵循,我將代碼移到了自定義視圖控制器類,但是我仍然無法從我的場景中調用它。 – Matt