swift share按钮在模拟器中工作而不在设备中

时间:2015-01-07 17:42:53

标签: ios swift share

我在

后面有一个分享按钮
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)

        }
    }
}

2 个答案:

答案 0 :(得分:2)

问题是您尝试以不受支持的方式呈现视图控制器。

以下是Apple's UIActivityViewController Documentation的引用:

  

您的应用负责配置,展示和解除此视图控制器。视图控制器的配置涉及指定视图控制器应该在其上操作的数据对象。 (您还可以指定应用程序支持的自定义服务列表。)在呈现视图控制器时,必须使用适当的方法为当前设备执行此操作。 在iPad上,您必须在弹出窗口中显示视图控制器。在iPhone和iPod touch上,您必须以模态方式呈现它。

因此,您应该检查一下,根据具体情况,您可以查看哪种类型的设备。例如,这是检查设备类型的方法(用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)

}

答案 1 :(得分:0)

在行&#34; self.presentViewController(activityVC,..&#34;

之前添加下一个代码
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
activityVC.popoverPresentationController?.sourceView = self.view
}