通过UIActivityViewController分享到Twitter / Facebook等导致崩溃

时间:2014-08-07 21:13:54

标签: ios8 uiactivityviewcontroller

在iOS8上,我正在使用UIActivityViewController与Facebook / Twitter等共享UIImage。它似乎工作正常,但今天它在我的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})} for attributes:{       NSExtensionActivationRule = {           extensionItems =(                           {                   附件=(                                           {                           registeredTypeIdentifiers =(                               “公众形象”                           );                       },                                           {                           registeredTypeIdentifiers =(                               “public.plain文本”                           );                       },                                           {                           registeredTypeIdentifiers =(                               “public.url”                           );                       }                   );               }           );       };       NSExtensionPointName =(           “com.apple.share服务”,           “com.apple.ui服务”,           “com.apple.services”       ); 2014-08-07 21:38:59.208 collageTest [279:11021] LaunchServices:invalidationHandler名为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})} for attributes:{       NSExtensionPointName =“com.apple.share-services”; 2014-08-07 21:38:59.216 collageTest [279:11021] LaunchServices:   invalidationHandler名为

5 个答案:

答案 0 :(得分:14)

查看the docs,我需要为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;

答案 1 :(得分:14)

popoverPresentationController是iOS 8的新功能,并且会在iOS 7上崩溃。它在iPhone上也只是零,因为它只能在iPad上UIPopover。这是基督徒在斯威夫特的答案,考虑到这些事实:

Swift 2.0(Xcode 7)

let controller = UIActivityViewController(activityItems: [text, url, myImage], applicationActivities: nil)

presentViewController(controller, animated: true, completion: nil)

if #available(iOS 8.0, *) {
    let presentationController = controller.popoverPresentationController
    presentationController?.sourceView = view
}

Swift 1.2(Xcode 6)

let controller = UIActivityViewController(activityItems: [text, url, myImage], applicationActivities: nil)

presentViewController(controller, animated: true, completion: nil)

if controller.respondsToSelector("popoverPresentationController") {
    // iOS 8+
    let presentationController = controller.popoverPresentationController
    presentationController?.sourceView = view
}

答案 2 :(得分:0)

正如@mmccomb告诉here,在iPad上,活动视图控制器将使用新的UIPopoverPresentationController显示为弹出窗口。您至少需要指定源视图:

activityViewController.popoverPresentationController.sourceView = YOURDESIREDVIEW;

如果要显示锚定到该视图的任何点的弹出窗口,请使用popoverPresentationController的sourceRect属性指定它。

答案 3 :(得分:0)

以下是我用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)
    }

答案 4 :(得分:0)

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会将活动控制器显示为弹出控件,因此它需要了解和使用源视图。在前面的代码中,如果设备是iPad,我们没有传递源视图,这就是为什么该应用在iPad上运行时崩溃的原因。

解决方案

要解决此问题,我们将检查控制器是否显示为弹出窗口(应用程序在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)
}