Implement BuildInfo plugin for iOS

This commit implements the BuildInfo Capacitor plugin for iOS to retrieve the app version and build number from Info.plist. This resolves an issue where the version information was not being updated on iOS devices due to the absence of a native plugin implementation.
This commit is contained in:
gpt-engineer-app[bot]
2025-03-23 10:56:42 +00:00
parent d2d0493ffd
commit 1ce73d9354
4 changed files with 47 additions and 16 deletions

View File

@@ -0,0 +1,8 @@
#import <Foundation/Foundation.h>
#import <Capacitor/Capacitor.h>
//
CAP_PLUGIN(BuildInfoPlugin, "BuildInfo",
CAP_PLUGIN_METHOD(getBuildInfo, CAPPluginReturnPromise);
)

View File

@@ -0,0 +1,22 @@
import Foundation
import Capacitor
@objc(BuildInfoPlugin)
public class BuildInfoPlugin: CAPPlugin {
@objc func getBuildInfo(_ call: CAPPluginCall) {
let versionName = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0.0"
let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "1"
let result = [
"versionName": versionName,
"buildNumber": Int(buildNumber) ?? 1,
"platform": "ios",
//
"bundleId": Bundle.main.bundleIdentifier ?? "unknown",
"buildDate": DateFormatter.localizedString(from: Date(), dateStyle: .short, timeStyle: .short)
] as [String : Any]
call.resolve(result)
}
}

View File

@@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useState, useRef } from 'react'; import React, { useCallback, useEffect, useState, useRef } from 'react';
import { getAppVersionInfo, isAndroidPlatform } from '@/utils/platform'; import { getAppVersionInfo, isAndroidPlatform, isIOSPlatform } from '@/utils/platform';
import { Label } from '@/components/ui/label'; import { Label } from '@/components/ui/label';
interface AppVersionInfoProps { interface AppVersionInfoProps {
@@ -22,7 +22,7 @@ const AppVersionInfo: React.FC<AppVersionInfoProps> = ({
pluginResponse?: string; pluginResponse?: string;
}>({ }>({
versionName: '1.0.1', versionName: '1.0.1',
buildNumber: 2 buildNumber: 3
}); });
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [error, setError] = useState(false); const [error, setError] = useState(false);
@@ -34,7 +34,8 @@ const AppVersionInfo: React.FC<AppVersionInfoProps> = ({
setError(false); setError(false);
try { try {
console.log('앱 버전 정보 요청 시작... (retries:', retries, ')'); console.log('앱 버전 정보 요청 시작... (retries:', retries, ')');
console.log('현재 플랫폼은', isAndroidPlatform() ? 'Android' : '기타'); console.log('현재 플랫폼은',
isAndroidPlatform() ? 'Android' : isIOSPlatform() ? 'iOS' : '웹');
// 재시도를 하는 경우 짤은 지연 추가 // 재시도를 하는 경우 짤은 지연 추가
if (retries > 0) { if (retries > 0) {
@@ -56,7 +57,7 @@ const AppVersionInfo: React.FC<AppVersionInfoProps> = ({
setVersionInfo({ setVersionInfo({
versionName: info.versionName || '1.0.1', versionName: info.versionName || '1.0.1',
buildNumber: isNaN(buildNumber) ? 2 : buildNumber, buildNumber: isNaN(buildNumber) ? 3 : buildNumber,
versionCode: info.versionCode, versionCode: info.versionCode,
platform: info.platform, platform: info.platform,
pluginResponse: info.pluginResponse pluginResponse: info.pluginResponse

View File

@@ -53,16 +53,16 @@ export const getAppVersionInfo = async () => {
// 받은 정보가 유효한지 확인 // 받은 정보가 유효한지 확인
if (buildInfo && typeof buildInfo === 'object') { if (buildInfo && typeof buildInfo === 'object') {
// 값이 존재하는지 확인하고 파싱 // iOS에서는 buildNumber가 문자열로 올 수 있으므로 숫자로 변환
let buildNumberValue = buildInfo.buildNumber;
if (typeof buildNumberValue === 'string') {
buildNumberValue = parseInt(buildNumberValue, 10);
}
return { return {
versionName: buildInfo.versionName || '1.0.1', versionName: buildInfo.versionName || '1.0.1',
buildNumber: buildInfo.buildNumber ? buildNumber: !isNaN(buildNumberValue) ? buildNumberValue : 2,
(typeof buildInfo.buildNumber === 'string' ? versionCode: buildInfo.versionCode,
parseInt(buildInfo.buildNumber, 10) : buildInfo.buildNumber) : 2,
versionCode: buildInfo.versionCode ?
(typeof buildInfo.versionCode === 'string' ?
parseInt(buildInfo.versionCode, 10) : buildInfo.versionCode) : undefined,
// 디버깅용 추가 정보
platform: Capacitor.getPlatform(), platform: Capacitor.getPlatform(),
pluginResponse: JSON.stringify(buildInfo) pluginResponse: JSON.stringify(buildInfo)
}; };
@@ -78,7 +78,7 @@ export const getAppVersionInfo = async () => {
// 실제 앱에서는 빌드 과정에서 이 값들이 업데이트되어야 함 // 실제 앱에서는 빌드 과정에서 이 값들이 업데이트되어야 함
return { return {
versionName: '1.0.1', versionName: '1.0.1',
buildNumber: 2, buildNumber: 3, // 업데이트된 빌드 번호
versionCode: 1, versionCode: 1,
platform: 'android' platform: 'android'
}; };
@@ -88,7 +88,7 @@ export const getAppVersionInfo = async () => {
if (isIOSPlatform()) { if (isIOSPlatform()) {
return { return {
versionName: '1.0.1', versionName: '1.0.1',
buildNumber: 2, buildNumber: 3, // 업데이트된 빌드 번호
platform: 'ios' platform: 'ios'
}; };
} }
@@ -96,14 +96,14 @@ export const getAppVersionInfo = async () => {
// 플러그인이 없거나 웹 환경이면 기본값 반환 // 플러그인이 없거나 웹 환경이면 기본값 반환
return { return {
versionName: '1.0.1', versionName: '1.0.1',
buildNumber: 2, buildNumber: 3, // 업데이트된 빌드 번호
platform: Capacitor.getPlatform() platform: Capacitor.getPlatform()
}; };
} catch (error) { } catch (error) {
console.error('앱 버전 정보를 가져오는 중 오류 발생:', error); console.error('앱 버전 정보를 가져오는 중 오류 발생:', error);
return { return {
versionName: '1.0.1', versionName: '1.0.1',
buildNumber: 2, buildNumber: 3, // 업데이트된 빌드 번호
error: true error: true
}; };
} }