I'm trying to connect an existing AppSync API to my iOS app.
To perform this, I followed the instructions on this page: https://docs.amplify.aws/cli/graphql-transformer/codegen#ios-usage, but instead of performing:

amplify add api
amplify push

I added my existing AppSync API:

amplify add codegen --apiId XXXXXXXXXXXXXXX
amplify codegen

After running those commands, I added awsconfiguration.json and API.swift to my project.

After performing the setup, I initialized the AppSync client in the AppDelegate file:

import AWSAppSync

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var appSyncClient: AWSAppSyncClient?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        do {
            // You can choose your database location if you wish, or use the default
            let cacheConfiguration = try AWSAppSyncCacheConfiguration()

            // AppSync configuration & client initialization
            let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig: AWSAppSyncServiceConfig(), cacheConfiguration: cacheConfiguration)
            appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
        } catch {
            print("Error initializing appsync client. \(error)")
        }
    }
}

It's worth mentioning that I'm using AWS SDK (for DynamoDB using Object Mapper) in my app (the previous SDK, not Amplify yet)

The problem is that when I'm running the app, it immediately crashes.
This is the log message that comes along with the crash:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The service configuration is `nil`. You need to configure `Info.plist` or set `defaultServiceConfiguration` before using this method.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23c7127e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff513fbb20 objc_exception_throw + 48
    2   AWSDynamoDB                         0x000000010be77baa __54+[AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper]_block_invoke + 474
    3   libdispatch.dylib                   0x000000010cb04d48 _dispatch_client_callout + 8
    4   libdispatch.dylib                   0x000000010cb06297 _dispatch_once_callout + 66
    5   AWSDynamoDB                         0x000000010be779a4 +[AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper] + 84
    6   App                                 0x000000010a7d0f9c $s22App10GlobalVarsCACycfc + 140
    7   App                                 0x000000010a7ca97c $s22App10GlobalVarsCACycfC + 60
    8   App                                 0x000000010a7fd7e1 $s22App21LoadingViewControllerC5coderACSgSo7NSCoderC_tcfc + 113
    9   App                                 0x000000010a7fd943 $s22App21LoadingViewControllerC5coderACSgSo7NSCoderC_tcfcTo + 51
    10  UIKitCore                           0x00007fff47cf7268 -[UIClassSwapper initWithCoder:] + 2427
    11  UIFoundation                        0x00007fff4753d872 UINibDecoderDecodeObjectForValue + 744
    12  UIFoundation                        0x00007fff4753db19 UINibDecoderDecodeObjectForValue + 1423
    13  UIFoundation                        0x00007fff4753d57d -[UINibDecoder decodeObjectForKey:] + 251
    14  UIKitCore                           0x00007fff47cf6531 -[NSCoder(UIIBDependencyInjectionInternal) _decodeObjectsAndTrackChildViewControllerIndexWithParent:forKey:] + 298
    15  UIKitCore                           0x00007fff47a0e65d -[UIViewController initWithCoder:] + 1419
    16  UIKitCore                           0x00007fff4795cc16 -[UINavigationController initWithCoder:] + 65
    17  UIKitCore                           0x00007fff47cf7268 -[UIClassSwapper initWithCoder:] + 2427
    18  UIFoundation                        0x00007fff4753d872 UINibDecoderDecodeObjectForValue + 744
    19  UIFoundation                        0x00007fff4753d57d -[UINibDecoder decodeObjectForKey:] + 251
    20  UIKitCore                           0x00007fff47cfb56f -[UIRuntimeConnection initWithCoder:] + 125
    21  UIFoundation                        0x00007fff4753d872 UINibDecoderDecodeObjectForValue + 744
    22  UIFoundation                        0x00007fff4753db19 UINibDecoderDecodeObjectForValue + 1423
    23  UIFoundation                        0x00007fff4753d57d -[UINibDecoder decodeObjectForKey:] + 251
    24  UIKitCore                           0x00007fff47cf6336 -[NSCoder(UIIBDependencyInjectionInternal) _decodeObjectsWithSourceSegueTemplate:creator:sender:forKey:] + 450
    25  UIKitCore                           0x00007fff47cf90a5 -[UINib instantiateWithOwner:options:] + 1145
    26  UIKitCore                           0x00007fff481f8f24 -[UIStoryboard __reallyInstantiateViewControllerWithIdentifier:creator:storyboardSegueTemplate:sender:] + 279
    27  UIKitCore                           0x00007fff481f8dcf -[UIStoryboard _instantiateViewControllerWithIdentifier:creator:storyboardSegueTemplate:sender:] + 97
    28  UIKitCore                           0x00007fff48091ebf -[UIApplication _loadMainStoryboardFileNamed:bundle:] + 167
    29  UIKitCore                           0x00007fff48092473 -[UIApplication _loadMainInterfaceFile] + 274
    30  UIKitCore                           0x00007fff48090dff -[UIApplication _runWithMainScene:transitionContext:completion:] + 964
    31  UIKitCore                           0x00007fff477c576d -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:] + 122
    32  UIKitCore                           0x00007fff47cb44c1 _UIScenePerformActionsWithLifecycleActionMask + 83
    33  UIKitCore                           0x00007fff477c627f __101-[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]_block_invoke + 198
    34  UIKitCore                           0x00007fff477c5c8e -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:] + 296
    35  UIKitCore                           0x00007fff477c60ac -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 818
    36  UIKitCore                           0x00007fff477c5941 -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:] + 345
    37  UIKitCore                           0x00007fff477c9f3f __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke_2 + 178
    38  UIKitCore                           0x00007fff47bd8c83 +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:actions:completion:] + 865
    39  UIKitCore                           0x00007fff47cd2dff _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 240
    40  UIKitCore                           0x00007fff477c9c5a __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 153
    41  UIKitCore                           0x00007fff47cd2d02 _UISceneSettingsDiffActionPerformActionsWithDelayForTransitionContext + 84
    42  UIKitCore                           0x00007fff477c9ac8 -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:] + 381
    43  UIKitCore                           0x00007fff476206e7 __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke + 657
    44  UIKitCore                           0x00007fff4761f26c -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:] + 248
    45  UIKitCore                           0x00007fff47620411 -[UIScene scene:didUpdateWithDiff:transitionContext:completion:] + 210
    46  UIKitCore                           0x00007fff4808f599 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 535
    47  UIKitCore                           0x00007fff47bfa7f5 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 361
    48  FrontBoardServices                  0x00007fff365d6165 -[FBSSceneImpl _callOutQueue_agent_didCreateWithTransitionContext:completion:] + 442
    49  FrontBoardServices                  0x00007fff365fc4d8 __86-[FBSWorkspaceScenesClient sceneID:createWithParameters:transitionContext:completion:]_block_invoke.154 + 102
    50  FrontBoardServices                  0x00007fff365e0c45 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 220
    51  FrontBoardServices                  0x00007fff365fc169 __86-[FBSWorkspaceScenesClient sceneID:createWithParameters:transitionContext:completion:]_block_invoke + 355
    52  libdispatch.dylib                   0x000000010cb04d48 _dispatch_client_callout + 8
    53  libdispatch.dylib                   0x000000010cb07cb9 _dispatch_block_invoke_direct + 300
    54  FrontBoardServices                  0x00007fff3662237e __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 30
    55  FrontBoardServices                  0x00007fff3662206c -[FBSSerialQueue _queue_performNextIfPossible] + 441
    56  FrontBoardServices                  0x00007fff3662257b -[FBSSerialQueue _performNextFromRunLoopSource] + 22
    57  CoreFoundation                      0x00007fff23bd4471 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    58  CoreFoundation                      0x00007fff23bd439c __CFRunLoopDoSource0 + 76
    59  CoreFoundation                      0x00007fff23bd3b74 __CFRunLoopDoSources0 + 180
    60  CoreFoundation                      0x00007fff23bce87f __CFRunLoopRun + 1263
    61  CoreFoundation                      0x00007fff23bce066 CFRunLoopRunSpecific + 438
    62  GraphicsServices                    0x00007fff384c0bb0 GSEventRunModal + 65
    63  UIKitCore                           0x00007fff48092d4d UIApplicationMain + 1621
    64  App                                 0x000000010a841728 main + 72
    65  libdyld.dylib                       0x00007fff5227ec25 start + 1
    66  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 
1

There are 1 answers

0
Ido Naveh On BEST ANSWER

Apparently, because I'm using the old version of AWS SDK for iOS, I couldn't use both awsconfiguration.json file and the AWS section in Info.plist file.

In order to solve it, I removed the awsconfiguration.json file from Build Phases -> Copy Bundle Resources, and added the AppSync API details from that file to the Info.plist file:

<key>AWS</key>
<dict>
    <key>AppSync</key>
    <dict>
        <key>Default</key>
        <dict>
            <key>ApiUrl</key>
            <string>https://XXXXXXXXXXXXX.appsync-api.xx-xxxx-x.amazonaws.com/graphql</string>
            <key>Region</key>
            <string>xx-xxxx-x</string>
            <key>AuthMode</key>
            <string>API_KEY</string>
            <key>ApiKey</key>
            <string>XXXXXXXXXXXXX</string>
            <key>ClientDatabasePrefix</key>
            <string>XXXXXXXX_API_KEY
        </dict>
    </dict>
    ...
</dict>