swift 分享按钮在模拟器中有效,在设备中无效
swift share button works in simulator not in device
我在
之后有一个分享按钮
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 但忽略它,因为一切正常。但是,它没有,应用程序崩溃了。
似乎与
相似
但我尝试添加它,但我的代码不知道 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)
}
}
}
问题是您试图以不受支持的方式呈现视图控制器。
引用自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)
}
在行前添加下一个代码 "self.presentViewController(activityVC,.."
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
activityVC.popoverPresentationController?.sourceView = self.view
}
我在
之后有一个分享按钮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 但忽略它,因为一切正常。但是,它没有,应用程序崩溃了。
似乎与
相似
但我尝试添加它,但我的代码不知道 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)
}
}
}
问题是您试图以不受支持的方式呈现视图控制器。
引用自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)
}
在行前添加下一个代码 "self.presentViewController(activityVC,.."
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
activityVC.popoverPresentationController?.sourceView = self.view
}