共享按钮在 iPhone 上完美运行,但在 iPad 上崩溃
Sharing button works perfectly on iPhone but crash on iPad
我正在尝试添加一个按钮,以便在 Twitter、Facebook 等中分享一些句子。这一切都适用于所有 iPhone 模型,但模拟器崩溃并出现 iPad。
这是我的代码:
@IBAction func shareButton(sender: AnyObject) {
frase = labelFrases.text!
autor = labelAutores.text!
var myShare = "\(frase) - \(autor)"
let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
这是错误:
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7c0f9190>) should have a non-nil sourceView or barButtonItem set before the presentation occurs
我该如何解决?
对于ipad (iOS > 8.0) 你需要设置popoverPresentationController:
//check ipad
if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
//ios > 8.0
if ( activityVC.respondsToSelector(Selector("popoverPresentationController"))){
activityVC.popoverPresentationController?.sourceView = super.view
}
}
self.presentViewController(activityVC, animated: true, completion: nil)
更多信息在这里:
UIActivityViewController crashing on iOS 8 iPads
为 Swift 5 执行此操作,以使共享按钮在 iPad 和 iPhone 上都有效:
@IBAction func shareButton(sender: UIButton) { {
let itemToShare = ["Some Text goes here"]
let avc = UIActivityViewController(activityItems: itemToShare, applicationActivities: nil)
//Apps to be excluded sharing to
avc.excludedActivityTypes = [
UIActivityType.print,
UIActivityType.addToReadingList
]
// Check if user is on iPad and present popover
if UIDevice.current.userInterfaceIdiom == .pad {
if avc.responds(to: #selector(getter: UIViewController.popoverPresentationController)) {
avc.popoverPresentationController?.barButtonItem = sender
}
}
// Present share activityView on regular iPhone
self.present(avc, animated: true, completion: nil)
}
希望对您有所帮助!
我正在尝试添加一个按钮,以便在 Twitter、Facebook 等中分享一些句子。这一切都适用于所有 iPhone 模型,但模拟器崩溃并出现 iPad。
这是我的代码:
@IBAction func shareButton(sender: AnyObject) {
frase = labelFrases.text!
autor = labelAutores.text!
var myShare = "\(frase) - \(autor)"
let activityVC: UIActivityViewController = UIActivityViewController(activityItems: [myShare], applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
这是错误:
Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<_UIAlertControllerActionSheetRegularPresentationController: 0x7c0f9190>) should have a non-nil sourceView or barButtonItem set before the presentation occurs
我该如何解决?
对于ipad (iOS > 8.0) 你需要设置popoverPresentationController:
//check ipad
if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
//ios > 8.0
if ( activityVC.respondsToSelector(Selector("popoverPresentationController"))){
activityVC.popoverPresentationController?.sourceView = super.view
}
}
self.presentViewController(activityVC, animated: true, completion: nil)
更多信息在这里: UIActivityViewController crashing on iOS 8 iPads
为 Swift 5 执行此操作,以使共享按钮在 iPad 和 iPhone 上都有效:
@IBAction func shareButton(sender: UIButton) { {
let itemToShare = ["Some Text goes here"]
let avc = UIActivityViewController(activityItems: itemToShare, applicationActivities: nil)
//Apps to be excluded sharing to
avc.excludedActivityTypes = [
UIActivityType.print,
UIActivityType.addToReadingList
]
// Check if user is on iPad and present popover
if UIDevice.current.userInterfaceIdiom == .pad {
if avc.responds(to: #selector(getter: UIViewController.popoverPresentationController)) {
avc.popoverPresentationController?.barButtonItem = sender
}
}
// Present share activityView on regular iPhone
self.present(avc, animated: true, completion: nil)
}
希望对您有所帮助!