gpt4 book ai didi

ios - 快速共享按钮适用于模拟器而不是设备

转载 作者:行者123 更新时间:2023-11-28 13:18:25 26 4
gpt4 key购买 nike

我在后面有一个分享按钮

http://www.codingexplorer.com/sharing-swift-app-uiactivityviewcontroller/

这在模拟器中运行良好,它从底部弹出(iphone6),但当然它没有 facebook 和其他选项,只有电子邮件和其他选项。但是当我在 ipad 上尝试这个时,它给出了一个错误

2015-01-07 12:32:43.173  Clown[2011:2967183] LaunchServices: invalidationHandler called
2015-01-07 12:32:43.185 Clown[2011:2967169] LaunchServices: invalidationHandler called
2015-01-07 12:32:43.766 Clown[2011:2967083] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x1565e7d0>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'

他确实在文章中提到将抛出一个 invalidationHandler 但忽略它,因为一切正常。但是,它没有,应用程序崩溃了。

看起来很像

http://stackoverflow.com/questions/26039229/swift-uialtertcontroller-actionsheet-ipad-ios8-crashes

但我试图添加它,但我的代码不知道 alertcontroller 是什么。这是代码

@IBAction func handleShareButton(sender: AnyObject) {
if let detail: AnyObject = self.detailItem {
let theItem = detail as [String: String]
let textToShare = "..."

if let myWebsite = NSURL(string: theItem["image"]!)
{
let objectsToShare = [textToShare, myWebsite]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

//New Excluded Activities Code
activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList]

self.presentViewController(activityVC, animated: true, completion: nil)

}
}
}

最佳答案

问题是您试图以不受支持的方式呈现 View Controller 。

这是来自 Apple's UIActivityViewController Documentation 的引述:

Your app is responsible for configuring, presenting, and dismissing this view controller. Configuration for the view controller involves specifying the data objects on which the view controller should act. (You can also specify the list of custom services your app supports.) When presenting the view controller, you must do so using the appropriate means for the current device. On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally.

因此,您应该检查一下您拥有的是哪种类型的设备,并根据它以不同的方式呈现。例如,这是检查设备类型的方法(用 Obj-C 编写,但应该很容易翻译):

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// The app is running on an iPad, so you have to wrap it in a UIPopOverController
UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:activityVC];

// Option 1: If your "share" button is a UIBarButtonItem
[popOver presentPopoverFromBarButtonItem:barButtonItem permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

// Option 2: If your "share" button is NOT a UIBarButtonItem
[popOver presentPopoverFromRect:someRect inView:someView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
// The app is running on an iPod Touch or iPhone
// Use the same code you already have here...
[self presentViewController:activityVC animated:YES completion:nil];
}


编辑:(naturalc 为 Swift 提供的新代码)

if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
// The app is running on an iPad, so you have to wrap it in a UIPopOverController
var popOver: UIPopoverController = UIPopoverController(contentViewController: activityVC)

// if your "share" button is a UIBarButtonItem
popOver.presentPopoverFromBarButtonItem(self.shareButton, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)

} else {
self.presentViewController(activityVC, animated: true, completion: nil)

}

关于ios - 快速共享按钮适用于模拟器而不是设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27825366/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com