Skip to content

iOS_setup

guoling edited this page Jul 9, 2024 · 4 revisions

MMKV for iOS/macOS

MMKV is an efficient, small, easy-to-use mobile key-value storage framework used in the WeChat application. It's currently available on both Android, iOS/macOS, Windows and POSIX.

Getting Started

Prerequisites

  • Apps using MMKV can target: iOS 12.0 or later, macOS 10.13 or later, WatchOS 4.0 or later.
  • Xcode 15.0 or later.

Installation

  • Via CocoaPods:

    1. Install CocoaPods;
    2. Open the terminal, cd to your project directory, run pod repo update to make CocoaPods aware of the latest available MMKV versions;
    3. Edit your Podfile, add pod 'MMKV' to your app target, or pod 'MMKVAppExtension' for AppExtension target, or pod 'MMKVWatchExtension' for your WatchExtension target.
    4. Run pod install;
    5. Open the .xcworkspace file generated by CocoaPods;
    6. Add #import <MMKV/MMKV.h> to your source file and we are done, replace with <MMKVAppExtension/MMKV.h> and <MMKVWatchExtension/MMKV.h> accordingly for other extension targets.
  • Via Carthage:

    1. Install Carthage;
    2. Edit your Cartfile, add github "Tencent/MMKV";
    3. Open the terminal, cd to your project directory, run carthage update;
    4. Drag MMKV.framework from the appropriate platform directory in Carthage/Build/ to the Linked Binary and Libraries section of your Xcode project’s Build Phases settings;
    5. On your application target's Build Phases settings tab, click the "+" icon and choose New Run Script Phase. Create a Run Script with carthage copy-frameworks, add the paths to the framework under Input Files: $(SRCROOT)/Carthage/Build/iOS/MMKV.framework, and add the paths to the copied frameworks to the Output Files: $(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/MMKV.framework;
    6. Add #import <MMKV/MMKV.h> to your source file and we are done.

    Note: Carthage has trouble building multi schemes. If you want to use MMKV in AppExtensions, you should choose other methods.

  • Via Dynamic Framework:

    1. Getting source code from the git repository:
      • git clone https://github.com/Tencent/MMKV.git
    2. Drag MMKV.xcodeproj in iOS/MMKV/ into your project;
    3. Add MMKV.framework to the Embedded Binaries section of your Xcode project's General settings; Note that there are two frameworks here and the dynamic one should be chosen. You can check it at Build Phases->Target Dependencies. The right one is MMKV while MMKV Static is used for static lib.
    4. Accordingly replace the MMKV.framework with MMKVAppextension.framework for AppExtension, and MMKVWatchExtension.framework for WatchExtension.
    5. Add #import <MMKV/MMKV.h> to your source file and we are done.

    Note: If you are pretty sure that encryption is not needed, you can turn on the preprocess directive MMKV_DISABLE_CRYPT in Core/MMKVPredef.h to save some binary size.

  • Via Static Framework:

    1. Getting source code from git repository:
      • git clone https://github.com/Tencent/MMKV.git
    2. Drag MMKV.xcodeproj in iOS/MMKV/ into your project;
    3. Add MMKV Static to the Target Dependencies section of your Xcode project's Build Phases settings;
    4. Add libMMKV Static.alibz.tbd to the Linked Binary and Libraries section of your Xcode project's Build Phases settings;
    5. Add -ObjC to the Other Linker Flags section of your Xcode project's Build Settings.
    6. Add #import <MMKV/MMKV.h> to your source file and we are done.
    7. If you want to use MMKV for AppExtensions, you should link them as dynamic frameworks as written above.

    Note: If you are pretty sure that encryption is not needed, you can turn on the preprocess directive MMKV_DISABLE_CRYPT in Core/MMKVPredef.h to save some binary size.

What's Next