Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3832

YouTube Data API for iOS application is not functioning, why?

$
0
0

I had been using the YouTube Data APIv3 to allow users to watch Kamala's most recent videos from within the app. It has already received a couple hundred downloads. It (being the video view) no longer functions as expected. It is in a state of "endless loading" now. Furthermore, when I view my API Dashboard it is as though I never used the API to begin with. I have an API key coupled with the channelID. Would anyone know what might be the issue?

Additional reading:

I do not build applications unless they are free of errors, meaning there are no errors (per se) in the code. It is a pretty simple view. The entire application is written in Swift/SwiftUI. I am in school for CompSci and this is my second application. I volunteer and I made this app for that. You know: "there's an app for that." Thanks in advance.

Respectfully,Erica

I tried to launch the app as per usual. I expected to watch her most recent speech, but the video view would not load. There is nothing that I can do except rewrite the view using a deprecated version of WebKit.

This is the code in its entirety:

import AVKitimport SwiftUIimport WebKitstruct Videos: View {    @State private var videos: [Video] = []    @State private var isLoading = true    @State private var selectedVideo: Video?    private let apiKey = "myAPIkey"    private let channelId = "channelIdFromWhichIFetchVideos"    var body: some View {        NavigationView {            ZStack {                if isLoading {                    ProgressView("Loading...")                } else {                    List(videos.prefix(8)) { video in                        VideoRow(video: video)                            .onTapGesture {                                selectedVideo = video // Set the selected video when tapped                            }                    }                    .navigationTitle("Recent Videos")                }            }            .onAppear(perform: fetchVideos)            .sheet(item: $selectedVideo) { video in                VideoPlayerView(videoURL: URL(string: "https://www.youtube.com/embed/\(video.id)")!)            }            .fullScreenCover(item: $selectedVideo) { video in                VideoPlayerView(videoURL: URL(string: "https://www.youtube.com/embed/\(video.id)")!)            }        }    }    private func fetchVideos() {        let urlString = "https://www.googleapis.com/youtube/v3/search?key=\(apiKey)&channelId=\(channelId)&part=snippet,id&order=date&maxResults=8"        guard let url = URL(string: urlString) else { return }        URLSession.shared.dataTask(with: url) { data, response, error in            if let error = error {                print("Error fetching videos: \(error)")                return            }            guard let data = data else { return }            if let jsonString = String(data: data, encoding: .utf8) {                print("JSON Response: \(jsonString)")            }            do {                let decodedResponse = try JSONDecoder().decode(YouTubeResponse.self, from: data)                DispatchQueue.main.async {                    self.videos = decodedResponse.items.compactMap { Video(id: $0.id.videoId, title: $0.snippet.title, thumbnail: $0.snippet.thumbnails.default.url) }                    self.isLoading = false                }            } catch {                print("Error decoding JSON: \(error)")            }        }.resume()    }}struct VideoRow: View {    var video: Video    var body: some View {        HStack {            AsyncImage(url: URL(string: video.thumbnail)) { image in                image                    .resizable()                    .scaledToFit()                    .frame(width: 100, height: 75)            } placeholder: {                ProgressView()            }            VStack(alignment: .leading) {                Text(video.title)                    .font(.headline)                    .lineLimit(2)            }        }    }}struct VideoPlayerView: View {    var videoURL: URL    var body: some View {        WebView1(url: videoURL)            .navigationTitle("Video Player")            .navigationBarTitleDisplayMode(.inline)    }}struct WebView1: UIViewRepresentable {    let url: URL    func makeUIView(context: Context) -> WKWebView {        let webView = WKWebView()        return webView    }    func updateUIView(_ uiView: WKWebView, context: Context) {        uiView.load(URLRequest(url: url))    }}struct YouTubeResponse: Codable {    let items: [VideoItem]}struct VideoItem: Codable {    let id: VideoID    let snippet: Snippet}struct VideoID: Codable {    let videoId: String}struct Snippet: Codable {    let title: String    let thumbnails: Thumbnails}struct Thumbnails: Codable {    let `default`: ThumbnailDetails}struct ThumbnailDetails: Codable {    let url: String}struct Video: Identifiable {    let id: String    let title: String    let thumbnail: String}#Preview {    Videos()}

Viewing all articles
Browse latest Browse all 3832

Trending Articles