stleamist / BetterSafariView

A better way to present a SFSafariViewController or start a ASWebAuthenticationSession in SwiftUI.
MIT License
581 stars 57 forks source link

BetterSafariView Opens URL Twice #48

Closed aokj4ck closed 10 months ago

aokj4ck commented 10 months ago

Description

BetterSafariView is opened twice on the same URL.

Project Configuration

Xcode 15.1 targeting iOS 17.2. The containing view does not have a navigation stack.

Sample Project

SafariDemo.zip

Demo Video

https://github.com/stleamist/BetterSafariView/assets/384288/c1dcc68b-26fe-40c8-8659-7aa6aa9330a3

Sample Code

The relevant code is in this listing.

import SwiftUI
import BetterSafariView

enum UseCaseButtons: CaseIterable, Identifiable {
    var id: Self { self }

    case networkKit
    case betterSafariView
    case reviewKit
    case urlEncodedForm

    var url: URL {
        switch self {
        case .networkKit:
            URL(string: "https://swiftpackageindex.com/sabapathyk7/NetworkKit").unsafelyUnwrapped
        case .betterSafariView:
            URL(string: "https://swiftpackageindex.com/stleamist/BetterSafariView").unsafelyUnwrapped
        case .urlEncodedForm:
            URL(string: "https://swiftpackageindex.com/interactord/URLEncodedForm").unsafelyUnwrapped
        case .reviewKit:
            URL(string: "https://swiftpackageindex.com/FlineDev/ReviewKit").unsafelyUnwrapped
        }
    }

    var title: String {
        switch self {
        case .networkKit:
            "NetworkKit"
        case .betterSafariView:
            "BetterSafariView"
        case .reviewKit:
            "ReviewKit"
        case .urlEncodedForm:
            "URLEncodedForm"
        }
    }
}

struct ContentView: View {
    @State var selectedUseCase: UseCaseButtons?

    var body: some View {
        ScrollView {
            VStack {
                ForEach(UseCaseButtons.allCases) { message in
                    Button(message.title) {
                        selectedUseCase = message
                    }
                }
                .padding()
                .safariView(item: $selectedUseCase) { useCase in
                    SafariView(url: useCase.url)
                }
            }
        }
    }
}

#Preview {
    ContentView()
}
aokj4ck commented 10 months ago

This was as scoping issue, applying the safariView to a parent (the ScrollView) instead of the ForEach fixes the problem.