9

I'd like to authenticate an iOS device to use AppSync/S3 services via Cognito user pools. The AWSMobileClient provides some nice conveniences but the initialization requires that you're bundle have an awsconfiguration.json file -- which our application will define dynamically. Is there a way to configure that manually?

tgk
  • 3,857
  • 2
  • 27
  • 42

3 Answers3

8

The current solution is to use the multi-environment workflow from the CLI. https://aws-amplify.github.io/docs/cli/multienv?sdk=ios


Edit

If the multi-environment workflow from the Amplify team doesn't work for you, what you can do is create debug and prod versions of your config, and then create a build phase that copies the correct one based on your build settings (debug vs release, etc). This is working extremely well for one of my projects.

Config files

Build phases

#export; #Prints list of all xcode variables with values
printf "$CONFIGURATION\n";

if [ "$CONFIGURATION" = "Debug" ]; then
printf "creating debug configuration";
cp -r "$PROJECT_DIR/awsconfiguration-debug.json" "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/awsconfiguration.json"
else 
printf "creating production configuration";
cp -r "$PROJECT_DIR/awsconfiguration-prod.json" "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app/awsconfiguration.json"
fi
Mongo
  • 2,674
  • 1
  • 20
  • 22
  • 1
    Ah, bummer as close as we can get right now I suppose! Thanks for posting, I'll accept this answer -- for my particular use-case, I am going to configure services via QR code at runtime so still looking for another solution. Thanks again! – tgk Apr 08 '19 at 23:54
  • I agree. I'd much rather specify it via code like the JS SDK allows you to do. – Mongo Apr 09 '19 at 15:26
  • @tgk check out the edit above, it may work for you. – Mongo May 07 '19 at 21:45
3

As of AWS iOS SDK 2.11.0 (9th September 2019) it is now possible to configure without an awsconfiguration.json file.

It's even documented in the amplify documentation here

See also my answer to a related question

Nick Ager
  • 1,227
  • 14
  • 15
  • For anyone looking for android, see https://stackoverflow.com/questions/56771885/how-to-setup-different-configurationawsconfiguration-json-with-awsmobileclient/58184476#answer-63284523. – CamHart Aug 12 '21 at 18:21
  • Anything specifically for flutter? – sj_959 Jun 23 '22 at 16:31
1

Here's a specific solution:

extension AWSMobileClient {
    convenience init?(configuration url: URL) {
        guard let data = try? Data(contentsOf: url) else { return nil }
        guard let dict = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] else { return nil }

        self.init(configuration: dict)
    }

    convenience init?(configuration name: String) {
        guard let url = Bundle.main.url(forResource: name, withExtension: "json") else {
            return nil
        }

        print("INITIALIZING AWSMobileClient [\(name)]")
        self.init(configuration: url)
    }
}

To use it, you can have as many different awsconfiguration-XXX.json files as you need, and at runtime you initialize with the one you want by:

let mobileClient = AWSMobileClient(configuration: "awsconfiguration-XXX")
mobileClient.initialize { (userState, error) in ... }
biomiker
  • 3,108
  • 1
  • 29
  • 26