YouTube SwiftUI maxResults

How can I display maxResults in the YouTube SwiftUI app?

    AF.request(url,
               parameters: ["part": "snippet","playlistId":Constants.PLAYLIST_ID,"key":Constants.API_KEY]

This url returns " https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLqXdpeEcVg9soE6Ko7VlPbOa_mcmdU5Sr&key=[YOUR_API_KEY]"

I need to return ’ https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=999&playlistId=PLqXdpeEcVg9soE6Ko7VlPbOa_mcmdU5Sr&key=[YOUR_API_KEY]

I cannot get this to work please help

    guard let url = URL(string: "\(Constants.API_URL)/playlistItems?part=snippet&maxResults=49&playlistId=PLqXdpeEcVg9soE6Ko7VlPbOa_mcmdU5Sr&key=\(Constants.API_KEY)") else {
        return
    }

This should work but in the console is says " The data couldn’t be read because it is missing."

and if I simply remove “&maxResults=49” it runs perfectly and returns 5 videos

Thanks for providing all these details! Although, next time I suggest having your two lower comments as the original post. You can always edit the original post if you forgot something later.

It looks like you forgot to add the parameter in your AF.request. Perhaps, add it there, and it will fix things:

AF.request(url,
               parameters: ["part": "snippet", "maxResults": "49","playlistId":Constants.PLAYLIST_ID,"key":Constants.API_KEY]

Otherwise, another way to call the API, would be to build your URL with URLComponents. This is how you would likely call it:

 var urlComponents = URLComponents(string: Constants.apiUrl)
        
// Add the query parameters
urlComponents?.queryItems = [
            URLQueryItem(name: "part", value: "snippet"),
            URLQueryItem(name: "maxResults", value:  "49"),
            URLQueryItem(name: "playlistId", value:  String(Constants.PLAYLIST_ID)),
            URLQueryItem(name: "key", value: Constants.API_KEY)
]

let url = urlComponents?.url
        
// If the url is not nil, then proceed 
   if let url = url {
   // Call YouTube API here
}        

That’s a little cleaner way to build this long URL string. From there, you can print(url) to see if it generates the response you want. You can see an example of how I call an API using the same method here starting at line 111: City-Sights-App/ContentModel.swift at master · agholson/City-Sights-App · GitHub

If that still doesn’t work, then I would suggest firing up Proxyman. This proxies your requests, so you can see all of the network requests made by your app. See here for details on how to use it: GitHub - agholson/City-Sights-App

Hope this gets you moving in the right direction!

Cheers,
Andrew

1 Like