Skip to content

Commit

Permalink
feat: (iOS) UIDevice 클래스를 통해 기기 모델명 가져오기
Browse files Browse the repository at this point in the history
  • Loading branch information
floydkim committed Jan 1, 2024
1 parent eb445a6 commit a3449ae
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cpp/react-native-jsi-tutorial-bob.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#include "react-native-jsi-tutorial-bob.h"
#include <jsi/jsi.h>

using namespace facebook;

namespace jsitutorialbob {
double add(double a, double b) {
return a + b;
}

void throwJSError(jsi::Runtime &runtime, const std::string &message) {
throw jsi::JSError(runtime, message);
}
}
5 changes: 5 additions & 0 deletions cpp/react-native-jsi-tutorial-bob.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#ifndef JSITUTORIALBOB_H
#define JSITUTORIALBOB_H

#include <jsi/jsi.h>

using namespace facebook;

namespace jsitutorialbob {
double add(double a, double b);
void throwJSError(jsi::Runtime &runtime, const std::string &message);
}

#endif /* JSITUTORIALBOB_H */
3 changes: 2 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

import { StyleSheet, View, Text } from 'react-native';
import { add } from 'react-native-jsi-tutorial-bob';
import { add, getDeviceModel } from 'react-native-jsi-tutorial-bob';

export default function App() {
// const [result, setResult] = React.useState<number | undefined>();
Expand All @@ -14,6 +14,7 @@ export default function App() {
<View style={styles.container}>
{/*<Text>Result: {result}</Text>*/}
<Text>Result: {add(30, 10)}</Text>
<Text>Device Model: {getDeviceModel()}</Text>
</View>
);
}
Expand Down
16 changes: 16 additions & 0 deletions ios/JsiTutorialBob.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import "JsiTutorialBob.h"
#include "react-native-jsi-tutorial-bob.h" // Include the C++ header
//#import <React/RCTBridgeModule.h>
//#import <React/RCTBridge+Private.h>

@implementation JsiTutorialBob
RCT_EXPORT_MODULE()
Expand All @@ -19,6 +21,20 @@ @implementation JsiTutorialBob
return @(jsitutorialbob::add(a, b)); // Call the C++ function
}

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(getDeviceModel)
{
@try {
NSString *deviceModel = [UIDevice currentDevice].model;
return deviceModel;
} @catch (NSException *exception) {
// JS 런타임을 가져오는 방법을 모르겠음.
// jsi::Runtime& runtime = _bridge.jsCallInvoker->jsRuntime();
// jsitutorialbob::throwJSError(runtime, std::string([exception.reason UTF8String]));
return @"unknown";
}
}


// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
1 change: 1 addition & 0 deletions src/NativeJsiTutorialBob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TurboModuleRegistry } from 'react-native';
export interface Spec extends TurboModule {
// multiply(a: number, b: number): Promise<number>;
add(a: number, b: number): number;
getDeviceModel(): string;
}

export default TurboModuleRegistry.getEnforcing<Spec>('JsiTutorialBob');
4 changes: 4 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ const JsiTutorialBob = JsiTutorialBobModule
export function add(a: number, b: number): number {
return JsiTutorialBob.add(a, b);
}

export function getDeviceModel(): string {
return JsiTutorialBob.getDeviceModel();
}

0 comments on commit a3449ae

Please sign in to comment.