0

I'm creating an app that plays a sound when a button is clicked. It consists of UIButton to play the sound, UIImageView to display the associated image, and another UIButton which I'm using like a label to describe the button. I want to be able to configure all three parameters so I can change them remotely from Firebase. So far I figured out how to change the label, but I want to be able to change the URL that the sound and image load from. Here is my code:

import UIKit
import Firebase
import AVKit

class FirebaseViewController: UIViewController, AVAudioPlayerDelegate {

    //These variables are for my sound when I click a button
    var firesound1 = AVPlayer()
    var firesound2 = AVPlayer()
    var firesound3 = AVPlayer()
    
    //These outlets reference the labels(UIButton) and UIImageView in the storyboard
    @IBOutlet weak var firelabel1: UIButton!
    @IBOutlet weak var firelabel2: UIButton!
    @IBOutlet weak var firelabel3: UIButton!
    @IBOutlet weak var fireimage1: UIImageView!
    @IBOutlet weak var fireimage2: UIImageView!
    @IBOutlet weak var fireimage3: UIImageView!
    
    func updateViewWithRCValues() {
        
//These remote config options allow me to change the text of the UIButton, which here I'm using like a UILabel
        firelabel1.setTitle(buttonLabel1, for: .normal)
        let buttonLabel2 = RemoteConfig.remoteConfig().configValue(forKey: "label2").stringValue ?? ""
        firelabel2.setTitle(buttonLabel2, for: .normal)
        let buttonLabel3 = RemoteConfig.remoteConfig().configValue(forKey: "label3").stringValue ?? ""
        firelabel3.setTitle(buttonLabel3, for: .normal)
        let url = RemoteConfig.remoteConfig().configValue(forKey: "url1").stringValue ?? ""
        firelabel3.setTitle(buttonLabel3, for: .normal)
    }
   
    func setupRemoteConfigDefaults() {
        let defaultValues = [
        "label1": "" as NSObject,
        "label2": "" as NSObject,
        "label3": "" as NSObject
        ]
        RemoteConfig.remoteConfig().setDefaults(defaultValues)
    }
    
    func fetchRemoteConfig() {
        // Remove this before production!!
        let debugSettings = RemoteConfigSettings(developerModeEnabled: true)
        RemoteConfig.remoteConfig().configSettings = debugSettings!
        
        RemoteConfig.remoteConfig().fetch(withExpirationDuration: 0) { [unowned self] (status, error) in guard error == nil else {
            print ("Error fetching remote values: \(String(describing: error))")
            return
            }
            print("Retrieved values from the cloud")
            RemoteConfig.remoteConfig().activateFetched()
            self.updateViewWithRCValues()
        }
    }

        override func viewDidLoad() {
            super.viewDidLoad()
            setupRemoteConfigDefaults()
            fetchRemoteConfig()
            
//This code loads an image from a url into a UIImageView. I want to be able to configure the url like a parameter so I can change the url from the firebase website.
            let url = URL(string: "https://ichef-1.bbci.co.uk/news/976/media/images/83351000/jpg/_83351965_explorer273lincolnshirewoldssouthpicturebynicholassilkstone.jpg")
            let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
                if (error != nil)
                {
                    print("ERROR")
                }
                    else
                    {
                        var documentsDirectory: String?
                        var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
                        if paths.count > 0
                        {
                            documentsDirectory = paths [0]
                            let savePath = documentsDirectory! + "/ImageOne"
                            
                            FileManager.default.createFile(atPath: savePath, contents: data, attributes: nil)
                            DispatchQueue.main.async
                                {
                                    self.fireimage1.image = UIImage(named: savePath)
                            }
                    }
                }
            }
           task.resume()
    
}
   
    //This code plays the sounds. I also want to be able to configure the url like a parameter.
    @IBAction func soundpressed1(_ sender: Any) {
        
        let sound1 = AVPlayerItem(url: URL(string: "https://firebasestorage.googleapis.com/v0/b/mlg-soundboard-2018-edition.appspot.com/o/hitmarker.mp3?alt=media&token=e5d342d6-4074-4c50-ad9d-f1e41662d9e9")!)
        firesound1 = AVPlayer(playerItem: sound1)
        firesound1.play()
    }

    override func didReceiveMemoryWarning() {
            
    }    
}

Basically I want to be able to swap out the URLs with Remote Config.

picciano
  • 21,170
  • 9
  • 62
  • 81
Adem
  • 49
  • 11
  • There's far too much code in the question. See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). That being said, this should be a pretty straight forward task to attach a Firebase observer to a node, and when they node get's updated (which will send the event & data to your app) just update your UI with that new data. What's the specific issue? – Jay Apr 06 '18 at 17:39
  • I got some help today with the issue, will try to update with an answer later. I was trying to switch out images and sounds using firebase, but it was better to use database and not remote config. – Adem Apr 06 '18 at 17:40

1 Answers1

0

You can either create separate keys in Remote config for Text, Sound URL and Image URL.

Or you can create a key called button_config and supply all the three params in a JSON

button_config = {"text" : "My button label", "sound_url" : "https://foo.com/sound.mp3", "image_url" : "https://foo.com/image.png"}
Mayank Jain
  • 2,537
  • 2
  • 21
  • 18