ios13share-button

How to update share button coding to work on iOS 13


My code for the share button works well on all versions below iOS 13, How do I modify the coding to work on iOS 13 for both IPad and IPhone?

   class CatseyeViewController: BaseViewController{
        
        @IBOutlet weak var sliderImage: UIImageView!
        
        
        @IBAction func share(_ sender: UIButton) {
        
          let activityController = UIActivityViewController(activityItems: 
[self.sliderImage.image as Any], applicationActivities: nil)
                           
    self.present(activityController, animated:true, completion: nil)
         
    if let popOver = activityController.popoverPresentationController {
        popOver.sourceView = self.view
                             
              }
                       
                      
                   }
                
        }

Solution

  • In order to update the share sheet to work on iOS 13, create an IF/ELSE statement to detect first for iOS 13 versus all other versions.

     class CatseyeViewController: BaseViewController{
    
     @IBOutlet weak var sliderImage: UIImageView!
     
    @IBAction func share(_ sender: UIButton) {
            
            let version = OperatingSystemVersion(majorVersion: 13, minorVersion: 0, patchVersion: 0)
                      if ProcessInfo.processInfo.isOperatingSystemAtLeast(version) {
                         /* runNewCode()*/
            let items = [self.sliderImage.image]
                   let ac = UIActivityViewController(activityItems: items as [Any], applicationActivities: nil)
               UIApplication.shared.windows.first?.rootViewController?.present(ac, animated: true, completion: nil)
                        
                        if UIDevice.current.userInterfaceIdiom == .pad {
                                   ac.popoverPresentationController?.sourceView = UIApplication.shared.windows.first
                                  ac.popoverPresentationController?.sourceRect = CGRect(
                                       x: UIScreen.main.bounds.width / 2.1,
                                       y: UIScreen.main.bounds.height / 2.3,
                                       width: 200, height: 200)
                               }         
            }
            else
                      {
            /* runOriginalCode() that works for earlier IOS*/
            
            let activityController = UIActivityViewController(activityItems: [self.sliderImage.image as Any], applicationActivities: nil)
            
            self.present(activityController, animated:true, completion: nil)
            if let popOver = activityController.popoverPresentationController {
                popOver.sourceView = self.view
             
            }
          
        
            }//end of else statement
        }//end of share function
    }//end of controller