UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to call PHPhotoLibrary's presentLimitedLibraryPicker from SwiftUI?

Forums > SwiftUI

What would be the correct way of calling PHPhotoLibrary's presentLimitedLibraryPicker method from SwiftUI?

The method requires a UIViewController, which I don't have in SwiftUI.

I have tried to use UIViewControllerRepresentable in order to create a UIViewController, and it works, but the result is that two View Controllers are presented, the one I create with UIViewControllerRepresentable, and the Limited Library Picker.

Both View Controllers need to be dismissed in order to get to the original screen, which is not desirable.

To sum up the issue I see:

  1. presentLimitedLibraryPicker works by passing a ViewController.
  2. This makes me create and present a dummy ViewController, just so I can call the method.
  3. There's no way to obtain a reference to the Limited Library Picker, also it doesn't offer a delegate. So I can't detect when the Limited Library Picker controller is dismissed.

This is my attempt (It shows the picker, but when you dismiss it, you need to dismiss the extra, dummy, view controller too:

import Foundation
import SwiftUI
import Photos
import PhotosUI

struct TestView: View {

   @State var showLibraryPicker = false

var body: some View {
    NavigationView {
        VStack {
            Button("Open Library Picker") { showLibraryPicker = true }
        }
        .navigationBarTitle("Test", displayMode: .inline)
        .navigationViewStyle(StackNavigationViewStyle())
        .sheet(isPresented: $showLibraryPicker, onDismiss: { print("Dismissed") }) {
            TestLimitedLibraryPicker()
            }
        }
    }
}

struct TestLimitedLibraryPicker: UIViewControllerRepresentable {

     func makeUIViewController(context: Context) -> UIViewController {
         let controller = UIViewController()
         PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: controller)
          return controller
     }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

     }
}

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.