یک پروژه Xcode راه اندازی کنید

After you enable billing and create an API key, you're ready to set up the Xcode project that you use to develop your app.

Release notes are available for each release.

Step 1: Install the required software

To build a project using the Maps SDK for iOS, you must download and install:

  • Xcode version 26.0 or later

Step 2: Create the Xcode project and install the Maps SDK for iOS

The SDK install size can vary. For example, the install size for Maps SDK for iOS version 9.0 is about 18 MB.

مدیر بسته سوئیفت

The Maps SDK for iOS can be installed via Swift Package Manager . To add the SDK, ensure you have removed any existing Maps SDK for iOS dependencies.

To add the SDK to a new or existing project, follow these steps:

  1. Open your Xcode project or workspace , then go to File > Add Package Dependencies .
  2. Enter https://github.com/googlemaps/ios-maps-sdk as the URL, press Enter to pull in the package, and click "Add Package".
  3. To install a specific version , set the Dependency Rule field to one of the version-based options. For new projects, we recommend specifying the latest version and using the "Exact Version" option. Once complete, click "Add Package".

To update the package for an existing project, follow these steps:

  1. If you are upgrading from a version earlier than 9.0.0, you must remove the following dependencies: GoogleMapsBase , GoogleMapsCore , and GoogleMapsM4B after upgrading. Do not remove the dependency for GoogleMaps . For more information, see the Version 9.0.0 Release Notes .

    From your Xcode project configuration settings, find Frameworks, Libraries, and Embedded Content . Use the minus sign(-) to remove the following framework:

    • GoogleMapsBase (Only for upgrades from versions earlier than 9.0.0)
    • GoogleMapsCore (Only for upgrades from versions earlier than 9.0.0)
    • GoogleMapsM4B (Only for upgrades from versions earlier than 9.0.0)
  2. From Xcode, go to "File > Packages > Update To Latest Package Versions".
  3. To verify your installation, go to the Package Dependencies section of Project Navigator to verify the package and its version.

To remove existing Maps SDK for iOS dependencies added using CocoaPods , follow these steps:

  1. Close your Xcode workspace. Open terminal and execute the following command:
    sudo gem install cocoapods-deintegrate cocoapods-clean
    pod deintegrate
    pod cache clean --all
  2. Remove the Podfile , Podfile.resolved and the Xcode workspace if you are not using them for anything other than CocoaPods.
To remove existing Maps SDK for iOS installed manually, follow these steps:
  1. From your Xcode project configuration settings, find Frameworks, Libraries, and Embedded Content . Use the minus sign(-) to remove the following framework:
    • GoogleMaps
    • GoogleMapsBase (Only for installations earlier than version 9.2.0)
    • GoogleMapsCore (Only for installations earlier than version 9.2.0)
    • GoogleMapsM4B (Only for installations earlier than version 9.0.0)
  2. From the top level directory of your Xcode project, remove the GoogleMaps bundle.

نصب دستی

This guide shows how to manually add the XCFrameworks containing the Maps SDK for iOS to your project and configure your build settings in Xcode. An XCFramework is a binary package that you can use on multiple platforms, including machines using Apple silicon .

  1. فایل‌های باینری و منابع SDK زیر را دانلود کنید:
  2. Extract the files to access the XCFrameworks and resources.
  3. If you don't have an Xcode project yet, create one now and save it to your local machine. If you're new to iOS development:
    1. یک پروژه جدید ایجاد کنید.
    2. قالب iOS > App را انتخاب کنید.
    3. در صفحه گزینه‌های پروژه:
      1. نام پروژه را وارد کنید.
      2. Record the value of the Bundle identifier field. You can use that value to restrict your API key below.
      3. رابط پروژه را روی Storyboard تنظیم کنید.
      4. زبان را روی Swift یا Objective-C تنظیم کنید.
  4. Open the General tab. Drag the following XCFrameworks into your project under Frameworks, Libraries, and Embedded Content . Make sure to select Do Not Embed :
    • GoogleMaps.xcframework
  5. Copy the GoogleMaps.bundle from the GoogleMapsResources you downloaded into your Xcode project's top level directory. Make sure to select Copy items into destination group's folder when prompted.
  6. Select your project from the Project Navigator, and choose your application's target.
  7. تب Build Phases را برای برنامه‌ی هدف خود باز کنید. در داخل Link Binary with Libraries ، فریم‌ورک‌ها و کتابخانه‌های زیر را اضافه کنید:
    • Accelerate.framework
    • Contacts.framework
    • CoreData.framework
    • CoreGraphics.framework
    • CoreImage.framework
    • CoreLocation.framework
    • CoreTelephony.framework
    • CoreText.framework
    • GLKit.framework
    • ImageIO.framework
    • libc++.tbd
    • libz.tbd
    • Metal.framework
    • MetricKit.framework
    • OpenGLES.framework (Only if you are using OpenGL)
    • QuartzCore.framework
    • Security.framework
    • SystemConfiguration.framework
    • UIKit.framework
  8. Choose your project, rather than a specific target, and open the Build Settings tab. In the Linking - General -> Other Linker Flags section, add -ObjC to "Debug" and "Release". If these settings aren't visible, change the filter in the Build Settings bar from Basic to All .

  9. To install the Places SDK for iOS XCFramework, see Get Started with the Places SDK for iOS .

کوکوپادز

The Maps SDK for iOS is available as a CocoaPods pod. CocoaPods is an open source dependency manager for Swift and Objective-C Cocoa projects.

If you don't already have the CocoaPods tool, install it on macOS by running the following command from the terminal. For details, see the CocoaPods Getting Started guide .

sudo gem install cocoapods

Create a Podfile for the Maps SDK for iOS and use it to install the API and its dependencies:

  1. If you don't have an Xcode project yet, create one now and save it to your local machine. If you're new to iOS development:
    1. یک پروژه جدید ایجاد کنید.
    2. قالب iOS > App را انتخاب کنید.
    3. در صفحه گزینه‌های پروژه:
      1. نام پروژه را وارد کنید.
      2. مقدار فیلد شناسه بسته (Bundle identifier) ​​را ثبت کنید. می‌توانید از آن مقدار برای محدود کردن کلید API خود در زیر استفاده کنید.
      3. رابط پروژه را روی Storyboard تنظیم کنید.
      4. زبان را روی Swift یا Objective-C تنظیم کنید.
  2. Create a file named Podfile in your project directory. This file defines your project's dependencies.
  3. Podfile ویرایش کنید و وابستگی‌های خود را به همراه نسخه‌های آنها اضافه کنید. در اینجا مثالی آورده شده است که شامل وابستگی مورد نیاز برای Maps SDK برای iOS است:
    source 'https://github.com/CocoaPods/Specs.git'
    
    platform :ios, '16.0'
    
    target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
      pod 'GoogleMaps', '10.13.0'
    end
    Regularly run pod outdated to detect when there is a newer version to make sure you're always on the latest version. If necessary, upgrade to the latest version .
  4. Podfile را ذخیره کنید.
  5. Open a terminal and go to the directory containing the Podfile :

    cd <path-to-project>
  6. Run the pod install command. This will install the APIs specified in the Podfile , along with any dependencies they may have.

    pod install
  7. Xcode را ببندید و سپس فایل .xcworkspace پروژه خود را باز کنید (روی آن دوبار کلیک کنید). از این به بعد، برای باز کردن پروژه باید از فایل .xcworkspace استفاده کنید.

To update the API for an existing project, follow these steps:

  1. Open a terminal and go to the project directory containing the Podfile .
  2. Run the pod update command. This will update all of the APIs specified in the Podfile to the latest version.

مرحله ۳: کلید API خود را به پروژه اضافه کنید

In Get an API key , you generated an API key for your app. Now add that key to your Xcode project.

در مثال‌های زیر، YOUR_API_KEY را با کلید API خود جایگزین کنید.

سویفت

کلید API خود را به صورت زیر به AppDelegate.swift خود اضافه کنید:

  1. عبارت import زیر را اضافه کنید:
    import GoogleMaps
  2. Add the following to your application(_:didFinishLaunchingWithOptions:) method, using your API key:
    GMSServices.provideAPIKey("YOUR_API_KEY")
  3. اگر از Places API نیز استفاده می‌کنید، کلید خود را دوباره همانطور که در اینجا نشان داده شده است اضافه کنید:
    GMSPlacesClient.provideAPIKey("YOUR_API_KEY")

هدف-سی

کلید API خود را به صورت زیر به AppDelegate.m خود اضافه کنید:

  1. عبارت import زیر را اضافه کنید:
    @import GoogleMaps;
  2. با استفاده از کلید API خود، کد زیر را به application:didFinishLaunchingWithOptions: اضافه کنید:
    [GMSServices provideAPIKey:@"YOUR_API_KEY"];
  3. اگر از Places API نیز استفاده می‌کنید، کلید خود را دوباره همانطور که در اینجا نشان داده شده است اضافه کنید:
    [GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];

مرحله ۴ (اختیاری): بررسی فایل مانیفست حریم خصوصی اپل

اپل برای برنامه‌های موجود در اپ استور، جزئیات حریم خصوصی برنامه را الزامی می‌داند. برای به‌روزرسانی‌ها و اطلاعات بیشتر ، از صفحه جزئیات حریم خصوصی اپ استور اپل دیدن کنید.

The Apple Privacy Manifest file is included in the resources bundle for the SDK. To verify that the Privacy Manifest File has been included, and to inspect its contents, create an archive of your app and generate a privacy report from the archive.

مرحله ۵ (اختیاری): طرح‌های URL مورد استفاده توسط API را اعلام کنید

Beginning with iOS 9 and Xcode 7, apps can declare the URL schemes that they intend to open, by specifying the schemes in the app's Info.plist file. The Maps SDK for iOS opens the Google Maps mobile app when the user clicks the Google logo on the map, and your app can therefore declare the relevant URL schemes.

برای اعلام طرح‌های URL مورد استفاده توسط Maps SDK برای iOS، خطوط زیر را به Info.plist خود اضافه کنید:

 <key>LSApplicationQueriesSchemes</key>
 <array>
        <string>googlechromes</string>
        <string>comgooglemaps</string>
    </array>

تصویر زیر پیکربندی رابط کاربری Xcode را نشان می‌دهد:

پیکربندی `LSApplicationQueriesSchemes` در Xcode

بدون تعریف فوق، وقتی کاربر روی لوگوی گوگل روی نقشه ضربه می‌زند، خطاهای زیر ممکن است رخ دهد:

  • -canOpenURL: failed for URL: "comgooglemaps://" - error: "This app is not allowed to query for scheme comgooglemaps"
  • -canOpenURL: failed for URL: "googlechromes://" - error: "This app is not allowed to query for scheme googlechromes"

برای حذف این خطاها، این اعلان را به Info.plist خود اضافه کنید.

قدم بعدی چیست؟

Now that you have an API key and an Xcode project, you can create and run apps. The Navigation SDK for iOS provides many tutorials and sample apps that can help you get started. For more details, see: