Back to React Native

Turbo Native Modules: iOS

website/versioned_docs/version-0.77/turbo-native-modules-ios.md

latest4.6 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import constants from '@site/core/TabsConstants';

Now it's time to write some iOS platform code to make sure localStorage survives after the application is closed.

Prepare your Xcode Project

We need to prepare your iOS project using Xcode. After completing these 6 steps you'll have RCTNativeLocalStorage that implements the generated NativeLocalStorageSpec interface.

  1. Open the CocoaPods generated Xcode Workspace:
bash
cd ios
open TurboModuleExample.xcworkspace
  1. Right click on app and select <code>New Group</code>, call the new group NativeLocalStorage.

  2. In the NativeLocalStorage group, create <code>New</code><code>File from Template</code>.

  3. Use the <code>Cocoa Touch Class</code>.

  4. Name the Cocoa Touch Class <code>RCTNativeLocalStorage</code> with the <code>Objective-C</code> language.

  5. Rename <code>RCTNativeLocalStorage.m</code><code>RCTNativeLocalStorage.mm</code> making it an Objective-C++ file.

Implement localStorage with NSUserDefaults

Start by updating RCTNativeLocalStorage.h:

objc
//  RCTNativeLocalStorage.h
//  TurboModuleExample

#import <Foundation/Foundation.h>
// highlight-add-next-line
#import <NativeLocalStorageSpec/NativeLocalStorageSpec.h>

NS_ASSUME_NONNULL_BEGIN

// highlight-remove-next-line
@interface RCTNativeLocalStorage : NSObject
// highlight-add-next-line
@interface RCTNativeLocalStorage : NSObject <NativeLocalStorageSpec>

@end

Then update our implementation to use NSUserDefaults with a custom suite name.

objc
//  RCTNativeLocalStorage.m
//  TurboModuleExample

#import "RCTNativeLocalStorage.h"

static NSString *const RCTNativeLocalStorageKey = @"local-storage";

@interface RCTNativeLocalStorage()
@property (strong, nonatomic) NSUserDefaults *localStorage;
@end

@implementation RCTNativeLocalStorage

RCT_EXPORT_MODULE(NativeLocalStorage)

- (id) init {
  if (self = [super init]) {
    _localStorage = [[NSUserDefaults alloc] initWithSuiteName:RCTNativeLocalStorageKey];
  }
  return self;
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const facebook::react::ObjCTurboModule::InitParams &)params {
  return std::make_shared<facebook::react::NativeLocalStorageSpecJSI>(params);
}

- (NSString * _Nullable)getItem:(NSString *)key {
  return [self.localStorage stringForKey:key];
}

- (void)setItem:(NSString *)value
          key:(NSString *)key {
  [self.localStorage setObject:value forKey:key];
}

- (void)removeItem:(NSString *)key {
  [self.localStorage removeObjectForKey:key];
}

- (void)clear {
  NSDictionary *keys = [self.localStorage dictionaryRepresentation];
  for (NSString *key in keys) {
    [self removeItem:key];
  }
}

@end

Important things to note:

  • RCT_EXPORT_MODULE exports and registers the module using the identifier we'll use to access it in the JavaScript environment: NativeLocalStorage. See these docs for more details.
  • You can use Xcode to jump to the Codegen @protocol NativeLocalStorageSpec. You can also use Xcode to generate stubs for you.

Build and run your code on a Simulator

<Tabs groupId="package-manager" queryString defaultValue={constants.defaultPackageManager} values={constants.packageManagers}> <TabItem value="npm"> ```bash npm run ios ``` </TabItem> <TabItem value="yarn"> ```bash yarn run ios ``` </TabItem> </Tabs> <video width="30%" height="30%" playsinline="true" autoplay="true" muted="true" loop="true"> <source src="/docs/assets/turbo-native-modules/turbo-native-modules-ios.webm" type="video/webm" /> <source src="/docs/assets/turbo-native-modules/turbo-native-modules-ios.mp4" type="video/mp4" /> </video>