Ios8 通过UIActivityViewController共享到Twitter/Facebook等,导致崩溃

Ios8 通过UIActivityViewController共享到Twitter/Facebook等,导致崩溃,ios8,uiactivityviewcontroller,Ios8,Uiactivityviewcontroller,在iOS8上,我使用UIActivityViewController将UIImage共享到Facebook/Twitter等网站。它似乎工作正常,但今天在我的iPad上运行代码时突然崩溃。但是,它在模拟器中仍能正常工作 我的代码: UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[text, url, myImage] applicationActi

在iOS8上,我使用UIActivityViewController将UIImage共享到Facebook/Twitter等网站。它似乎工作正常,但今天在我的iPad上运行代码时突然崩溃。但是,它在模拟器中仍能正常工作

我的代码:

UIActivityViewController *controller =
[[UIActivityViewController alloc]
 initWithActivityItems:@[text, url, myImage]
 applicationActivities:nil];

[self presentViewController:controller animated:YES completion:nil];
崩溃时,Xcode会吐出:

发现的扩展:{( {id=com.apple.share.Facebook.post}, {id=com.apple.share.Twitter.post}, {id=com.apple.share.TencentWeibo.post}, {id=com.apple.share.SinaWeibo.post}属性:{ NSExtensionActivationRule={ 伸展岩=( { 附件=( { registeredTypeIdentifiers=( “公众形象” ); }, { registeredTypeIdentifiers=( “公共.纯文本” ); }, { registeredTypeIdentifiers=( “public.url” ); } ); } ); }; NSExtensionPointName=( “com.apple.share services”, “com.apple.ui服务”, “com.apple.services” )}2014-08-07 21:38:59.208 collageTest[279:11021]LaunchServices:invalizationhandler调用2014-08-07 21:38:59.212 collageTest[279:11016]发现的扩展:{( {id=com.apple.share.Flickr.post}, {id=com.apple.mobileslideshow.StreamShareService}, {id=com.apple.share.Twitter.post}, {id=com.apple.share.Facebook.post}, {id=com.apple.share.Vimeo.post}, {id=com.apple.share.SinaWeibo.post}, {id=com.apple.share.TencentWeibo.post}属性:{ NSExtensionPointName=“com.apple.share services”}2014-08-07 21:38:59.216 collageTest[279:11021]启动服务: 调用了InvalizationHandler

现在,我需要为popover控制器定义一个源代码视图

UIActivityViewController *controller =
[[UIActivityViewController alloc]
 initWithActivityItems:@[text,url,myImage]
 applicationActivities:nil];

[self presentViewController:controller animated:YES completion:nil];

UIPopoverPresentationController *presentationController =
[controller popoverPresentationController];

presentationController.sourceView = self.view;

popoverPresentationController
是iOS 8的新版本,将在iOS 7上崩溃。在iPhone上也将是零,因为它只在iPad上的
uipover
中。以下是Christian在Swift中的回答,并考虑了这些事实:

Swift 2.0(代码7) Swift 1.2(代码6) 正如@mmccomb所说,在iPad上,活动视图控制器将使用新的UIPopoverPresentationController显示为一个popover。您至少需要指定源代码视图:

activityViewController.popoverPresentationController.sourceView = YOURDESIREDVIEW;

如果要显示锚定到该视图任意点的popover,请使用PopoOverPresentationController的sourceRect属性指定它。

以下是我如何使用swift解决的:

    let someText:String = "shareText"
    let google:NSURL = NSURL(string:"http://google.com")!

    let activityViewController = UIActivityViewController(activityItems: [someText, google], applicationActivities: nil)

    if respondsToSelector("popoverPresentationController") {
        self.senderView.presentViewController(activityViewController, animated: true, completion: nil)
        activityViewController.popoverPresentationController?.sourceView = sender
    }else{
        senderView.presentViewController(activityViewController, animated: true, completion: nil)
    }
Swift 5-工作项目的副本 问题 如果您以以下方式呈现
UIActivityViewController
进行共享,您的应用程序将正常工作,但在iPad设备上会崩溃

func shareSiteURL()
{
    let title = "My app"
    let url = URL(string: "https://myWebSite.com")

    let activityController = UIActivityViewController(activityItems: [url!, title], applicationActivities: nil)
    self.present(activityController, animated: true, completion: nil)
}
解释 当您在iPad上显示
UIActivityViewController
时,iPad会将活动控制器显示为popover控制器,因此需要了解并使用源代码视图。在前面的代码中,如果设备是iPad,我们没有传递源代码视图,这就是为什么应用程序在iPad上运行时崩溃的原因

解决方案 为了解决这个问题,我们将检查控制器是否显示为popover(应用程序正在iPad上运行),如果是,我们将传递源代码视图,否则应用程序将崩溃

func shareSiteURL()
{
    let title = "My app"
    let url = URL(string: "https://myWebSite.com")

    // check if the controller is presented as a popover (the app is running on iPad)
    // and if so, we will pass the source view otherwise the app will crash.

    if let popoverController = activityController.popoverPresentationController
    {
        popoverController.sourceView = viewController.view
    }

    let activityController = UIActivityViewController(activityItems: [url!, title], applicationActivities: nil)
    self.present(activityController, animated: true, completion: nil)
}

和我在项目中遇到的问题一样。你找到背后的原因了吗?我在Swift中也尝试过同样的方法,但是Xcode force会打开popoverPresentationController,当我构建并运行时,仍然会看到InvalizationHandler问题,它会在找到popoverPresentationController nil时崩溃/这对我很有用,我还喜欢设置presentationController.sourceRect,这样它就不会从屏幕角显示出来。如果您试图在iOS 7和8上使用Swift,这个答案很重要。没有一个很好的方法来检查版本,但是这个很有效。所以我使用respondsToSelector(“popoverPresentationController”),但是当我验证时,iTunes拒绝了它,说它是一个非公共选择器。我猜是因为它在iOS 7上不存在(即“非公开”),我们的应用程序应该可以在7和8上运行。这里的解决方案是什么?检查iOS版本?您是否无法向应用商店提交内容?在Swift 2.0中,如果可用(iOS 8.0,*){…},您现在可以使用
您是否能够提交到应用商店并支持iOS 7?是的,Swift向后兼容,因为它将整个语言运行时捆绑在应用本身中。即使只包含一行Swift的应用程序的大小也要大约5MB。该死,我不知道它们更大。关于这个细节,你有什么资源可以链接到我这里吗?
func shareSiteURL()
{
    let title = "My app"
    let url = URL(string: "https://myWebSite.com")

    let activityController = UIActivityViewController(activityItems: [url!, title], applicationActivities: nil)
    self.present(activityController, animated: true, completion: nil)
}
func shareSiteURL()
{
    let title = "My app"
    let url = URL(string: "https://myWebSite.com")

    // check if the controller is presented as a popover (the app is running on iPad)
    // and if so, we will pass the source view otherwise the app will crash.

    if let popoverController = activityController.popoverPresentationController
    {
        popoverController.sourceView = viewController.view
    }

    let activityController = UIActivityViewController(activityItems: [url!, title], applicationActivities: nil)
    self.present(activityController, animated: true, completion: nil)
}