Xcode プロジェクトを設定する

課金を有効にして API キーを作成したら、アプリの開発に使用する Xcode プロジェクトをセットアップできます。

リリースノートは、各リリースごとに用意されています。

ステップ 1: 必要なソフトウェアをインストールする

Places SDK for iOS を使用してプロジェクトを作成するには、以下が必要です。

  • Xcode バージョン 14.0 以降
  • Cocoapods
  • ステップ 2: Xcode プロジェクトを作成し、Places SDK for iOS をインストールする

    新しいプロジェクトに API をインストールする手順は次のとおりです。

    CocoaPods を使う

    Places SDK for iOS は、すべてのプレイス機能を含む CocoaPod ポッドの GooglePlaces です。

    CocoaPods は、Swift と Objective-C の Cocoa プロジェクトにおけるオープンソースの依存関係マネージャーです。CocoaPods ツールをまだインストールしていない場合は、ターミナルから次のコマンドを実行して macOS にインストールします。詳しくは、CocoaPods のスタートガイドをご覧ください。

    sudo gem install cocoapods

    Places SDK for iOS の Podfile を作成し、それを使用して SDK とその依存関係をインストールします。

    1. Xcode プロジェクトをまだ作成していない場合は、ここで作成してローカルマシンに保存しますiOS 開発経験がない場合は、新しいプロジェクトを作成して、iOS アプリ テンプレートを選択します。
    2. プロジェクトのディレクトリに、Podfile という名前のファイルを作成します。このファイルでプロジェクトの依存関係を定義します。
    3. Podfile を編集して、依存関係とそのversionsを追加します。以下に、アプリケーションのターゲット名と GooglePlaces Pod の名前を指定する例を示します。
      source 'https://github.com/CocoaPods/Specs.git'
      
      platform :ios, '14.0'
      
      target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
        pod 'GooglePlaces', '8.3.0'
      end
      
      常に最新の状態になるように、pod outdated を定期的に実行して新しいバージョンが検出されるようにしてください。
    4. Podfile を保存します。
    5. ターミナルを開き、Podfile を含むディレクトリに移動します。

      cd <path-to-project>
    6. pod install コマンドを実行します。Podfile で指定した API とその依存関係がインストールされます。

      pod install
    7. Xcode を終了し、プロジェクトの .xcworkspace ファイルを(ダブルクリックして)開いて、Xcode を起動します。これ以降、このプロジェクトを開くには .xcworkspace ファイルを使用する必要があります。

    既存のプロジェクトの API を更新する手順は次のとおりです。

    1. ターミナルを開いて、Podfile を含むプロジェクト ディレクトリに移動します。
    2. pod update コマンドを実行します。これにより、Podfile で指定されたすべての API が最新バージョンに更新されます。

    手動でのインストール

    このガイドでは、Places SDK for iOS を含む XCFramework をプロジェクトに手動で追加し、Xcode でビルド設定を構成する方法について説明します。XCFramework は、Apple シリコンを使用したマシンなど、複数のプラットフォームで使用できるバイナリ パッケージです。

    1. 次の SDK バイナリ ファイルとリソース ファイルをダウンロードします。
    2. zip ファイルを解凍して、XCFramework とリソースにアクセスします。
    3. Xcode を起動し、既存のプロジェクトを開くか、新しいプロジェクトを作成します。iOS 開発経験がない場合は、新しいプロジェクトを作成して、iOS アプリ テンプレートを選択します。
    4. 以前のリリースの Maps バンドルをプロジェクトから削除します。
    5. プロジェクトの [Frameworks, Libraries, and Embedded Content] に、次の XCFramework をドラッグします。必ず、[埋め込みしない] を選択してください。
      • GooglePlaces.xcframework
    6. ダウンロードした GooglePlacesResources から GooglePlaces.bundle を Xcode プロジェクトの最上位ディレクトリにドラッグします。プロンプトが表示されたら、[Copy items into destination group's folder] が選択されていることを確認します。
    7. プロジェクト ナビゲータでプロジェクトを選択し、アプリのターゲットを選択します。
    8. [Build Phases] タブを開き、[Link Binary with Libraries] 内で、次のフレームワークとライブラリを追加します。
      • CoreGraphics.framework
      • CoreLocation.framework
      • libc++.tbd
      • libz.tbd
      • QuartzCore.framework
      • UIKit.framework
    9. 特定のターゲットではなくプロジェクトを選択し、[Build Settings] タブを開きます。

    10. [Other Linker Flags] セクションで -ObjC を追加します。これらの設定が表示されない場合は、[Build Settings] バーのフィルタを [Basic] から [All] に変更します。

    ステップ 3: アプリに 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"];

    ステップ 4: コードを記述する

    次のコードサンプルは、現在の場所を取得する方法を示しています。

    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
          

    次のステップ

    プロジェクトを設定したら、サンプルアプリを確認できます。Cocoapods v1.6.1 がインストールされている必要があります。