【问题标题】:Use AWSMobileClient without `awsconfiguration.json`in iOS在 iOS 中使用没有 `awsconfiguration.json` 的 AWSMobileClient
【发布时间】:2019-05-28 03:39:09
【问题描述】:

我想验证 iOS 设备以通过 Cognito 用户池使用 AppSync/S3 服务。 AWSMobileClient 提供了一些很好的便利,但the initialization 要求您捆绑有一个awsconfiguration.json 文件——我们的应用程序将动态定义该文件。有没有办法手动配置?

【问题讨论】:

    标签: ios aws-amplify


    【解决方案1】:

    当前的解决方案是使用 CLI 中的多环境工作流。 https://aws-amplify.github.io/docs/cli/multienv?sdk=ios


    编辑

    如果 Amplify 团队的多环境工作流程不适合您,您可以创建配置的调试和生产版本,然后创建一个构建阶段,根据您的构建设置复制正确的版本(调试与发布等)。这对我的一个项目非常有效。

    #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
    

    【讨论】:

    • 啊,我想我们现在尽可能接近了!感谢您的发帖,我会接受这个答案——对于我的特定用例,我将在运行时通过 QR 码配置服务,因此仍在寻找另一个解决方案。再次感谢!
    • 我同意。我宁愿通过 JS SDK 允许你做的代码来指定它。
    • @tgk 查看上面的编辑,它可能对你有用。
    【解决方案2】:

    从 AWS iOS SDK 2.11.0(2019 年 9 月 9 日)开始,现在可以在没有 awsconfiguration.json 文件的情况下进行配置。

    它甚至记录在放大文档here

    另请参阅我的answerrelated question

    【讨论】:

    【解决方案3】:

    这是一个具体的解决方案:

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

    要使用它,您可以根据需要拥有任意多个不同的 awsconfiguration-XXX.json 文件,并在运行时使用所需的文件进行初始化:

    let mobileClient = AWSMobileClient(configuration: "awsconfiguration-XXX")
    mobileClient.initialize { (userState, error) in ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-22
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多