2

I builded a func that worked for me in some of my aplications. sharing text as a pdf file. In a new project (iOS15) I wanted to reuse this func, but getting a depreciated messages. I don't understand how should I change my code to the new UIWindow.Scene.windows.

The message shows up at two positions in my code (marked <--- here)

--> 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

How could I change my code to be compatible with iOS 15?

func sharePDF(pdf: Data) {
    
    let pdfData = pdf
    let printingDate = Datum()
    
    let temporaryFolder = FileManager.default.temporaryDirectory
    let fileName = "Scan2Clipboard " + printingDate + ".pdf"
    let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
    
    
    
    do {
        try pdfData.write(to: temporaryFileURL)
        
        let vc = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: nil)
        
        if UIDevice.current.userInterfaceIdiom == .pad {
            vc.popoverPresentationController?.sourceView = UIApplication.shared.windows.first <--- here
            vc.popoverPresentationController?.sourceRect = CGRect (
                x: UIScreen.main.bounds.width / 2.1,
                y: UIScreen.main.bounds.height / 2.3,
                width: 300, height: 300)
        }
        
        UIApplication.shared.windows.first?.rootViewController?.present(vc, animated: true, completion: nil) <--- here
        
    } catch {
        print(error)
    }
    
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
Michael
  • 616
  • 5
  • 20
  • https://stackoverflow.com/q/70296066/1187415, https://stackoverflow.com/q/69650504/1187415 – Martin R Jan 12 '22 at 19:15
  • Hi Martin, I've found this two questions asI searched for my problem. But it seems I'm not experienced enough to match ist with my code. I will try to dive deeper... – Michael Jan 12 '22 at 19:23

1 Answers1

2

this has been updated for iOS 15

UIApplication
.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }
.rootViewController?.present()
Squared
  • 308
  • 3
  • 13
  • Thanks for your answer. Do you mean I have to replace my UIApplication.shared.windows.first with your code? I'll get this error message: Value of type 'UIWindow?' has no member 'rootViewController' Do you have another little hint for me? :-) – Michael Jan 12 '22 at 19:01
  • if you are using this function in ViewController then just use this – Squared Jan 12 '22 at 19:13
  • self.view.present() – Squared Jan 12 '22 at 19:15
  • remove the line that showing error in .pad condition with in last line just use present(vc, animation: true, completion: nil) – Squared Jan 12 '22 at 19:20
  • I'm new in UIKit and don't really understand what you mean. Could you please show me your answer in my code if it's possible? – Michael Jan 12 '22 at 19:27
  • do { try pdfData.write(to: temporaryFileURL) let vc = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: nil) if UIDevice.current.userInterfaceIdiom == .pad { vc.popoverPresentationController?.sourceRect = CGRect ( x: UIScreen.main.bounds.width / 2.1, y: UIScreen.main.bounds.height / 2.3, width: 300, height: 300) } present(vc, animation: true, completion: nil) } catch { print(error) } – Squared Jan 12 '22 at 19:33
  • I'll get an error message but I won't bother you. I'll try to understand your solution an repair my code. Perhaps I stand directly in front of the solution an can't see it. Coding is learning :-) – Michael Jan 12 '22 at 19:44
  • not an issue, lets solve this and you can share error if you want – Squared Jan 12 '22 at 19:46
  • I will do so! Thank you – Michael Jan 12 '22 at 19:49