[英]How to use AWS SDK for iOS with Objective-C instead of Swift?
我按照此 AWS-amplify 教程执行此操作,将基于 Objective-C 的应用程序连接到 AWS S3 文件存储。 它使用 AWS Amplify CLI 和适用于 iOS 的 AWS 开发工具包 (AWSAppSync)。 我的问题是需要将 Swift 代码段添加到 appDelegate 文件中。 但是,我的 appDelegate 是 Objective-C,我不知道正确添加它的最佳方法是什么。
[1] 委托文件中的这个额外的代码片段真的有必要吗? 我只会将图像上传到 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
在目标 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;
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.