0

I have a sharesheet that only seems to crash on iPad devices, running iOS 16.

The code for my sharemanager is

struct ShareManager {
    
    enum ShareType {
        case Twitter
        case Other(vc: UIViewController)
    }
    
    static func share(for type: ShareType, with post: String) {
        
        let application = UIApplication.shared
        
        guard let encodedPost = post.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
            print("Encoding failed.")
            return
        }
        
        switch type {
        case .Twitter:
            
            guard let appUrl = URL(string: "twitter://post?message=\(encodedPost)") else {
                return
            }
            
            guard let webUrl = URL(string: "https://twitter.com/intent/tweet?text=\(encodedPost)") else {
                return
            }
            
            if application.canOpenURL(appUrl) {
                // App is Installed
                print("Twitter App Installed")
                application.open(appUrl)
            } else {
                // Open Web-App instead
                print("Twitter App not Installed")
                application.open(webUrl)
            }
            
        case .Other(let vc):
            
            let sharingItems: [Any] = [post as NSString]
            let activityController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
            vc.present(activityController, animated: true)
            
        }
        
    }
    
}

The Twitter element works fine, however the share sheet presentation crashes on iPad

let sharingItems: [Any] = [post as NSString]
        let activityController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
        vc.present(activityController, animated: true)

Strangely it relates to UIPopoverPresentationController which I was not aware I was using.

user1695971
  • 103
  • 1
  • 5
  • 15

0 Answers0