iOS 消费者 SDK 模块化迁移

借助 iOS 版消费者 SDK,您可以使用 模块化架构您可以根据需要使用 API 的各个部分 供您为特定应用使用,并将它们与您自己的 API 集成。 消费者 SDK 不同功能的 API 被封装到单独的模块中。

如果您的拼车应用使用的是较低版本的消费者 SDK, 您需要升级应用才能使用这种模块化架构。这个 迁移指南介绍了如何升级您的应用。

概览

消费者 SDK 模块化架构引入了 用于保存界面状态的 MapViewSession 对象。在上一个 版本,则应用在各状态之间流动。包含 在此模块化架构中,您需要创建一个 MapViewSession 对象 并选择在地图上显示会话如果没有会话 那么地图显示的内容与仅使用 Maps SDK for iOS。

MapViewSession 对象代表 模块。会话是模块 API 的接入点。例如: “旅程分享”会话会跟踪一次行程。您与之互动 JourneySharingSession 对象来监控行程。

TripModel 对象

在旧版使用方 SDK 中,TripService 实例 一次只能监控一个实时行程。通过使用 TripModel 对象, 您可以在一个 TripService 实例中监控多个实时行程。 可以通过 TripService 创建一个 TripModel 实例, TripModel 的实例绑定到特定行程。

如果注册订阅者位于TripModel 行程更新。

TripModel 对象还可用于创建 JourneySharingSession 对象。

MapViewSession 个州

一个 MapViewSession 实例一次只能在一个 mapView 中添加,并且 处于以下两种状态之一:

  • GMTCMapViewSessionStateInactive 状态表示此 MapViewSession 尚未添加到任何 mapView,或已从 mapView 中移除。 从 mapView 中移除 MapViewSession 实例后,系统会调用 didRemoveFromMapView 方法。

    调用 hideMapViewSessionhideAllMapViewSessions 方法可从 mapView 中移除 mapViewSession

    [_mapView hideMapViewSession:mapViewSessionA];
    

    [_mapView hideAllMapViewSessions];
    
  • GMTCMapViewSessionStateActive 状态表示此 MapViewSession 已被 添加到了 mapView。如果将 MapViewSession 的实例添加到 mapView,系统会调用 didAddToMapView 方法。 调用 showMapViewSession 方法可将 mapViewSession 添加到目标 mapView

    [_mapView showMapViewSession:mapViewSessionA];
    

使用仅数据组件和界面组件

您可以使用仅含数据的组件来创建拼车应用 或按需乘车和送货解决方案提供的界面元素 API。

使用仅包含数据的组件

如需使用纯流量组件创建拼车应用,请执行以下操作:

  1. 通过指定提供方 ID 和访问令牌提供方来初始化 GMTCServices 对象。
  2. GMTCServices 对象的共享实例获取 tripService 属性。
  3. 使用 tripService 对象的 tripModelForTripName 方法为指定的行程创建或检索 GMTCTripModel 对象实例。
  4. GMTCTripModel 实例注册回调以开始监控行程。

以下示例展示了如何使用仅包含数据的组件:

[GMTCServices setAccessTokenProvider:[[AccessTokenProvider alloc] init]
                          providerID:yourProviderID];
GMTCTripService *tripService = [GMTCServices sharedServices].tripService;

// Create a tripModel instance for listening to updates to the trip specified by this trip name.
GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];

// Register for the trip update events.
[tripModel registerSubscriber:self];

// To stop listening for the trip update.
[tripModel unregisterSubscriber:self];

使用界面元素 API

按照此流程创建包含按需乘车和送货解决方案的消费者应用 界面元素 API:

  1. 通过指定提供方 ID 和访问令牌提供方来初始化 GMTCServices 对象。
  2. 初始化用于渲染基本地图的 GMTCMapView 对象。
  3. GMTCServices 对象的共享实例获取 tripService 属性。
  4. 使用 tripService 对象的 tripModelForTripName 方法为指定的行程创建或检索 GMTCTripModel 对象实例。
  5. 使用 GMTCTripModel 实例创建一个 GMTCJourneySharingSession 对象。
  6. mapView 上显示 GMTCJourneySharingSession 对象。
  7. GMTCTripModel 实例注册回调以开始监控行程。

以下示例展示了如何使用界面 API:

[GMTCServices setAccessTokenProvider:[[AccessTokenProvider alloc] init]
                          providerID:yourProviderID];
GMTCTripService *tripService = [GMTCServices sharedServices].tripService;

// Create a tripModel instance for listening to updates to the trip specified by this trip name.
GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];
GMTCJourneySharingSession *journeySharingSession =
  [[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];

// Add the journeySharingSession instance on the mapView for UI updating.
[self.mapView showMapViewSession:journeySharingSession];

// Register for the trip update events.
[tripModel registerSubscriber:self];

// To remove the JourneySharingSession from the mapView:
[self.mapView hideMapViewSession:journeySharingSession];

// To stop listening for the trip update.
[tripModel unregisterSubscriber:self];

模块化架构代码更改

如果您的拼车应用使用的是较低版本的消费者 SDK, 更新后的模块化架构需要对您的代码进行一些更改。 本部分介绍了其中的部分更改。

行程监控

更新后的模块化架构需要更改这两者的代码 数据层用户和用户界面用户

在早期版本中,数据层用户可能负责处理 监控行程:

GRCTripRequest *tripRequest =
    [[GRCTripRequest alloc] initWithRequestHeader:[GRSRequestHeader defaultHeader]
                                         tripName:tripName
                          autoRefreshTimeInterval:1];
GRCTripService *tripService = [GRCServices sharedServices].tripService;
[tripService registerServiceSubscriber:self];
[tripService setActiveTripWithRequest:tripRequest];

如果采用模块化架构,数据层用户将可以使用 以下代码:

GMTCTripService *tripService = [GMTCServices sharedServices].tripService;
GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];
tripModel.options.autoRefreshTimeInterval = 1;
[tripModel registerSubscriber:self];

在早期版本中,用户界面用户可处理行程监控 使用以下代码:

// Show the Journey Sharing user interface.
[self.mapView startTripMonitoring];

// Hide the Journey Sharing user interface.
[self.mapView resetCustomerState];

使用模块化架构时,界面用户应该使用 以下代码:

// Show the Journey Sharing user interface.
GMTCJourneySharingSession *journeySharingSession =
  [[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];
[self.mapView showMapViewSession:journeySharingSession];

// Hide the Journey Sharing user interface.
[self.mapView hideMapViewSession:journeySharingSession];