使用广告资源网专有 API
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
中介广告联盟可能会在其自己的 Android 和 iOS SDK 中提供 API,以便进一步自定义广告。您可以使用平台渠道从 Dart 代码中调用这些 API。
以下示例展示了如何从 Dart 调用 AppLovin 的 Android 和 iOS SDK 中的隐私权 API。
在 Dart 中创建方法渠道
在 Dart 代码中,创建方法渠道:
/// Wraps a method channel that makes calls to AppLovin privacy APIs.
class MyMethodChannel {
final MethodChannel _methodChannel =
MethodChannel('com.example.mediationexample/mediation-channel');
/// Sets whether the user is age restricted in AppLovin.
Future<void> setAppLovinIsAgeRestrictedUser(bool isAgeRestricted) async {
return _methodChannel.invokeMethod(
'setIsAgeRestrictedUser',
{
'isAgeRestricted': isAgeRestricted,
},
);
}
/// Sets whether we have user consent for the user in AppLovin.
Future<void> setHasUserConsent(bool hasUserConsent) async {
return _methodChannel.invokeMethod(
'setHasUserConsent',
{
'hasUserConsent': hasUserConsent,
},
);
}
}
修改 Android 代码
设置方法渠道,以便在 Android 中向 AppLovin SDK 发出 API 调用:
public class MainActivity extends FlutterActivity {
private static final String CHANNEL_NAME =
"com.example.mediationexample/mediation-channel";
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
// Set up a method channel for calling APIs in the AppLovin SDK.
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
.setMethodCallHandler(
(call, result) -> {
switch (call.method) {
case "setIsAgeRestrictedUser":
AppLovinPrivacySettings.setIsAgeRestrictedUser(call.argument("isAgeRestricted"), context);
result.success(null);
break;
case "setHasUserConsent":
AppLovinPrivacySettings.setHasUserConsent(call.argument("hasUserConsent"), context);
result.success(null);
break;
default:
result.notImplemented();
break;
}
}
);
}
}
修改 iOS 代码
设置方法渠道,以便在 iOS 中向 AppLovin SDK 发出 API 调用:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
// Set up a method channel for calling methods in 3P SDKs.
FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
FlutterMethodChannel* methodChannel = [FlutterMethodChannel
methodChannelWithName:@"com.example.mediationexample/mediation-channel"
binaryMessenger:controller.binaryMessenger];
[methodChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
if ([call.method isEqualToString:@"setIsAgeRestrictedUser"]) {
[ALPrivacySettings setIsAgeRestrictedUser:call.arguments[@"isAgeRestricted"]];
result(nil);
} else if ([call.method isEqualToString:@"setHasUserConsent"]) {
[ALPrivacySettings setHasUserConsent:call.arguments[@"hasUserConsent"]];
result(nil);
} else {
result(FlutterMethodNotImplemented);
}
}];
}
@end
在 Dart 中使用 MethodChannel
现在,您可以使用 MyMethodChannel
从 Dart 调用 AppLovin 隐私权 API:
/// An example widget for the home page of your app.
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
// Keep a reference to your MyMethodChannel.
static MyMethodChannel platform = MyMethodChannel();
@override
void initState() {
super.initState();
_updateAppLovinSettingsAndLoadAd();
}
Future<void> _updateAppLovinSettingsAndLoadAd() async {
// Update the AppLovin settings before loading an ad.
await platform.setAppLovinIsAgeRestrictedUser(true);
await platform.setHasUserConsent(false);
_loadAd();
}
void _loadAd() {
// TODO: Load an ad.
};
@override
Widget build(BuildContext context) {
// TODO: Build your widget.
}
}
如需详细了解其他网络支持的 API,请参阅 Android 和 iOS 文档。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eMediation networks sometimes offer custom ad features through their Android/iOS SDKs, accessible via platform channels in Flutter.\u003c/p\u003e\n"],["\u003cp\u003eThis guide demonstrates calling privacy APIs in AppLovin's SDK from Dart code using a method channel for Android and iOS.\u003c/p\u003e\n"],["\u003cp\u003eYou need to create a method channel in Dart, modify your native Android/iOS code to handle the calls, and then use the channel to invoke the APIs.\u003c/p\u003e\n"],["\u003cp\u003eDetailed steps are provided for setting up the method channel and invoking the \u003ccode\u003esetIsAgeRestrictedUser\u003c/code\u003e and \u003ccode\u003esetHasUserConsent\u003c/code\u003e APIs for AppLovin.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the Android and iOS network documentation for information on specific APIs supported by other mediation networks.\u003c/p\u003e\n"]]],["Mediation networks' APIs for ad customization can be accessed via platform channels. A method channel, `MyMethodChannel`, is created in Dart to interact with AppLovin's privacy APIs. Dart code calls methods like `setAppLovinIsAgeRestrictedUser` and `setHasUserConsent`. In Android and iOS, method channels handle calls, using `AppLovinPrivacySettings` and `ALPrivacySettings` respectively, to set user age restriction and consent. Dart code can then update AppLovin settings and load ads, after setting user restriction and consent settings.\n"],null,["# Use network specific APIs\n\nMediation networks may provide APIs in their own Android and iOS SDKs that allow\nfor further ads customization. You can call these APIs from dart code by using a\n[platform channel](https://flutter.dev/docs/development/platform-integration/platform-channels).\n\nThe following sample shows you how to call privacy APIs in AppLovin's\n[Android](/admob/android/mediation/applovin#eu_consent_and_gdpr) and\n[iOS](/admob/android/mediation/applovin#eu_consent_and_gdpr) SDKs from Dart.\n\nCreate a method channel in dart\n-------------------------------\n\nIn your dart code, create a method channel: \n\n /// Wraps a method channel that makes calls to AppLovin privacy APIs.\n class MyMethodChannel {\n final MethodChannel _methodChannel =\n MethodChannel('com.example.mediationexample/mediation-channel');\n\n /// Sets whether the user is age restricted in AppLovin.\n Future\u003cvoid\u003e setAppLovinIsAgeRestrictedUser(bool isAgeRestricted) async {\n return _methodChannel.invokeMethod(\n 'setIsAgeRestrictedUser',\n {\n 'isAgeRestricted': isAgeRestricted,\n },\n );\n }\n\n /// Sets whether we have user consent for the user in AppLovin.\n Future\u003cvoid\u003e setHasUserConsent(bool hasUserConsent) async {\n return _methodChannel.invokeMethod(\n 'setHasUserConsent',\n {\n 'hasUserConsent': hasUserConsent,\n },\n );\n }\n }\n\nModify your Android code\n------------------------\n\nSet up a method channel to make API calls to the AppLovin SDK in Android: \n\n public class MainActivity extends FlutterActivity {\n private static final String CHANNEL_NAME =\n \"com.example.mediationexample/mediation-channel\";\n\n @Override\n public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {\n super.configureFlutterEngine(flutterEngine);\n\n // Set up a method channel for calling APIs in the AppLovin SDK.\n new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)\n .setMethodCallHandler(\n (call, result) -\u003e {\n switch (call.method) {\n case \"setIsAgeRestrictedUser\":\n AppLovinPrivacySettings.setIsAgeRestrictedUser(call.argument(\"isAgeRestricted\"), context);\n result.success(null);\n break;\n case \"setHasUserConsent\":\n AppLovinPrivacySettings.setHasUserConsent(call.argument(\"hasUserConsent\"), context);\n result.success(null);\n break;\n default:\n result.notImplemented();\n break;\n }\n }\n );\n }\n }\n\nModify your iOS code\n--------------------\n\nSet up a method channel to make API calls to the AppLovin SDK in iOS: \n\n @implementation AppDelegate\n\n - (BOOL)application:(UIApplication *)application\n didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\n [GeneratedPluginRegistrant registerWithRegistry:self];\n\n // Set up a method channel for calling methods in 3P SDKs.\n FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;\n\n FlutterMethodChannel* methodChannel = [FlutterMethodChannel\n methodChannelWithName:@\"com.example.mediationexample/mediation-channel\"\n binaryMessenger:controller.binaryMessenger];\n [methodChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {\n if ([call.method isEqualToString:@\"setIsAgeRestrictedUser\"]) {\n [ALPrivacySettings setIsAgeRestrictedUser:call.arguments[@\"isAgeRestricted\"]];\n result(nil);\n } else if ([call.method isEqualToString:@\"setHasUserConsent\"]) {\n [ALPrivacySettings setHasUserConsent:call.arguments[@\"hasUserConsent\"]];\n result(nil);\n } else {\n result(FlutterMethodNotImplemented);\n }\n }];\n }\n @end\n\nUse your MethodChannel in Dart\n------------------------------\n\nNow you can use your `MyMethodChannel` to call AppLovin privacy APIs from Dart: \n\n /// An example widget for the home page of your app.\n class HomePage extends StatefulWidget {\n @override\n _HomePageState createState() =\u003e _HomePageState();\n }\n\n class _HomePageState extends State\u003cHomePage\u003e {\n // Keep a reference to your MyMethodChannel.\n static MyMethodChannel platform = MyMethodChannel();\n @override\n void initState() {\n super.initState();\n\n _updateAppLovinSettingsAndLoadAd();\n }\n Future\\\u003cvoid\\\u003e _updateAppLovinSettingsAndLoadAd() async {\n // Update the AppLovin settings before loading an ad.\n await platform.setAppLovinIsAgeRestrictedUser(true);\n await platform.setHasUserConsent(false);\n _loadAd();\n }\n void _loadAd() {\n // TODO: Load an ad.\n };\n\n @override\n Widget build(BuildContext context) {\n // TODO: Build your widget.\n }\n }\n\nMore details on the APIs supported by other networks can be found\nin the [Android](/admob/android/choose-networks#network_details) and\n[iOS](/admob/ios/choose-networks#network_details) documentation."]]