1

我正在尝试将现有 AppSync API 连接到我的 iOS 应用程序。
要执行此操作,我按照此页面上的说明操作:https ://docs.amplify.aws/cli/graphql-transformer/codegen#ios-usage ,但没有执行:

amplify add api
amplify push

我添加了现有的 AppSync API:

amplify add codegen --apiId XXXXXXXXXXXXXXX
amplify codegen

运行这些命令后,我将awsconfiguration.json和添加API.swift到我的项目中。

执行设置后,我在 AppDelegate 文件中初始化了 AppSync 客户端:

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)")
        }
    }
}

值得一提的是,我在我的应用程序中使用了 AWS SDK(对于使用 Object Mapper 的 DynamoDB)(以前的 SDK,还没有 Amplify)

问题是,当我运行该应用程序时,它会立即崩溃。
这是与崩溃一起出现的日志消息:

*** 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) 
4

1 回答 1

2

显然,因为我使用的是旧版本的 AWS SDK for iOS,我不能同时使用awsconfiguration.json文件和文件中的AWS部分Info.plist

为了解决它,我awsconfiguration.jsonBuild Phases->中删除了该文件,并将该文件Copy Bundle Resources中的 AppSync API 详细信息添加到该Info.plist文件中:

<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>
于 2020-10-21T10:07:04.047 回答