Flutter广告集成插件ironsource_adpluginx的使用
Flutter广告集成插件ironSource Flutter插件的使用
概述
ironSource Flutter 插件是一个用于集成 ironSource SDK 的桥梁插件。通过此插件,开发者可以在 Flutter 应用中轻松集成广告功能。
安装
首先,在项目的 pubspec.yaml
文件中添加插件依赖:
flutter pub get ironsource_adpluginx
Android 配置
Gradle 依赖
确保在 PROJECT_ROOT/android/app/build.gradle
文件中添加以下依赖项:
dependencies {
...
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.0'
implementation 'com.google.android.gms:play-services-basement:18.0.0'
implementation 'com.google.android.gms:play-services-appset:16.0.1'
}
AndroidManifest.xml
在 AndroidManifest.xml
文件中添加以下配置以避免冲突:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourapp">
<application
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
tools:replace="android:label">
<!-- 其他配置 -->
</application>
</manifest>
iOS 配置
SKAdNetwork 支持
在 info.plist
文件中添加以下配置:
<key>SKAdNetworkItems</key>
<array>
<dict>
<key>SKAdNetworkIdentifier</key>
<string>su67r6k2v3.skadnetwork</string>
</dict>
</array>
App Transport Security 设置
在 info.plist
文件中添加以下配置以允许 HTTP 调用:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
App Tracking Transparency (ATT) 提示
实现 ATT 提示以请求用户授权:
Future<void> checkATT() async {
final currentStatus = await ATTrackingManager.getTrackingAuthorizationStatus();
if (currentStatus == ATTStatus.NotDetermined) {
final returnedStatus = await ATTrackingManager.requestTrackingAuthorization();
print('ATTStatus returned: $returnedStatus');
}
return;
}
一般使用
实现监听器
创建一个类来实现广告回调:
class YourDartClass with IronSourceRewardedVideoListener {
/// 奖励视频广告点击事件
[@override](/user/override)
void onRewardedVideoAdClicked(IronSourceRVPlacement placement) {
print('onRewardedVideoAdClicked Placement:$placement');
}
[@override](/user/override)
void onRewardedVideoAdClosed() {
print("onRewardedVideoAdClosed");
}
[@override](/user/override)
void onRewardedVideoAdEnded() {
print("onRewardedVideoAdClosed");
}
[@override](/user/override)
void onRewardedVideoAdOpened() {
print("onRewardedVideoAdOpened");
}
[@override](/user/override)
void onRewardedVideoAdRewarded(IronSourceRVPlacement placement) {
print("onRewardedVideoAdRewarded Placement: $placement");
}
[@override](/user/override)
void onRewardedVideoAdShowFailed(IronSourceError error) {
print("onRewardedVideoAdShowFailed Error:$error");
}
[@override](/user/override)
void onRewardedVideoAdStarted() {
print("onRewardedVideoAdStarted");
}
[@override](/user/override)
void onRewardedVideoAvailabilityChanged(bool isAvailable) {
print('onRewardedVideoAvailabilityChanged: $isAvailable');
// 更改奖励视频广告显示按钮的状态
setState(() {
_isRVAvailable = isAvailable;
});
}
}
初始化插件
初始化 ironSource 插件:
Future<void> initIronSource() async {
final appKey = Platform.isAndroid
? ANDROID_APP_KEY
: Platform.isIOS
? IOS_APP_KEY
: throw Exception("Unsupported Platform");
try {
IronSource.setFlutterVersion('2.8.1'); // 必须在初始化前调用
IronSource.validateIntegration();
IronSource.setRVListener(this);
await IronSource.setAdaptersDebug(true);
await IronSource.shouldTrackNetworkState(true);
// 不要使用 GAID 或 IDFA。
await IronSource.setUserId("unique-application-user-id");
await IronSource.init(appKey: appKey, adUnits: [IronSourceAdUnit.RewardedVideo]);
} on PlatformException catch (e) {
print(e);
}
}
显示广告
调用方法显示广告:
Future<void> _showRVOnClick() async {
if (await IronSource.isRewardedVideoAvailable()) {
IronSource.showRewardedVideo();
}
}
广告位定位
位置
定义位置枚举:
enum IronSourceBannerPosition {
Top,
Center,
Bottom,
}
偏移量
设置垂直偏移量:
IronSource.loadBanner(
size: IronSourceBannerSize.BANNER,
position: IronSourceBannerPosition.Bottom,
verticalOffset: -50, // 添加 50dp/50point 的底部边距
placementName: 'YOUR_PLACEMENT');
中介
可以通过添加适配器/SDK 来使用 ironSource LevelPlay 的中介功能。请参考官方文档进行额外设置。
Android
请遵循 ironSource 知识中心文档进行额外设置。确保在 YOUR_PROJECT/android/app/build.gradle
和 YOUR_PROJECT/android/app/src/main/AndroidManifest.xml
中添加必要的配置。
iOS
请遵循 ironSource 知识中心文档进行额外设置。确保在 YOUR_PROJECT/ios/Podfile: target 'Runner'
和 YOUR_PROJECT/ios/Runner/info.plist
中添加必要的配置。
示例代码
example/lib/main.dart
import 'package:flutter/material.dart';
import './app_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(home: AppScreen());
}
}
更多关于Flutter广告集成插件ironsource_adpluginx的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter广告集成插件ironsource_adpluginx的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
ironsource_adpluginx
是一个用于在 Flutter 应用中集成 IronSource 广告的插件。IronSource 是一个广告聚合平台,支持多种广告格式,如插屏广告、激励视频广告、横幅广告等。通过 ironsource_adpluginx
,开发者可以轻松地在 Flutter 应用中集成这些广告。
以下是使用 ironsource_adpluginx
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 ironsource_adpluginx
插件的依赖:
dependencies:
flutter:
sdk: flutter
ironsource_adpluginx: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来获取依赖。
2. 初始化 IronSource
在你的 Flutter 应用中,首先需要初始化 IronSource。通常,这可以在 main.dart
文件中完成:
import 'package:flutter/material.dart';
import 'package:ironsource_adpluginx/ironsource_adpluginx.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 IronSource
await IronSource.init(
appKey: 'YOUR_APP_KEY', // 替换为你的 IronSource App Key
adUnits: [IronSourceAdUnit.REWARDED_VIDEO, IronSourceAdUnit.INTERSTITIAL],
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('IronSource Ad Example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: () {
// 展示激励视频广告
IronSource.showRewardedVideo();
},
child: Text('Show Rewarded Video'),
),
ElevatedButton(
onPressed: () {
// 展示插屏广告
IronSource.showInterstitial();
},
child: Text('Show Interstitial'),
),
],
),
),
);
}
}
3. 处理广告事件
你可以通过监听广告事件来处理广告的展示、关闭、奖励等事件。例如:
IronSource.setRewardedVideoListener(
onRewardedVideoAdOpened: () {
print('Rewarded Video Ad Opened');
},
onRewardedVideoAdClosed: () {
print('Rewarded Video Ad Closed');
},
onRewardedVideoAdRewarded: (placement) {
print('Rewarded Video Ad Rewarded: $placement');
},
);
IronSource.setInterstitialListener(
onInterstitialAdOpened: () {
print('Interstitial Ad Opened');
},
onInterstitialAdClosed: () {
print('Interstitial Ad Closed');
},
onInterstitialAdShowSucceeded: () {
print('Interstitial Ad Show Succeeded');
},
);
4. 加载和展示广告
在初始化之后,你可以加载和展示广告。例如,加载和展示激励视频广告:
IronSource.loadRewardedVideo();
IronSource.showRewardedVideo();
对于插屏广告:
IronSource.loadInterstitial();
IronSource.showInterstitial();
5. 处理生命周期
为了确保广告在应用生命周期中的正确处理,你需要在 WidgetsBindingObserver
中处理应用的生命周期事件:
class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
IronSource.onResume();
break;
case AppLifecycleState.paused:
IronSource.onPause();
break;
default:
break;
}
}
}
6. 测试
在开发阶段,你可以使用 IronSource 的测试模式来确保广告正常工作:
IronSource.setMetaData('is_test_suite', 'true');