본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

플랫폼 감지

원문 보기 위키 갱신

플랫폼 감지 (Platform Detection)

Quasar에는 코드가 실행되는 컨텍스트 내의 플랫폼(및 그 기능)을 감지하는 헬퍼들이 내장되어 있어요.

[!TIP] 필요에 따라 Style & Identity > Visibility 페이지를 확인해 같은 효과를 CSS만으로 내는 방법을 볼 수도 있어요. 후자의 방법은 플랫폼과 무관하게 DOM 요소나 컴포넌트를 렌더링하므로, 앱의 성능을 어떻게 다룰지 현명하게 선택하세요.

출처: 문서

본문

Platform API

Props

  • userAgent (string, 선택) 클라이언트 브라우저 User Agent 예시:
    • 'mozilla/5.0 (macintosh; intel mac os x 10_14_5) applewebkit/537.36 (khtml, like gecko) chrome/75.0.3770.100 safari/537.36'
  • is (object, 선택) 클라이언트 브라우저 세부 정보(속성 이름은 브라우저에 따라 다름) 예시:
    • { chrome: true, version: '71.0.3578.98', versionNumber: 71, mac: true, desktop: true, webkit: true, name: 'chrome', platform: 'mac' } 객체 형태:
    • name (string, 선택) 브라우저 이름 예시: 'chrome'
    • platform (string, 선택) 플랫폼 이름 예시: 'mac'
    • version (string, 선택) 상세한 브라우저 버전 예시: '71.0.3578.98'
    • versionNumber (number, 선택) 숫자로 된 주 브라우저 버전
    • mobile (boolean, 선택) 플랫폼이 모바일인가요?
    • desktop (boolean, 선택) 플랫폼이 데스크톱인가요?
    • cordova (boolean, 선택) 플랫폼이 Cordova인가요?
    • capacitor (boolean, 선택) 플랫폼이 Capacitor인가요?
    • nativeMobile (boolean, 선택) 플랫폼이 네이티브 모바일 래퍼인가요?
    • nativeMobileWrapper (string, 선택) 네이티브 모바일 래퍼의 종류 허용값: 'cordova', 'capacitor'
    • electron (boolean, 선택) 플랫폼이 Electron인가요?
    • bex (boolean, 선택) 플랫폼이 BEX(Browser Extension)인가요?
    • linux (boolean, 선택) 운영체제가 Linux인가요?
    • mac (boolean, 선택) 운영체제가 Mac OS인가요?
    • win (boolean, 선택) 운영체제가 Windows인가요?
    • cros (boolean, 선택) 운영체제가 Chrome OS인가요?
    • chrome (boolean, 선택) 브라우저가 Google Chrome인가요?
    • firefox (boolean, 선택) 브라우저가 Firefox인가요?
    • opera (boolean, 선택) 브라우저가 Opera인가요?
    • safari (boolean, 선택) 브라우저가 Safari인가요?
    • vivaldi (boolean, 선택) 브라우저가 Vivaldi인가요?
    • edge (boolean, 선택) 브라우저가 Microsoft Edge인가요?
    • webkit (boolean, 선택) 브라우저가 Webkit 또는 Webkit 기반인가요?
    • android (boolean, 선택) 운영체제가 Android인가요?
    • ios (boolean, 선택) 운영체제가 iOS인가요?
    • ipad (boolean, 선택) 장치가 iPad인가요?
    • iphone (boolean, 선택) 장치가 iPhone인가요?
    • ipod (boolean, 선택) 장치가 iPod인가요?
  • has (object, 선택) 클라이언트 브라우저의 감지 가능한 속성 예시:
    • { touch: false, webStorage: true } 객체 형태:
    • touch (boolean, 선택) 클라이언트 브라우저가 터치 지원 장치에서 실행됨
    • webStorage (boolean, 선택) 클라이언트 브라우저가 Web Storage를 지원함
  • within (object, 선택) 클라이언트 브라우저 환경 예시: { iframe: false } 객체 형태:
    • iframe (boolean, 선택) 앱이 iframe 안에서 실행되나요?

메서드 (Methods)

  • parseSSR(ssrContext: object): object SSR/SSG 사용 시에만, 그리고 전역 import에서만 사용($q.platform에서는 아님) 파라미터:
    • ssrContext (object, 필수) SSR Context 객체 반환값: object SSR/SSG 사용 목적의 Platform 객체($q.platform과 같음)

Vue 주입 (Vue Injection)

$q.platform으로 접근할 수 있어요(Options API에서는 this.$q.platform, Composition API에서는 useQuasar().platform).

사용법 (Usage)

Vue 컴포넌트 JS 안에서의 사용:

import { useQuasar } from 'quasar'

setup () {
  const $q = useQuasar()

  $q.platform.is.mobile
}

Vue 컴포넌트 템플릿 안에서의 사용:

$q.platform.is.cordova

Vue 컴포넌트 밖에서 쓸 때는 import 해야 해요:

import { Platform } from 'quasar'

Platform.is 자체는 현재 플랫폼에 대한 세부 정보를 담은 객체를 반환해요. 예를 들어 MacOS 데스크톱 머신에서 Chrome을 실행할 때 Platform.is는 다음과 비슷한 것을 반환해요:

{
  chrome: true,
  desktop: true,
  mac: true,
  name: "chrome",
  platform: "mac",
  version: "70.0.3538.110",
  versionNumber: 70,
  webkit: true
}

이제 코드가 실행되는 플랫폼에 따라 서로 다른 컴포넌트나 DOM 요소를 렌더링하고 싶다고 해 볼게요. 데스크톱에서는 한 가지를, 모바일에서는 다른 것을 보여주고 싶다고 해요. 이렇게 하면 돼요:

<div v-if="$q.platform.is.desktop"> I'm only rendered on desktop! </div>

<div v-if="$q.platform.is.mobile"> I'm only rendered on mobile! </div>

<div v-if="$q.platform.is.electron"> I'm only rendered on Electron! </div>

예시 "Your device":

<template>
  <div class="q-mb-md">
    Browser User Agent: "<strong>{{ $q.platform.userAgent }}</strong
    >"
  </div>

  <q-markup-table flat bordered dense>
    <thead>
      <tr>
        <th class="text-left">Property</th>
        <th class="text-left">Value</th>
      </tr>
    </thead>

    <tbody>
      <tr
        v-for="(value, prop) in $q.platform.is"
        :key="prop"
        :class="
          value ? 'text-weight-bold platform-detection--row-highlight' : ''
        "
      >
        <td>{{ prop }}</td>
        <td>{{ value }}</td>
      </tr>
    </tbody>
  </q-markup-table>

  <div class="q-mt-md">
    The device which you are using to view this website
    <strong>{{ touch }}</strong> touch capability.
  </div>
</template>

<script setup>
import { useQuasar } from 'quasar'
import { computed } from 'vue'

const $q = useQuasar()
const touch = computed(() => ($q.platform.has.touch ? 'has' : 'does not have'))
</script>

<style lang="sass">
body
  &.body--light
    .platform-detection--row-highlight
      background-color: rgba(0,0,0,.05)
  &.body--dark
    .platform-detection--row-highlight
      background-color: rgba(255,255,255,.05)
</style>

속성 (Properties)

Platform 객체에 사용 가능한 속성들은 다음과 같아요. 전체 목록은 아니에요. 더 자세한 내용은 아래 API 섹션을 참고하세요.

속성 타입 의미
Platform.is.mobile Boolean 코드가 모바일 장치에서 실행 중인가요?
Platform.is.cordova Boolean 코드가 Cordova 안에서 실행 중인가요?
Platform.is.capacitor Boolean 코드가 Capacitor 안에서 실행 중인가요?
Platform.is.nativeMobile Boolean 코드가 네이티브 모바일 래퍼(Cordova/Capacitor) 안에서 실행 중인가요?
Platform.is.nativeMobileWrapper String 네이티브 모바일 래퍼의 이름('cordova', 'capacitor', 또는 undefined)
Platform.is.electron Boolean 코드가 Electron 안에서 실행 중인가요?
Platform.is.desktop Boolean 코드가 데스크톱 브라우저에서 실행 중인가요?
Platform.is.bex Boolean 코드가 브라우저 확장 프로그램에서 실행 중인가요?
Platform.is.android Boolean 앱이 Android 장치에서 실행 중인가요?
Platform.is.cros Boolean 앱이 Chrome OS 운영체제 장치에서 실행 중인가요?
Platform.is.ios Boolean 앱이 iOS 장치에서 실행 중인가요?
Platform.is.ipad Boolean 앱이 iPad에서 실행 중인가요?
Platform.is.iphone Boolean 앱이 iPhone에서 실행 중인가요?
Platform.is.ipod Boolean 앱이 iPod에서 실행 중인가요?
Platform.is.linux Boolean 코드가 Linux 운영체제 장치에서 실행 중인가요?
Platform.is.mac Boolean 코드가 MacOS 운영체제 장치에서 실행 중인가요?
Platform.is.win Boolean 코드가 Windows 운영체제 장치에서 실행 중인가요?
Platform.is.chrome Boolean 코드가 Google Chrome 브라우저 안에서 실행 중인가요?
Platform.is.firefox Boolean 코드가 Firefox 브라우저 안에서 실행 중인가요?
Platform.is.opera Boolean 코드가 Opera 브라우저 안에서 실행 중인가요?
Platform.is.safari Boolean 코드가 Apple Safari 브라우저 안에서 실행 중인가요?
Platform.is.vivaldi Boolean 코드가 Vivaldi 브라우저 안에서 실행 중인가요?
Platform.is.edge Boolean 코드가 Microsoft Edge 브라우저 안에서 실행 중인가요?
Platform.is.webkit Boolean 코드가 Webkit 또는 webkit 기반에서 실행 중인가요?
Platform.has.touch Boolean 코드가 터치 지원 화면에서 실행 중인가요?
Platform.within.iframe Boolean 앱이 IFRAME 안에서 실행 중인가요?

[!NOTE] 모바일에서 실행된다는 것은 코드가 모바일 장치(폰 또는 태블릿)에서지만 브라우저로 실행될 수 있다는 뜻이에요. Cordova 래퍼 안은 아니에요.

SSR/SSG에 관한 참고 (Note about SSR/SSG)

SSR/SSG용으로 빌드할 때는 $q.platform 형식만 사용하세요. 또는 서버 측일 때 이것도 사용할 수 있는 또 다른 예시예요:

import { Platform } from 'quasar'

// `ssrContext`에 접근해야 합니다
function (ssrContext) {
  const platform = import.meta.env.QUASAR_SERVER
    ? Platform.parseSSR(ssrContext)
    : Platform // 그 외엔 클라이언트에 있음

  // platform은 비-SSR 빌드에서의 전역 import와 동등함
}

ssrContext는 @quasar/app-vite 부트 파일에서 사용할 수 있어요. 그리고 @quasar/app-vite preFetch 기능에서도 파라미터로 제공돼요.

이 모든 이유는, 클라이언트 전용 앱에서는 모든 사용자가 브라우저에서 앱의 새 인스턴스를 사용하기 때문이에요. 서버 사이드 렌더링에서도 똑같이 원해요: 각 요청이 신선하고 격리된 앱 인스턴스를 가져야 해서 요청 간 상태 오염이 없어야 해요. 그래서 Platform은 각 요청에 별도로 바인딩되어야 해요.

더 알아보기 (Learn more)

Platform 플러그인은 $q.platform으로 브라우저·운영체제·모바일 래퍼·기능(터치, iframe 등)을 감지해요. Platform.is.*/Platform.has.*를 조합해 조건부 렌더링을 하고, SSR/SSG에서는 Platform.parseSSR(ssrContext)를 사용하세요.