中介广告联盟可能会在自己的 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 文档。