Sharing via UIActivityViewController to Twitter/Facebook etc. causing crash

后端 未结 5 1182
野的像风
野的像风 2020-12-06 02:10

On iOS8 I\'m using a UIActivityViewController to share a UIImage to Facebook/Twitter etc. It seemed to be working fine, but today it suddenly started crashing when running t

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 02:53

    popoverPresentationController was new to iOS 8 and will crash on iOS 7. It'll also be nil on iPhone because it's only in a UIPopover on iPad. Here's Christian's answer in Swift, with those facts taken into account:

    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
    }
    

提交回复
热议问题