0.3.0 • Published 21 days ago

adrop-ads-react-native v0.3.0

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
21 days ago

adrop-ads-react-native

Adrop Ads SDK for React Native

npm

Prerequisites

 

Step 1: Create a Adrop project

Before you can add Adrop to your React Native app, you need to create a Adrop project to connect to your app.

Step 2: Register your app with Adrop

To use Adrop in your React Native app, you need to register your app with your Adrop project. Registering your app is often called "adding" your app to your project.

Note

Make sure to enter the package name that your app is actually using. The package name value is case-sensitive, and it cannot be changed for this Adrop Android app after it's registered with your Adrop project.

  1. Go to the Adrop console.
  2. In the center of the project app page, click the React icon button to launch the setup workflow.
  3. Enter your app's package name in the React package name field.
    • A package name uniquely identifies your app on the device and in the Google Play Store or App Store.
    • A package name is often referred to as an application ID.
    • Be aware that the package name value is case-sensitive, and it cannot be changed for this Adrop React app after it's registered with your Adrop project.
  4. Enter other app information: App nickname.
    • App nickname: An internal, convenience identifier that is only visible to you in the Adrop console
  5. Click Register app and then Android and Apple apps will be created respectively.

Step 3: Add a Adrop configuration file

Android

  1. Download adrop_service.json to obtain your Adrop Android platforms config file.
  2. Move your config file into your assets directory. android/app/src/main/assets/adrop_service.json

iOS

  1. Download adrop_service.json to obtain your Adrop Apple platforms config file.
  2. Move your config file into the root of your Xcode project. If prompted, select to add the config file to all targets.

Step 4: Add Adrop library to your your app

  1. From your React Native project directory, run the following command to install the plugin.

    npm install adrop-ads-react-native
  2. Altering CocoaPods to use frameworks Open the file ./ios/Podfile and add this line inside your targets

    ```bash
    use_frameworks!
    ```
    
    > **Note**
    >
    > **adrop-ads-react-native** uses use_frameworks, which has compatibility issues with Flipper.
    >
    > **Flipper**: use_frameworks [is not compatible with Flipper](https://github.com/reactwg/react-native-releases/discussions/21#discussioncomment-2924919). You need to disable Flipper by commenting out the :flipper_configuration line in your Podfile.
  3. Autolinking & rebuilding

    Once the above steps have been completed, the React Native Adrop library must be linked to your project and your application needs to be rebuilt.

    Users on React Native 0.60+ automatically have access to "autolinking", requiring no further manual installation steps. To automatically link the package, rebuild your project:

    # Android apps
    npx react-native run-android
    
    # iOS apps
    cd ios/
    pod install --repo-update
    cd ..
    npx react-native run-ios

Step 5: Initialize Adrop in your app

The final step is to add initialization code to your application. 1. Import the Adrop library and initialize.

```js
import { Adrop } from 'adrop-ads-react-native';

// ..
Adrop.initialize(production);
```

(Optional) Troubleshooting

# Add this line to your Podfile
use_frameworks!

# ...
post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|

    #...
    # Add this line to your Podfile
    config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
    end
  end
end

Creating Ad units

To create a new Ad unit: 1. From the left navigation menu, select Ad Units. 2. Select Create Ad unit to bring up the ad unit builder. 3. Enter an Ad unit name, then select your app (iOS or Android) and Ad format (Banner, Interstitial, or Rewarded). 4. Select Create to save your Ad unit.

Ad unit ID

The Ad unit’s unique identifier to reference in your code. This setting is read-only.

Note These are unit ids for test

  • PUBLIC_TEST_UNIT_ID_320_50
  • PUBLIC_TEST_UNIT_ID_375_80
  • PUBLIC_TEST_UNIT_ID_320_100
  • PUBLIC_TEST_UNIT_ID_INTERSTITIAL
  • PUBLIC_TEST_UNIT_ID_REWARDED

Display Ads

Initialize AdropBanner with Ad unit ID, then load ad.

const YourComponent: React.FC = () => {
    const ref = useRef(null)

    const reload = () => {
        ref.current?.load()
    }

    return (
        <View>
            <Button title="reload" onPress={reload}/>
            <AdropBanner
                ref={ref}
                unitId={unitId}
                style={{
                    width: Dimensions.get('window').width,
                    height: 80
                }}
            />
        </View>
    )
}

Step 1: (Optional) Construct event listener

const listener = {
        onAdReceived: (ad: AdropInterstitialAd) =>
            console.log(`Adrop interstitial Ad load with unitId ${ad.unitId}!`),
        onAdFailedToReceive: (ad: AdropInterstitialAd, errorCode: string) =>
            console.log(`error in ${ad.unitId} while load: ${errorCode}`),
        onAdFailedToShowFullScreen: (ad: AdropInterstitialAd, errorCode: string) =>
            console.log(`error in ${ad.unitId} while showing: ${errorCode}`),
        ...
    }

Step 2: Display an interstitial ad

const YourComponent: React.FC = () => {
    const [interstitialAd, setInterstitialAd] = useState<AdropInterstitialAd>(null)

    useEffect(() => {
        let adropInterstitialAd = new AdropInterstitialAd('YOUR_UNIT_ID')
        adropInterstitialAd.listener = listener
        adropInterstitialAd.load()
        setInterstitialAd(adropInterstitialAd)
    }, []);

    const show = () => {
        if (interstitialAd?.isLoaded) {
            interstitialAd?.show()
        } else {
            console.log('interstitial ad is loading...')
        }
    }

    return (
        <View>
            <Button title="display ad" onPress={show}/>
        </View>
    )

}

AdropInterstitialAd must be destroyed of when access to it is no longer needed.

interstitialAd.destroy()
const YourComponent: React.FC = () => {
    const { load, show, isLoaded } =
        useAdropInterstitialAd('YOUR_UNIT_ID')

    const handleShow = () => {
        if (isLoaded) show()
    }

    return (
        <View>
            <Button title="load ad" onPress={load}/>
            <Button title="display ad" onPress={handleShow}/>
        </View>
    )
}

Step 1: (Optional) Construct event listener

const listener = {
        onAdReceived: (ad: AdropRewardedAd) =>
            console.log(`Adrop rewarded Ad load with unitId ${ad.unitId}!`),
        onAdFailedToReceive: (ad: AdropRewardedAd, errorCode: string) =>
            console.log(`error in ${ad.unitId} while load: ${errorCode}`),
        onAdFailedToShowFullScreen: (ad: AdropRewardedAd, errorCode: string) =>
            console.log(`error in ${ad.unitId} while showing: ${errorCode}`),
        onAdEarnRewardHandler: (ad: AdropRewardedAd, type: number, amount: number) =>
            console.log(`Adrop rewarded Ad earn rewards: ${ad.unitId}, ${type}, ${amount}`),
        ...
    }

Step 2: Display a rewarded ad

const YourComponent: React.FC = () => {
    const [rewardedAd, setRewardedAd] = useState<AdropRewardedAd>(null)

    useEffect(() => {
        let adropRewardedAd = new AdropRewardedAd('YOUR_UNIT_ID')
        adropRewardedAd.listener = listener
        adropRewardedAd.load()
        setRewardedAd(adropRewardedAd)
    }, []);

    const show = () => {
        if (rewardedAd?.isLoaded) {
            rewardedAd?.show()
        } else {
            console.log('rewarded ad is loading...')
        }
    }

    return (
        <View>
            <Button title="display ad" onPress={show}/>
        </View>
    )

}

AdropRewardedAd must be destroyed of when access to it is no longer needed.

rewardedAd.destroy()
const YourComponent: React.FC = () => {
    const { load, show, isLoaded } =
        useAdropRewardedAd('YOUR_UNIT_ID')

    const handleShow = () => {
        if (isLoaded) show()
    }

    return (
        <View>
            <Button title="load ad" onPress={load}/>
            <Button title="display ad" onPress={handleShow}/>
        </View>
    )
}
0.3.0

21 days ago

0.2.1

4 months ago

0.2.0

4 months ago

0.1.4

5 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago