Amazon Location の初期コードを設定する
このページでは、Amazon Location Service と統合するサンプル iOS アプリケーションの初期コードを示します。このコードをベースに、マップ、ジオコーディング、ジオフェンシング、追跡、ルーティングなどの機能を活用した、位置情報対応の iOS アプリケーションをビルドできます。
アプリケーションで位置情報に対するアクセス許可を有効にする
Xcode プロジェクトを開きます。
プロジェクトの
Info.plist
ファイルを見つけます。アプリケーションの要件に基づいて、位置情報に対するアクセス許可に必要なキーを追加します。これらのキーは以下のとおりです。
NSLocationWhenInUseUsageDescription
: アプリケーションが使用時に位置情報へのアクセスを必要とする理由の説明。NSLocationAlwaysAndWhenInUseUsageDescription
: アプリケーションが継続的な位置情報へのアクセスを必要とする理由の説明。
次は、アプリケーションでリソース値を設定する必要があります。Config.xcconfig
という名前の新しいファイルを追加し、Amazon コンソールで先ほど作成した値を入力します。
REGION = INDEX_NAME = MAP_NAME = IDENTITY_POOL_ID = TRACKER_NAME =
左側のナビゲーターセクションから、プロジェクトを選択します。
[ターゲット] セクションで、アプリケーションを選択し、[情報] タブをクリックします。
以下のような値で情報プロパティを追加します。
以下の内容の
Config.swift
ファイルを追加します。このファイルに Bundle 情報ファイルから設定値が読み取られます。import Foundation enum Config { static let region = Bundle.main.object(forInfoDictionaryKey: "Region") as! String static let mapName = Bundle.main.object(forInfoDictionaryKey: "MapName") as! String static let indexName = Bundle.main.object(forInfoDictionaryKey: "IndexName") as! String static let identityPoolId = Bundle.main.object(forInfoDictionaryKey: "IdentityPoolId") as! String static let trackerName = Bundle.main.object(forInfoDictionaryKey: "TrackerName") as! String }
ViewModel
という名前の新しいフォルダを作成し、その中にTrackingViewModel.swift
ファイルを追加します。import SwiftUI import AmazonLocationiOSAuthSDK import MapLibre final class TrackingViewModel : ObservableObject { @Published var trackingButtonText = NSLocalizedString("StartTrackingLabel", comment: "") @Published var trackingButtonColor = Color.blue @Published var trackingButtonIcon = "play.circle" @Published var region : String @Published var mapName : String @Published var indexName : String @Published var identityPoolId : String @Published var trackerName : String @Published var showAlert = false @Published var alertTitle = "" @Published var alertMessage = "" @Published var centerLabel = "" var clientIntialised: Bool var client: LocationTracker! var authHelper: AuthHelper var credentialsProvider: LocationCredentialsProvider? var mlnMapView: MLNMapView? var mapViewDelegate: MapViewDelegate? var lastGetTrackingTime: Date? var trackingActive: Bool init(region: String, mapName: String, indexName: String, identityPoolId: String, trackerName: String) { self.region = region self.mapName = mapName self.indexName = indexName self.identityPoolId = identityPoolId self.trackerName = trackerName self.authHelper = AuthHelper() self.trackingActive = false self.clientIntialised = false } func authWithCognito(identityPoolId: String?) { guard let identityPoolId = identityPoolId?.trimmingCharacters(in: .whitespacesAndNewlines) else { alertTitle = NSLocalizedString("Error", comment: "") alertMessage = NSLocalizedString("NotAllFieldsAreConfigured", comment: "") showAlert = true return } credentialsProvider = authHelper.authenticateWithCognitoUserPool(identityPoolId: identityPoolId) initializeClient() } func initializeClient() { client = LocationTracker(provider: credentialsProvider!, trackerName: trackerName) clientIntialised = true } }