【问题标题】:Sharing via UIActivityViewController to Twitter/Facebook etc. causing crash通过 UIActivityViewController 分享到 Twitter/Facebook 等导致崩溃
【发布时间】:2014-10-01 06:33:24
【问题描述】:

在 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 = { 扩展项 = ( { 附件 = ( { 注册类型标识符 = ( “公众形象” ); }, { 注册类型标识符 = ( “public.plain-text” ); }, { 注册类型标识符 = ( “公共.url” ); } ); } ); }; NSExtensionPointName = ( "com.apple.share-services", "com.apple.ui-services", “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} )} 属性:{ NSExtensionPointName = "com.apple.share-services"; 2014-08-07 21:38:59.216 collageTest[279:11021] LaunchServices: 调用了 invalidationHandler

【问题讨论】:

    标签: ios8 uiactivityviewcontroller


    【解决方案1】:

    查看the docs,我需要为弹出框控制器定义一个源视图

    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;
    

    【讨论】:

    • 我在项目中遇到的同样问题。找到背后的原因了吗?
    • 我在 Swift 中尝试过同样的事情,但是 Xcode 强制解开 popoverPresentationController,当我构建和运行时,我仍然看到 invalidationHandler 问题,它在找到 popoverPresentationController nil 时崩溃。 ://
    • 这对我有用,我也喜欢设置presentationController.sourceRect,这样它就不会从屏幕角落显示出来。
    【解决方案2】:

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

    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
    }
    

    【讨论】:

    • 如果您尝试在 iOS 7 和 8 上使用 Swift,这个答案很重要。没有检查版本的好方法,但这个方法有效。
    • 所以我使用了 respondsToSelector("popoverPresentationController"),但是当我验证时,iTunes 拒绝它说它是一个非公共选择器。我猜是因为它在 iOS 7 上不存在(即“非公开”),而我们的应用程序应该在 7 和 8 上都可以工作。这里的解决方案是什么?检查 iOS 版本?
    • 您无法向 App Store 提交内容吗?
    • 在 Swift 2.0 中你现在可以使用 if #available(iOS 8.0, *) { ... }
    【解决方案3】:

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

    activityViewController.popoverPresentationController.sourceView = YOURDESIREDVIEW;
    

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

      • 你能提交到 App Store 并支持 iOS 7 吗?
      • 是的,Swift 是向后兼容的,因为它将整个语言运行时捆绑在应用程序本身中。甚至包含单行 Swift 的应用程序的大小大约要大 5MB。
      • 该死,我不知道它们更大。有没有关于这个细节的资源可以链接到我?
      【解决方案5】:

      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)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-08
        • 1970-01-01
        • 2014-01-14
        • 1970-01-01
        • 2014-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多