unsplash / unsplash-photopicker-ios

📱An iOS photo picker to search and download photos from Unsplash.
MIT License
406 stars 85 forks source link

Add support for SwiftUI #21

Closed lisindima closed 4 years ago

lisindima commented 4 years ago

Add support for SwiftUI

ocollet commented 4 years ago

@lisindima I have yet to dive into SwiftUI, so I'm not sure I understand the issue.

  1. Do you mean that you'd like the component to be built with SwiftUI?
  2. Do you mean the component cannot be used in a SwiftUI project?
  3. What would be required to add support?
lisindima commented 4 years ago

Thanks for the answer, I would like to see instructions on how to run this component in swiftui, I'm not sure if this can be done using UIViewControllerRepresentable

ocollet commented 4 years ago

Makes sense, I will see if I can test it this week.

lisindima commented 4 years ago

Hi, I was able to run this component on SwiftUI and it works fine)

import SwiftUI
import UnsplashPhotoPicker

struct UnsplashImagePicker: UIViewControllerRepresentable {

    var urlUnsplashImage = [UnsplashPhoto]()

    let configuration = UnsplashPhotoPickerConfiguration(
        accessKey: "accessKey",
        secretKey: "accessKey",
        allowsMultipleSelection: false)

    func makeUIViewController(context: UIViewControllerRepresentableContext<UnsplashImagePicker>) -> UnsplashPhotoPicker {
        let unsplashPhotoPicker = UnsplashPhotoPicker(configuration: configuration)
        unsplashPhotoPicker.photoPickerDelegate = context.coordinator
        return unsplashPhotoPicker
    }

    func makeCoordinator() -> UnsplashImagePicker.Coordinator {
        return Coordinator(self)
    }

    class Coordinator: UnsplashPhotoPickerDelegate {

        var parent: UnsplashImagePicker

        init(_ parent: UnsplashImagePicker) {
            self.parent = parent
        }

        func unsplashPhotoPicker(_ photoPicker: UnsplashPhotoPicker, didSelectPhotos photos: [UnsplashPhoto]) {
            print(photos)
        }

        func unsplashPhotoPickerDidCancel(_ photoPicker: UnsplashPhotoPicker) {
            print("Unsplash photo picker did cancel")
        }
    }

    func updateUIViewController(_ uiViewController: UnsplashPhotoPicker, context: UIViewControllerRepresentableContext<UnsplashImagePicker>) {

    }
}
lisindima commented 4 years ago

and yet there is a problem with this code, the unsplashPhotoPicker function does not work completely, respectively, it is not possible to select an image and nil always returns

Update: Returning to this problem, I found a small error, now everything works, all the information about the photo is also returned. I updated the code above, suddenly it will be needed!

ocollet commented 4 years ago

Great! Thank you for taking a shot at it! 👏