遷移 iOS Consumer SDK 模組化

Consumer SDK for iOS 可讓您使用模組化架構建立共乘應用程式。您可以使用要用於特定應用程式的 API 部分,並與自己的 API 整合。不同功能的 Consumer SDK API 已封裝至不同的模組。

如果您的代僱駕駛服務應用程式使用舊版 Consumer SDK,您必須升級應用程式才能使用這個模組架構。本遷移指南說明如何升級應用程式。

總覽

消費者 SDK 模組架構導入了保留使用者介面狀態的 MapViewSession 物件。在舊版 Consumer SDK 中,應用程式會在狀態之間流動。採用這種模組架構時,您可以建立 MapViewSession 物件,並選擇在地圖上顯示工作階段。如果未顯示工作階段,地圖會顯示與僅使用 Maps SDK for iOS 相同的內容。

MapViewSession 物件代表模組的單一生命週期使用例項。工作階段是模組 API 的存取點。舉例來說,行程分享工作階段會在單趟行程中追蹤。如要監控行程,您必須與 JourneySharingSession 物件互動。

TripModel 個物件

在舊版 Consumer SDK 中,TripService 例項可讓您一次只監控一個即時行程。透過使用 TripModel 物件,您可以監控單一 TripService 例項中的多個即時行程。您可以從 TripService 建立 TripModel 的執行個體,且 TripModel 的每個例項都會繫結至特定行程。

如果已註冊的訂閱者在行程中更新,TripModel 執行個體會呼叫更新事件。

TripModel 物件也可用來建立 JourneySharingSession 物件的執行個體。

MapViewSession 個州

MapViewSession 執行個體一次只能在一個 mapView 中新增,且可處於以下兩種狀態之一:

  • GMTCMapViewSessionStateInactive 狀態表示這個 MapViewSession 尚未新增至任何 mapView,或是已從 mapView 中移除。將 MapViewSession 執行個體從 mapView 中移除後,系統會呼叫 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];

使用 UI 元素 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];

模組化架構程式碼變更

如果您的代僱駕駛服務應用程式使用舊版 Consumer 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];