呼叫移动 Api
在Oracle Mobile Hub中,后端是定制 api、存储集合和其他可在应用中使用的资源的逻辑分组。后端还提供了用于访问这些资源的安全上下文。
下面是在您的 iOS 应用程序中使用后端的一般步骤:
-
将客户机 SDK 添加到您的应用程序。
-
填写
OMC.plist
,其中包含后端的环境和验证详细信息。 -
向您的应用程序添加 SDK 调用以加载配置信息。
-
向应用程序添加 SDK 调用以处理验证。
-
添加要使用的任何其他 SDK 调用。
加载后端配置
对于使用 iOS 客户机 SDK 成功完成对Oracle Mobile Hub API 的任何调用,您需要从应用程序的 OMC.plist
文件加载移动后端的配置。可以使用 OMCMobileBackend
类执行以下操作:
/**
* Returns the mobile backend that is configured in OMC.plist file
*/
OMCMobileBackend* mbe = [[OMCMobileManager sharedManager] mobileBackend];
调用平台 Api
在将后端的配置信息加载到应用程序中之后,可以根据 iOS 核心库类调用客户机 SDK 类。
IOS 核心库(libOMCCore.a
)提供以下关键接口:
-
OMCMobileManager
-
OMCMobileBackend
(OMCMobileComponent
的子类) -
OMCCxAAnalyticsApp
(OMCMobileComponent
的子类) -
OMCServiceProxy
SDK 中的根对象为 OMCMobileManager
。OMCMobileManager
管理 OMCMobileBackend
对象。
OMCMobileBackend
对象用于管理应用程序及其关联移动后端之间的连接、验证和其他事务处理,包括对平台 api 的调用以及您定义的任何定制 api。它通过 OMCServiceProxy
的子类(例如 OMCLocation
和 OMCStorage
)管理对平台 api 的调用。
下面是使用 SDK 类调用存储 API 的示例:
#import "OMCMobileBackend.h"
#import "OMCMobileManager.h"
#import "OMCAuthorization.h"
#import "OMCStorage.h"
#import "OMCMobileBackend+OMC_Storage.h"
#import "OMCSynchronization.h"
- (NSData*)dataFromStorageObjectWithID:(NSString*)objectID collectionID:(NSString*)collectionID {
// Get mobile backend
OMCMobileBackend* mbe = [[OMCMobileManager sharedManager] mobileBackend];
// Get storage object
OMCStorage* storage = [mbe storage];
// Get your collection
OMCStorageCollection* collection = [storage getCollection:collectionID];
// Get your object from your collection
OMCStorageObject* object = [collection get:objectID];
// Get the data from payload of your object
NSData* data = [object getPayloadData];
return data; }
}
注 :
您在 Objective-C 中写入的方法(用于 iOS 的Oracle Mobile Hub SDK)也可以映射到 Swift。调用定制 Api
客户机 SDK 提供了 OMCCustomCodeClient
类,用于简化Oracle Mobile Hub中定制 api 的调用。
使用此类时,将在请求有效负载为 JSON 或空且响应有效负载为 JSON 或为空的端点上调用 REST 方法(GET、PUT、POST 或 DELETE)。
此外,还可以提供在请求调用完成时调用的完成处理程序(表示处理程序异步运行)。
如果设置完成处理程序,在完成方法调用后将在 UI (主)线程中调用它,允许更新 UI 项。完成块将包含 JSON 对象的特定于格式的数据,即 NSDictionary
或 NSArray
。对返回的任何数据或错误、HTTP 或系统使用完成块。
所有必需的Oracle Mobile Hub 标头(如 Authorization
)(假定用户已验证)都将自动插入请求中。
使用 OMCCustomCodeClient
可能如下所示:
#import "OMCCore/OMCMobileBackend.h"
#import "OMCCore/OMCCustomCodeClient.h"
...
// A GET, PUT, POST, or DELETE method may be specified here - sent or returned JSON data object may be nil as appropriate.
OMCMobileBackend *backend = [[OMCMobileManager sharedManager] mobileBackend];
OMCCustomCodeClient *ccClient = backend.customCodeClient;
NSDictionary *jsonPayload = @{@"myKey": @"myValue"};
[ccClient invokeCustomRequest: @"API2/endpoint2"
method: "@PUT"
data: jsonPayload,
completion: ^(NSError* error,
NSHTTPURLResponse *response,
id responseData) {
// error will be nil if no problems occurred, otherwise it will contain the error object
// response will be complete HTTP response
// response data will be Map or Array for JSON object if success or nil if error
}];