啟用計費功能並建立 API 金鑰後,您就可以設定用來開發應用程式的 Xcode 專案。
每個版本都會提供版本資訊。
步驟 1:安裝必要軟體
如要使用 Places SDK for iOS 建立專案,您需要:
- Xcode 15.0 版或更新版本
步驟 2:建立 Xcode 專案並安裝 Places SDK for iOS
Swift 套件管理工具
您可以透過 Swift 套件管理工具安裝 Places SDK for iOS。如要新增 SDK,請確認您已移除所有現有的 Places SDK for iOS 依附元件。
如要將 SDK 加入新專案或現有專案,請按照下列步驟操作:
-
開啟 Xcode
project
或workspace
,然後依序前往「File」>「Add Package Dependencies」。 - 輸入 https://github.com/googlemaps/ios-places-sdk 做為網址,然後按下 Enter 鍵提取套件,然後按一下「Add Package」。
-
如要安裝特定的
version
,請將「依附元件規則」欄位設為其中一個以版本為基礎的選項。如果是新專案,建議您指定最新版本並使用「精確版本」選項。完成後,按一下「新增套件」。 -
在「選擇套件產品」視窗中,確認
GooglePlaces
會新增至指定的main
目標。完成後,按一下「新增套件」。 -
如要驗證安裝狀態,請前往目標的
General
窗格。在「Frameworks、Library and Embedded Content」中應會顯示已安裝的套件。您也可以查看「Project Navigator」的「Package Dependencies」部分,確認套件及其版本。
如要更新現有專案的 package
,請按照下列步驟操作:
如果您要從 9.0.0 以下版本升級,必須在升級後移除下列依附元件:
GoogleMapsBase
、GoogleMapsCore
和GoogleMapsM4B
。請勿移除GoogleMaps
的依附元件。詳情請參閱 9.0.0 版本資訊。在 Xcode 專案設定設定中,找到「Frameworks、Library 和 Embedded Content」。請使用減號(-) 移除下列架構:
GoogleMapsBase
(僅適用於 9.0.0 以下版本的升級作業)GoogleMapsCore
(僅適用於 9.0.0 以下版本的升級作業)GoogleMapsM4B
(僅適用於 9.0.0 以下版本的升級作業)
- 在 Xcode 中,前往 [檔案] > [套件] > [更新至最新套件版本]。
- 如要驗證安裝狀態,請前往「Project Navigator」的「Package Dependencies」部分,檢查套件及其版本。
如要移除使用 CocoaPods
新增的現有 Places SDK for iOS 依附元件,請按照下列步驟操作:
- 關閉 Xcode 工作區。開啟終端機並執行下列指令:
sudo gem install cocoapods-deintegrate cocoapods-clean pod deintegrate pod cache clean --all
-
如果您未在 CocoaPods 以外的地方使用
Podfile
、Podfile.resolved
和 Xcodeworkspace
,請移除這些項目。
-
在 Xcode 專案設定設定中,找到「Frameworks、Library 和 Embedded Content」。請使用減號(-) 移除下列架構:
GooglePlaces.xcframework
-
在 Xcode 專案的頂層目錄中,移除
GooglePlaces
軟體包。
CocoaPods
Places SDK for iOS 以 CocoaPod Pod 的形式提供,該 Pod GooglePlaces 會包含所有地點功能。
CocoaPods 是 Swift 和 Objective-C Cocoa 專案的開放原始碼依附元件管理工具。如果您還沒有 CocoaPods 工具,請在終端機中執行下列指令,以便在 macOS 上安裝。詳情請參閱 CocoaPods 入門指南。
sudo gem install cocoapods
為 Places SDK for iOS 建立 Podfile
,並使用該 SDK 安裝 SDK 及其依附元件:
- 如果您還沒有 Xcode 專案,請立即建立一個並儲存至本機電腦 如果您是 iOS 開發新手,請建立新專案並選取「iOS App Template」。
- 在專案目錄中建立名為
Podfile
的檔案。這個檔案定義了專案的依附元件。 - 編輯
Podfile
,新增依附元件及其版本。以下範例指定了您的應用程式目標名稱和GooglePlaces
Pod 名稱:source 'https://github.com/CocoaPods/Specs.git' platform :ios, '15.0' target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pod 'GooglePlaces', '8.3.0' end
請務必定期執行pod outdated
來偵測是否有較新版本,確保您使用最新版本。 - 儲存
Podfile
。 開啟終端機並前往包含
Podfile
的目錄:cd <path-to-project>
執行
pod install
指令。此動作會安裝Podfile
中指定的 API 及其所有依附元件。pod install
關閉 Xcode,接著按兩下來開啟專案的
.xcworkspace
檔案來啟動 Xcode。從現在起,您必須使用.xcworkspace
檔案來開啟專案。
如要更新現有專案的 API,請按照下列步驟操作:
- 開啟終端機,然後前往包含
Podfile
的專案目錄。 - 執行
pod update
指令。這項操作會將Podfile
中指定的所有 API 更新為最新版本。
手動安裝程式庫
本指南說明如何手動將包含 Places SDK for iOS 的 XCFramework 新增至專案,並在 Xcode 中完成建構設定。XCFramework 是可以在多種平台上使用的二進位套件,包括使用 Apple 晶片的機器。
- 下載下列 SDK 二進位檔和資源檔案:
- 解壓縮檔案以存取 XCFramework 和資源。
- 啟動 Xcode,接著開啟現有專案,或建立新專案。如果您是 iOS 開發新手,請建立新專案並選取「iOS App Template」。
- 從專案中移除先前版本中的所有地圖套件。
-
開啟「一般」分頁。將下列 XCFramework 拖曳至專案「Frameworks、Library and Embedded Content」。並務必選取「不要嵌入」:
GooglePlaces.xcframework
從已下載的 GooglePlacesResources 拖曳 -
將您下載的 GooglePlacesResources 中的
GooglePlaces.bundle
複製到 Xcode 專案的頂層目錄。系統提示時,請務必選取「Copy items to destination group's folder」。 - 從專案導覽器中選取專案,然後選擇應用程式目標。
-
開啟「Build Phases」分頁。在「連結二進位檔與程式庫」中,新增下列架構和程式庫:
CoreGraphics.framework
CoreLocation.framework
libc++.tbd
libz.tbd
QuartzCore.framework
UIKit.framework
-
選擇您的專案 (而非特定目標),然後開啟「Build Settings」分頁。在「Linking - General」->「Other Linker Flags」部分中,將
-ObjC
新增至「Debug」和「Release」。如未顯示這些設定,請將「Build Settings」列中的篩選器從「Basic」變更為「All」。
GooglePlaces.bundle
GooglePlacesSwift
您可以使用 Swift Package Manager、CocoaPods 和手動安裝 GooglePlacesSwift (預先發布版) SDK。所有程序的安裝步驟皆與 Places SDK for iOS 所述的步驟相同,但有下列差異:
- Xcode 15.3 為必要項目。
- 如果您使用的是 Swift 套件管理工具,則必須將
GooglePlaces
的執行個體替換為GooglePlacesSwift
。其中包括匯入陳述式。 - 將版本號碼更新為 0.1.0。
- 如要手動安裝,請下載下列 SDK 二進位檔和資源檔案:
舉例來說,如果使用 CocoaPods,編輯的 Podfile
看起來會像這樣:
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '15.3' target 'YOUR_APPLICATION_TARGET_NAME_HERE' do pod 'GooglePlacesSwift', '0.1.0' end
步驟 3:檢查 Apple 隱私權資訊清單檔案
Apple 要求在 App Store 上架應用程式,要求取得應用程式隱私權詳細資訊。如需最新資訊和其他資訊,請前往 Apple App Store 隱私權詳細資料頁面。
Apple 隱私權資訊清單檔案包含在 SDK 資源套件中。如要確認隱私權資訊清單檔案已納入並檢查其中的內容,請建立應用程式的封存檔案,然後從封存檔中產生隱私權報告。
步驟 4:在應用程式中加入 API 金鑰
請將以下範例中的 YOUR_API_KEY
替換成您的 API 金鑰。
Swift
將 API 金鑰新增到您的 AppDelegate.swift
中,如下所示:
- 新增下列匯入陳述式:
import GooglePlaces
- 將以下內容新增到您的
application(_:didFinishLaunchingWithOptions:)
方法中,並將 YOUR_API_KEY 換成您的 API 金鑰:GMSPlacesClient.provideAPIKey("YOUR_API_KEY")
Objective-C
將 API 金鑰新增到您的 AppDelegate.m
中,如下所示:
- 新增下列匯入陳述式:
@import GooglePlaces;
- 將以下內容新增到您的
application:didFinishLaunchingWithOptions:
方法中,並將 YOUR_API_KEY 換成您的 API 金鑰:[GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];
GooglePlacesSwift
將 API 金鑰新增到您的 AppDelegate.swift
中,如下所示:
- 新增下列匯入陳述式:
import GooglePlacesSwift
- 將以下內容新增到您的
application(_:didFinishLaunchingWithOptions:)
方法中,並將 YOUR_API_KEY 換成您的 API 金鑰:PlacesClient.shared.provideAPIKey("YOUR_API_KEY")
步驟 5:開始編寫程式碼
下列程式碼範例示範如何取得目前位置。
Swift
import GooglePlaces import UIKit class GetStartedViewController : UIViewController { // Add a pair of UILabels in Interface Builder, and connect the outlets to these variables. @IBOutlet private var nameLabel: UILabel! @IBOutlet private var addressLabel: UILabel! private var placesClient: GMSPlacesClient! override func viewDidLoad() { super.viewDidLoad() placesClient = GMSPlacesClient.shared() } // Add a UIButton in Interface Builder, and connect the action to this function. @IBAction func getCurrentPlace(_ sender: UIButton) { let placeFields: GMSPlaceField = [.name, .formattedAddress] placesClient.findPlaceLikelihoodsFromCurrentLocation(withPlaceFields: placeFields) { [weak self] (placeLikelihoods, error) in guard let strongSelf = self else { return } guard error == nil else { print("Current place error: \(error?.localizedDescription ?? "")") return } guard let place = placeLikelihoods?.first?.place else { strongSelf.nameLabel.text = "No current place" strongSelf.addressLabel.text = "" return } strongSelf.nameLabel.text = place.name strongSelf.addressLabel.text = place.formattedAddress } } }
Objective-C
#import "GetStartedViewController.h" @import GooglePlaces; @interface GetStartedViewController () // Add a pair of UILabels in Interface Builder and connect the outlets to these variables @property (weak, nonatomic) IBOutlet UILabel *nameLabel; @property (weak, nonatomic) IBOutlet UILabel *addressLabel; @end @implementation GetStartedViewController { GMSPlacesClient *_placesClient; } - (void)viewDidLoad { [super viewDidLoad]; _placesClient = [GMSPlacesClient sharedClient]; } // Add a pair of UILabels in Interface Builder and connect the outlets to these variables. - (IBAction)getCurrentPlace:(UIButton *)sender { GMSPlaceField placeFields = (GMSPlaceFieldName | GMSPlaceFieldFormattedAddress); __weak typeof(self) weakSelf = self; [_placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:placeFields callback:^(NSArray<GMSPlaceLikelihood *> * _Nullable likelihoods, NSError * _Nullable error) { __typeof__(self) strongSelf = weakSelf; if (strongSelf == nil) { return; } if (error != nil) { NSLog(@"An error occurred %@", [error localizedDescription]); return; } GMSPlace *place = likelihoods.firstObject.place; if (place == nil) { strongSelf.nameLabel.text = @"No current place"; strongSelf.addressLabel.text = @""; return; } strongSelf.nameLabel.text = place.name; strongSelf.addressLabel.text = place.formattedAddress; }]; } @end
GooglePlacesSwift
struct ContentView: View { @State var place: Place? var body: some View { Button("Get Place") { // A hotel in Saigon with an attribution. let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs" let fetchPlaceRequest = FetchPlaceRequest( placeID: placeID, placeProperties: [.displayName, .formattedAddress] ) Task { switch await placesClient.fetchPlace(with: fetchPlaceRequest) { case .success(let place): self.place = place case .failure(let placesError): // Handle error } Text(swiftPlace?.displayName ?? "No place yet") .padding() Text(swiftPlace?.formattedAddress ?? "No place yet") .padding() } }
後續步驟
專案設定完畢後,您可以探索範例應用程式。