每个Flutter iOS构建中使用不同的Google Maps API密钥

9
据我理解从 google_maps_flutter 文档中,我们需要在 AppDelegate.swift 中声明谷歌地图 API 密钥,如下所示:
GMSServices.provideAPIKey("YOUR KEY HERE")

有没有一种方法可以在每个口味构建中使用不同的API密钥?例如开发和生产各有不同的API密钥。

你也在安卓或者网页上做了吗? - BeniaminoBaggins
1个回答

6
我使用了主动编译条件(Active Compilation Conditions)来解决这个问题:
  1. In Xcode, go to PROJECT -> Runner -> Build Settings and search for 'active compilation conditions'. Add a text value for each different flavor. active compilation conditions

  2. in AppDelegate.swift, add preprocessor conditional statements to execute different code for different flavors, in our case providing a different API key:

    import UIKit
    import Flutter
    import GoogleMaps
    
    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
      ) -> Bool {
    
        // Using active compilation conditions to provide the right API key for the right flavor.
        #if DEV
            GMSServices.provideAPIKey("<API key for DEV project>")
        #elseif QA
            GMSServices.provideAPIKey("<API key for QA project>")
        #elseif PROD
            GMSServices.provideAPIKey("<API key for PROD project>")
        #endif
    
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
    }
    

5
请勿将代码发布为图像,使用文本并格式化为代码块。 - Dijkgraaf
你知道在Android上该做什么吗? - BeniaminoBaggins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接