1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

XcodeでAPIキーなどを保存する方法

Last updated at Posted at 2025-01-14

Config.xcconfig を作成

configファイル.png

プロジェクトのInfo.plistに読み込めるように追記する

<key>GOOGLE_API_KEY</key>
<string>$(GOOGLE_API_KEY)</string>

configファイルの読み取り設定.png

Config.xcconfigファイルを読み込めるように登録する

ConfigurationsDebugReleaseConfig.xcconfigファイルを登録する
configファイルの登録.png

読み込み処理の実装例

AppDelegate.swift

//
//  AppDelegate.swift
//  OCR_TestApp
//

import Foundation

import UIKit
import GoogleSignIn

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Initialize sign-in
        if let apikey = Bundle.main.object(forInfoDictionaryKey: "GOOGLE_API_KEY") as? String {
            print("API Key: \(apikey)")
            // Google Sign-Inの初期化
            GIDSignIn.sharedInstance.configuration = GIDConfiguration(clientID: apikey)
            return true
        } else {
            print("API Key not found")
            return false
        }
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance.handle(url)
    }
}

読み込みに使用しているコード

Bundle.main.object(forInfoDictionaryKey: "GOOGLE_API_KEY")
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?