Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling API key from Info.plist in Swift

Tags:

ios

swift

i am pretty new in IOS development and currently learning about basic stuff related to IOS development. I recently see an article about storing your API key in info.plist so that it can store more securely, I followed the instruction but looks like I made a mistake, so what did I do wrong? if there's any good resources please do comment bellow, thanks

How I call my API key in view in swiftUI

var body: some View {
        ZStack{
            ScrollView{
                VStack(spacing:0){
                    //MARK: Top Content of HomeView
                    // This view represent a content located on the very top of homeview
                    HomeViewTopContent()
                    Text("API KEY : \(Bundle.main.infoDictionary?["API_KEY"]  as? String ?? "API KEY GA KE FETCH")")
                    //MARK: Main content
                    List{
                        switch self.ViewState {
                        case .load:
                            ListItemShimmer()
                        case .done:
                            ForEach(0..<5){_ in
                                ListItemShimmer()
                            }
                            .animation(.linear)
                        case .failed:
                            Text("Data not loaded, please do refresh")
                        }
                    }
                    .frame(width: UIScreen.width, height: UIScreen.height)
                    Spacer()
                }
            }
        }
        .frame(width: UIScreen.width, height: UIScreen.height)
        .ignoresSafeArea()
    }

How I store my API key API Key in Infoplist

Result Result

like image 839
Farhandika Avatar asked Oct 17 '25 04:10

Farhandika


1 Answers

In your code, you're looking for "API_KEY", but in your Info.plist, you've defined it as "API Key" -- note the capitalization difference, underscore, etc. -- it's important that there is an exact match.

Change the key in your Info.plist to API_KEY and your code will work as expected.

like image 186
jnpdx Avatar answered Oct 18 '25 21:10

jnpdx