2016-07-21 7 views
3

React Native와 PushNotificationIOS를 테스트하는 것이 처음입니다. 하지만 checkpermission 콜이 안드로이드에서 오류를 일으키고 있습니다.안드로이드에서 React Native의 iOS 전용 구성 요소를 올바르게 사용하는 방법

ExceptionsManager.js:61 Cannot read property 'checkPermissions' of undefined 

나는 iOS에서만 구성 요소를 사용해야하기 때문에 이것을 추측하고 있습니다. iOS에서만 전화를 걸려면 OS 검사를 어떻게 추가합니까?

componentWillMount: function() { 
    //-- need an OS check here?? 
    PushNotificationIOS.checkPermissions((data)=> { 
     console.log("in comp will mount: checking for permission") 
     console.log(data.alert) 
     console.log(data.badge) 
+1

나는 아래 AKADER의 대답에 동의하지만, 다른 장소에서 당신이 원 - 수행해야하는 경우 off Platform 모듈에서 Platform 모듈에 대한 설명서를 확인해야합니다. https://facebook.github.io/react-native/docs/platform-specific-code.html –

답변

5

은 내가 제안하는 것은 별도의 파일에서 해당 플랫폼 특정 코드를 분할한다 :

여기 내 코드입니다.

React Native는 파일에 .ios가있을 때이를 감지합니다. 또는 .android. 확장 프로그램을 실행하고 다른 구성 요소에서 필요한 경우 관련 플랫폼 파일을로드하십시오.

const MyFile= require('./MyFile'); 

을이

componentWillMount: function() { 
    //-- it would call the logic of what ever platform was detected automatically 
    MyFile.CallWhatEver(); 

처럼 사용하고 플랫폼 특정 코드를 실행합니다 :

MyFile.ios.js 
MyFile.android.js 

그런 다음 다음과 같이 구성 요소를 필요로 할 수 있습니다.

는 또 다른 방법은

가 기본이 응용 프로그램이 실행되는 플랫폼을 감지하는 모듈을 제공 반응하는

플랫폼 모듈에게 있습니다. 검색 로직을 사용하여 플랫폼 특정 코드를 구현할 수 있습니다. 구성 요소의 작은 부분 만 특정 플랫폼 일 때이 옵션을 사용하십시오.

if(Platform.OS === 'ios') 

도 값을 받아 들일 수있는 platform.select있다

const Component = Platform.select({ 
    ios:() => //function goes here, 
    android:() => require('ComponentAndroid'), 
})(); 

링크 https://facebook.github.io/react-native/docs/platform-specific-code.html

+0

고마워! 플랫폼이 필요합니다. :) 더 많은 플랫폼 특정 코드가있을 때 별도의 파일을 시도합니다. –

+0

Np. 당신이 필요로하는 유일한 것이 었는지 확신 할 수 없었기 때문에 첫 번째 예를 추천했습니다. 제 대답을 받아주세요 :) –

관련 문제