簡體   English   中英

如何使用帶有 Objective-C 而不是 Swift 的適用於 iOS 的 AWS 開發工具包?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM