Swift Importing Music library into my iOS App and have different tableview cells for iTunes music, apple music

Viewed 592

I'm working on an app which should show up all the music in the user's iOS music app, I have tried to get things to work but unfortunately I don't know how to not show apple music songs on the app since I don't have the authorization for it. But still my app pulls up the data and shows all the songs in the tableview. When I tap on a music from apple music my app crashes. I think this is because of the auth.

Please let me know if there are any solutions to not show apple music songs when i request to see the users library.

import UIKit
import MediaPlayer
import AVFoundation

class Tableview: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView : UITableView?
    let myTableView: UITableView = UITableView( frame: CGRect.zero, style: .grouped )

    var albums: [AlbumInfo] = []
    var songQuery: SongQuery = SongQuery()
    var audio: AVAudioPlayer?

    override func viewDidLoad() {

        super.viewDidLoad()
        tableView?.dataSource = self
        tableView?.delegate = self
        self.title = "Music"
        MPMediaLibrary.requestAuthorization { (status) in
            if status == .authorized {
                self.albums = self.songQuery.get(songCategory: "")
                DispatchQueue.main.async {
                    self.tableView?.rowHeight = UITableView.automaticDimension;
                    self.tableView?.estimatedRowHeight = 60.0;
                    self.tableView?.reloadData()
                }
            } else {
                self.displayMediaLibraryError()
            }
        }

    }

    func displayMediaLibraryError() {

        var error: String
        switch MPMediaLibrary.authorizationStatus() {
        case .restricted:
            error = "Media library access restricted by corporate or parental settings"
        case .denied:
            error = "Media library access denied by user"
        default:
            error = "Unknown error"
        }

        let controller = UIAlertController(title: "Error", message: error, preferredStyle: .alert)
        controller.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
        controller.addAction(UIAlertAction(title: "Open Settings", style: .default, handler: { (action) in
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)
            } else {
                // Fallback on earlier versions
            }
        }))
        present(controller, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return albums.count
    }

    func tableView( _ tableView: UITableView, numberOfRowsInSection section: Int ) -> Int  {

        return albums[section].songs.count
    }

    func tableView( _ tableView: UITableView, cellForRowAt indexPath:IndexPath ) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(
            withIdentifier: "MusicPlayerCell",
            for: indexPath) as! MusicPlayerCell
        cell.labelMusicTitle?.text = albums[indexPath.section].songs[indexPath.row].songTitle
        cell.labelMusicDescription?.text = albums[indexPath.section].songs[indexPath.row].artistName
        let songId: NSNumber = albums[indexPath.section].songs[indexPath.row].songId
        let item: MPMediaItem = songQuery.getItem( songId: songId )

        if  let imageSound: MPMediaItemArtwork = item.value( forProperty: MPMediaItemPropertyArtwork ) as? MPMediaItemArtwork {
            cell.imageMusic?.image = imageSound.image(at: CGSize(width: cell.imageMusic.frame.size.width, height: cell.imageMusic.frame.size.height))
        }
        return cell;
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

        return albums[section].albumTitle
    }
   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let songId: NSNumber = albums[indexPath.section].songs[indexPath.row].songId
    let item: MPMediaItem = songQuery.getItem( songId: songId )
   
    
    
    let url: NSURL = item.value( forProperty: MPMediaItemPropertyAssetURL ) as! NSURL
   
    if AVAsset(url: url as URL ) .isPlayable
    {
    do {
        audio = try AVAudioPlayer(contentsOf: url as URL)
        guard let player = audio else { return }

        player.prepareToPlay()
        player.play()
    } catch let error {
        print(error.localizedDescription)
    }
    }
    
    self.title = albums[indexPath.section].songs[indexPath.row].songTitle
  }
}
import Foundation
import MediaPlayer
import AVFoundation
import MediaToolbox


struct SongInfo {

    var albumTitle: String
    var artistName: String
    var songTitle:  String

    var songId   :  NSNumber
}

struct AlbumInfo {

    var albumTitle: String
    var songs: [SongInfo]
}

class SongQuery {

    func get(songCategory: String) -> [AlbumInfo] {

        var albums: [AlbumInfo] = []
         let albumsQuery: MPMediaQuery
        if songCategory == "Artist" {
            albumsQuery = MPMediaQuery.artists()

        } else if songCategory == "Album" {
            albumsQuery = MPMediaQuery.albums()

        } else {
            albumsQuery = MPMediaQuery.albums()
        }


       // let albumsQuery: MPMediaQuery = MPMediaQuery.albums()
        let albumItems: [MPMediaItemCollection] = albumsQuery.collections! as [MPMediaItemCollection]
      //  var album: MPMediaItemCollection

        for album in albumItems {

            let albumItems: [MPMediaItem] = album.items as [MPMediaItem]
           // var song: MPMediaItem

            var songs: [SongInfo] = []

            var albumTitle: String = ""

            for song in albumItems {
                if songCategory == "Artist" {
                    albumTitle = song.value( forProperty: MPMediaItemPropertyArtist ) as! String

                } else if songCategory == "Album" {
                    albumTitle = song.value( forProperty: MPMediaItemPropertyAlbumTitle ) as! String


                } else {
                    albumTitle = song.value( forProperty: MPMediaItemPropertyAlbumTitle ) as! String
                }

                let songInfo: SongInfo = SongInfo(
                    albumTitle: song.value( forProperty: MPMediaItemPropertyAlbumTitle ) as! String,
                    artistName: song.value( forProperty: MPMediaItemPropertyArtist ) as! String,
                    songTitle:  song.value( forProperty: MPMediaItemPropertyTitle ) as! String,
                    songId:     song.value( forProperty: MPMediaItemPropertyPersistentID ) as! NSNumber
                )
                songs.append( songInfo )
            }

            let albumInfo: AlbumInfo = AlbumInfo(

                albumTitle: albumTitle,
                songs: songs
            )

            albums.append( albumInfo )
        }

        return albums

    }

    func getItem( songId: NSNumber ) -> MPMediaItem {

        let property: MPMediaPropertyPredicate = MPMediaPropertyPredicate( value: songId, forProperty: MPMediaItemPropertyPersistentID )

        let query: MPMediaQuery = MPMediaQuery()
        query.addFilterPredicate( property )

        var items: [MPMediaItem] = query.items! as [MPMediaItem]

        return items[items.count - 1]

    }

}
0 Answers
Related