Xcode 프로젝트 설정

결제를 사용 설정하고 API 키를 만든 후에는 앱 개발에 사용할 Xcode 프로젝트를 설정할 수 있습니다.

출시 노트는 각 출시 버전에서 제공됩니다.

1단계: 필수 소프트웨어 설치하기

iOS용 Places SDK를 사용하여 프로젝트를 빌드하려면 다음이 필요합니다.

  • Xcode 버전 14.0 이상
  • Cocoapods
  • 2단계: Xcode 프로젝트 만들기 및 iOS용 Places SDK 설치하기

    새 프로젝트에 API를 설치하려면 다음 단계를 따르세요.

    CocoaPods 사용

    iOS용 Places SDK는 모든 장소 기능이 포함된 CocoaPod 포드인 GooglePlaces로 제공됩니다.

    CocoaPods는 Swift 및 Objective-C Cocoa 프로젝트용 오픈소스 종속 항목 관리자입니다. CocoaPods 도구가 아직 없다면 터미널에서 다음 명령어를 실행하여 macOS에 설치합니다. 자세한 내용은 CocoaPods 시작 가이드를 참고하세요.

    sudo gem install cocoapods

    iOS용 Places SDK의 Podfile를 만들고 이를 사용하여 SDK 및 종속 항목을 설치합니다.

    1. 아직 Xcode 프로젝트가 없으면 지금 만들어 로컬 시스템에 저장합니다. iOS 개발이 처음인 경우 새 프로젝트를 만들고 iOS App template을 선택합니다.
    2. 프로젝트 디렉터리에 Podfile 파일을 만듭니다. 이 파일은 프로젝트의 종속 항목을 정의합니다.
    3. Podfile를 수정하고 versions과 함께 종속 항목을 추가합니다. 다음은 애플리케이션 타겟 이름과 GooglePlaces 포드의 이름을 지정하는 예입니다.
      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가 API에 포함된 종속 항목과 함께 설치됩니다.

      pod install
    7. Xcode를 닫은 다음 프로젝트의 .xcworkspace 파일을 (더블클릭하여) 열어 Xcode를 실행합니다. 이 때부터는 .xcworkspace 파일을 사용하여 프로젝트를 열어야 합니다.

    기존 프로젝트의 API를 업데이트하려면 다음 단계를 따르세요.

    1. 터미널을 열고 Podfile가 포함된 프로젝트 디렉터리로 이동합니다.
    2. pod update 명령어를 실행합니다. 이렇게 하면 Podfile에 지정된 모든 API가 최신 버전으로 업데이트됩니다.

    수동 설치

    이 가이드에서는 iOS용 Places SDK가 포함된 XCFramework를 프로젝트에 수동으로 추가하고 Xcode에서 빌드 설정을 구성하는 방법을 설명합니다. XCFramework는 Apple 실리콘을 사용하는 머신을 비롯한 여러 플랫폼에서 사용할 수 있는 바이너리 패키지입니다.

    1. 다음 SDK 바이너리 및 리소스 파일을 다운로드합니다.
    2. 압축된 파일의 압축을 풀어 XCFramework 및 리소스에 액세스합니다.
    3. Xcode를 실행하고 기존 프로젝트를 열거나 새 프로젝트를 만듭니다. iOS 개발이 처음인 경우 새 프로젝트를 만들고 iOS App template을 선택합니다.
    4. 프로젝트의 이전 릴리스에서 모든 Maps 번들을 삭제합니다.
    5. 다음 XCFramework를 프레임워크, 라이브러리 및 삽입된 콘텐츠의 프로젝트로 드래그합니다. 삽입 안함을 선택해야 합니다.
      • GooglePlaces.xcframework
    6. 다운로드한 GooglePlacesResources에서 GooglePlaces.bundle를 Xcode 프로젝트의 최상위 디렉터리로 드래그합니다. 메시지가 표시되면 항목을 대상 그룹의 폴더에 복사가 선택되어 있는지 확인합니다.
    7. Project Navigator에서 프로젝트를 선택하고 애플리케이션의 대상을 선택합니다.
    8. Build Phases(빌드 단계) 탭을 열고 Link Binary with Library(바이너리를 라이브러리와 연결)에서 다음 프레임워크와 라이브러리를 추가합니다.
      • 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이 설치되어 있어야 합니다.