Skip to main content
Solved

How to initialize the API correctly in SwiftUI?

  • 29 October 2022
  • 2 replies
  • 532 views

I initialized the API using the following methodology 

https://www.docs.developers.amplitude.com/data/sdks/ios/#install

I connect API_KEY through Environment.

import UIKit
import Amplitude

class AppDelegate: NSObject, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: UIApplication.LaunchOptionsKey : Any]?) -> Bool {
// Enable sending automatic session events
Amplitude.instance().trackingSessionEvents = true
// Initialize SDK
Amplitude.instance().initializeApiKey("API_KEY")
// Set userId
Amplitude.instance().setUserId("userId")
// Log an event
Amplitude.instance().setServerZone(AMPServerZone.EU)
return true
}
}
import Amplitude

@main
struct easylearnApp: App {

let store: IAPStore
var locale = LocalizationManager()

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject private var firebaseController = FirebaseController()

........

}

I get an error when I run the application:
ERROR: Connection response received:400, invalid_api_key

The key is correct.

What can I do about it?

Thank you in advance for your answers.
Max

2 replies

Userlevel 5
Badge +5

Hi @mahus

Thank you for writing this post!

I can see you have set “setServerZone(AMPServerZone.EU)” in your code,  but this is only for our EU orgs. Could you please check if the url to your org has “eu” in it? If not, your org is located in our US Data Center, then you should removed the EU server zone option from your code.

Just to give you an example, my test org is located in US DC: https://analytics.amplitude.com/yuanyuan/home

Any EU org will have an URL like this: https://analytics.eu.amplitude.com/xxx

I hope this helps!

 

Hi Yuanyuan,

Thanks for the answer, but I still get the same error when I initialize the application.

It's something else.

Reply