如何使用AWS SDK for iOS与Objective-C而不是Swift一起使用?
我通过以下方式将基于Objective-C的应用程序连接到AWS S3文件存储。它使用AWS Amplify CLI和AWS SDK for iOS(AWSAPSync)。我的问题是需要将Swift代码段添加到appDelegate文件中。然而,我的appDelegate是Objective-C,我不知道正确添加它的最佳方式是什么 [1] 委托文件中的这个额外代码段真的有必要吗?我只想将图像上传到S3文件存储 [2] 如有必要,最好的解决方法是什么?(制作一个单独的swift文件并将其连接到appDelegate文件感觉有点太粗糙了) 以下是片段:如何使用AWS SDK for iOS与Objective-C而不是Swift一起使用?,ios,objective-c,swift,amazon-web-services,amazon-s3,Ios,Objective C,Swift,Amazon Web Services,Amazon S3,我通过以下方式将基于Objective-C的应用程序连接到AWS S3文件存储。它使用AWS Amplify CLI和AWS SDK for iOS(AWSAPSync)。我的问题是需要将Swift代码段添加到appDelegate文件中。然而,我的appDelegate是Objective-C,我不知道正确添加它的最佳方式是什么 [1] 委托文件中的这个额外代码段真的有必要吗?我只想将图像上传到S3文件存储 [2] 如有必要,最好的解决方法是什么?(制作一个单独的swift文件并将其连接到ap
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;
}
您可以使用
AWSCore
在objective c应用程序委托中导入AWSCore标题
@import AWSCore;
并在中创建服务配置
application:didFinishLaunchingWithOptions:
应用程序委托方法
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:CognitoRegionType
identityPoolId:CognitoIdentityPoolId];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType
credentialsProvider:credentialsProvider];
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;
为正在使用的服务(可能在视图控制器内)导入适当的标头。头文件导入约定为@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;
}];
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;
}];