gpt4 book ai didi

swift - 如何使用远程配置更改字符串?

转载 作者:行者123 更新时间:2023-11-30 11:37:22 24 4
gpt4 key购买 nike

我正在创建一个在单击按钮时播放声音的应用程序。它由用于播放声音的 UIButton、用于显示关联图像的 UIImageView 以及另一个我像标签一样用来描述的 UIButton 组成。按钮。我希望能够配置所有三个参数,以便可以从 Firebase 远程更改它们。到目前为止,我已经弄清楚如何更改标签,但我希望能够更改加载声音和图像的 URL。这是我的代码:

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() {

}
}

基本上我希望能够使用远程配置交换 URL。

最佳答案

您可以在远程配置中为文本、声音 URL 和图像 URL 创建单独的 key 。

或者您可以创建一个名为“button_config”的键,并在 JSON 中提供所有三个参数

button_config = {"text" : "My button label", "sound_url" : "https://foo.com/sound.mp3", "image_url" : "https://foo.com/image.png"}

关于swift - 如何使用远程配置更改字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49602877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com