我正在按照 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/