ios - 如何将适用于 iOS 的 AWS 开发工具包与 Objective-C 而不是 Swift 一起使用?

标签 ios objective-c swift amazon-web-services amazon-s3

我正在按照 this AWS-amplify tutorial 执行此操作,将我的基于 Objective-C 的应用程序连接到 AWS S3 文件存储。 .它使用 AWS Amplify CLI 和适用于 iOS 的 AWS 开发工具包 (AWSAppSync)。我的问题是需要将 Swift 代码段添加到 appDelegate 文件中。但是,我的 appDelegate 是 Objective-C,我不知道正确添加它的最佳方法是什么。

[1] 委托(delegate)文件中的这个额外代码片段真的有必要吗?我只会将图像上传到 S3 文件存储。

[2] 如有必要,最好的解决方法是什么? (制作一个单独的 swift 文件并将其桥接到 appDelegate 文件感觉有点太 hacky)

这是片段:

import AWSAppSync

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var appSyncClient: AWSAppSyncClient?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        do {
            // You can choose the directory in which AppSync stores its persistent cache databases
            let cacheConfiguration = try AWSAppSyncCacheConfiguration()

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

到目前为止,我已经尝试过使用这段代码:
#import <AWSAppSync/AWSAppSync.h>
#import <AWSCore/AWSCore.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType credentialsProvider:credentialsProvider];
    AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
}

但我得到错误:
Use of undeclared identifier 'DefaultServiceRegionType'
Use of undeclared identifier 'credentialsProvider'

最佳答案

您可以使用 AWSCore
在 objective-c 应用程序委托(delegate)中导入 AWSCore header 。

@import AWSCore;

并在
application:didFinishLaunchingWithOptions:

应用程序委托(delegate)方法。
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:CognitoRegionType
                                                                                                identityPoolId:CognitoIdentityPoolId];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType
                                                                     credentialsProvider:credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

为您正在使用的服务(可能在您的 View Controller 内)导入适当的 header 。头文件导入约定为@import AWSServiceName;作为
@import AWSS3;
@import AWSDynamoDB;
@import AWSSQS;
@import AWSSNS;
@import AWSCognito; 

并实现对 AWS 服务的调用。
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = yourBucket;
uploadRequest.key = yourKey;
uploadRequest.body = yourDataURL;
uploadRequest.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];

[[transferManager upload:uploadRequest] continueWithBlock:^id(AWSTask *task) {
    // Do something with the response
    return nil;
}];

关于ios - 如何将适用于 iOS 的 AWS 开发工具包与 Objective-C 而不是 Swift 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55361930/

相关文章:

ios - 自定义 UITableViewCell 中的 UITextField 委托(delegate)不会在第一次加载时被调用

ios - 在 iOS 9 中自定义 ContactPicker UI

覆盖方法的 Objective-C 约定

ios - react-native 错误 RCTJSONStringify() 遇到以下错误 : Invalid type in JSON write (NSURL)

iOS 试图解决严重的崩溃问题

ios - 如何快速更改标签栏未选中图标的颜色?

swift - 在 Swift 中存储按下的按钮

swift - 这是什么语法 : func funcName(stuff1)(stuff2)->returnType {}

ios - 使用 Cocoapods 在 Swift 中导入框架时出现问题

ios - 在 Swift 4 中以编程方式获取信号强度