AWS Amplify Auth Issues

I've build an app using a tutorial here on the AWS website to build an app and integrate AWS Amplify for authentication etc. I have followed all the steps to the letter, yet when I launch the app I get the following run time error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

It is marked against this line:
Code Block
let hostURL = "https://\(webDomain!)"


webDomain is a variable assigned a value here as follows:
Code Block
let webDomain = infoDictionary?["WebDomain"] as? String


This is in the file AWSMobileClient.swift.

Does anyone have any idea where I might look to fix this? I've googled everything phrase I can think of to no avail!
Thanks for reading,

Mark


That means webDomain is nil.
as webDomain is defined as
Code Block
let webDomain = infoDictionary?["WebDomain"] as? String

there are several possible reasons:
  • infoDictionary itself is nil

  • infoDictionary has no key "WebDomain" (take care of uppercases).

  • infoDictionary["WebDomain"] is not a String

To find out, instrument code and make it more robust:
Code Block
print("infoDictionary", infoDictionary)
print("WebDomain key value", infoDictionary?["WebDomain"])
let webDomain = infoDictionary?["WebDomain"] as? String
print("webDomain", webDomain)
if webDomain == nil {
print("Could not initialize webDomain")
return // quit initialize
}
let hostURL = "https://\(webDomain!)"

And show exactly what you get.
I assume you got the code from here: https ://github. com/aws-amplify/aws-sdk-ios/blob/main/AWSAuthSDK/Sources/AWSMobileClient/AWSMobileClient.swift

If you got the solution here, don't forget to close the thread by marking this answer.
Many thanks for your reply - The following was returned:

infoDictionary nil
WebDomain key value nil
webDomain nil
Could not initialize webDomain


I guess I need to know what on earth info dictionary is and why it isn't populated - I'm a long time SQL Developer but new to XCODE and AWS so I may be asking very obvious questions!

Earlier in the file, the following code seems to set info dictionary:

Code Block
let infoDictionaryMobileClient = AWSInfo.default().rootInfoDictionary["Auth"] as? [String: [String: Any]]
        let infoDictionary: [String: Any]? = infoDictionaryMobileClient?["Default"]?["OAuth"] as? [String: Any]

I'm also getting this exact error and situation. I'm wondering if the BE dev we have working on setting up cognito missed an integration step or if i need to supply this info in an un-obvious way to amplify.
So you have to check how infoDictionary is loaded.
And if you use it in a thread when it is fully loaded.

Add more tests and tell what you get:

Code Block
let infoDictionaryMobileClient = AWSInfo.default().rootInfoDictionary["Auth"] as? [String: [String: Any]]
print("infoDictionaryMobileClient", infoDictionaryMobileClient) // Is it nil ?
print("Default", infoDictionaryMobileClient?["Default"]) // Does "Default" key exist ?
print("OAuth", infoDictionaryMobileClient?["Default"]?["OAuth"]) // Does "OAuth"] exist ?
let infoDictionary: [String: Any]? = infoDictionaryMobileClient?["Default"]?["OAuth"] as? [String: Any]
print("infoDictionary", infoDictionary)

If you get nothing, that means that the test is negative
Code Block
if self.federationProvider == .hostedUI {

I ran into this error when doing this tutorial. It was because I had copied the wrong files into the project at the second step (Module Two, Initialize Amplify at Runtime). Verify that in your project has the correct configuration files included "amplifyconfiguration.json" and "awsconfiguration.json". I accidentally included the amplifytools.xcconfig and ran into the webDomain field being blank, causing the project to crash. Hope this helps someone.

Hi, I have encountered the same issue today while working through step 3 of AWS Front-End Developer Launch Your First Application Tutorial.

After searching through the web, I realized it was because I selected the wrong option for question 1 after running amplify add auth. It should be the second one Select Default configuration with Social Provider instead of the first one Select Default configuration.

I missed the with Social Provider part because I didn't bother to scroll to the right to read everything.

Once I updated my amplify auth using amplify update auth, then amplify push and pod install. I could see my webdomain section in the amplifyconfiguration.json file . (After some searchings, I read from a github discussion that mentioned about the amplifyconfiguration.json file and that webdomain and other info should be listed there.)

So the issue you and I had encountered was due to the missing parts in the amplifyconfiguration.json, which could be updated using amplify update auth.

I have attached a screenshot of my codes to fix this issue. The blue one is the right selection that would create webdomain and other needed info in the amplifyconfiguration.json file.

Once you make the update, your outputs from these steps should be same as the ones from step 3 of AWS Front-End Developer Launch Your First Application Tutorial.

I know my answer to this question comes kind of late, and hopefully you have already figured it out already.

Anyway, I hope this answer will help someone who is trying out AWS's Front-End Developer Launch Your First Application tutorial! It's so easy to miss small details like this when we are trying something new (and I struggled to find the solution too)!

Thanks YuyHong. That was exactly the problem I had.

AWS Amplify Auth Issues
 
 
Q