search for lyrics of a song in in swift

To search for the lyrics of a song in Swift, you can use the Genius API to get the lyrics of a particular song. Here are the steps:

  1. First, you need to get an access token from the Genius API by registering on their website.
  2. Next, install the Alamofire library in your project. You can do this by adding pod 'Alamofire' to your Podfile and running pod install.
  3. Import the Alamofire library in your file by adding import Alamofire.
  4. Create a function that will make a request to the Genius API to search for the lyrics of a particular song. Here's an example:
main.swift
func searchForLyrics(songTitle: String, artistName: String, completion: @escaping (Result<String, Error>) -> Void) {
    let headers: HTTPHeaders = [
        "Authorization": "Bearer YOUR_ACCESS_TOKEN_HERE"
    ]
    let url = "https://api.genius.com/search?q=\(songTitle) \(artistName)".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
    
    AF.request(url, headers: headers).responseJSON { response in
        switch response.result {
        case .success(let value):
            guard let json = value as? [String: Any],
                  let response = json["response"] as? [String: Any],
                  let hits = response["hits"] as? [[String: Any]],
                  let songId = hits.first?["result"]?["id"] as? Int else {
                completion(.failure(NSError(domain: "Unable to parse response", code: 0, userInfo: nil)))
                return
            }
            let lyricsUrl = "https://api.genius.com/songs/\(songId)/lyrics"
            AF.request(lyricsUrl, headers: headers).responseJSON { response in
                switch response.result {
                case .success(let value):
                    guard let json = value as? [String: Any],
                          let response = json["response"] as? [String: Any],
                          let song = response["song"] as? [String: Any],
                          let lyrics = song["lyrics"] as? String else {
                        completion(.failure(NSError(domain: "Unable to parse response", code: 0, userInfo: nil)))
                        return
                    }
                    completion(.success(lyrics))
                case .failure(let error):
                    completion(.failure(error))
                }
            }
        case .failure(let error):
            completion(.failure(error))
        }
    }
}
1849 chars
38 lines
  1. Call the function and pass in the song title and artist name to search for the lyrics. Here's an example:
main.swift
searchForLyrics(songTitle: "Blinding Lights", artistName: "The Weeknd") { result in
    switch result {
    case .success(let lyrics):
        print(lyrics)
    case .failure(let error):
        print(error.localizedDescription)
    }
}
237 chars
9 lines

This function will make a request to the Genius API to search for the lyrics of the song "Blinding Lights" by The Weeknd and print the lyrics to the console. You can modify the function to suit your needs and use it in your project to search for the lyrics of any song.

gistlibby LogSnag