네트워크별 API 사용

미디에이션 네트워크는 추가적인 광고 맞춤설정을 허용하는 자체 Android 및 iOS SDK에 API를 제공할 수 있습니다. 플랫폼 채널을 사용하여 Dart 코드에서 이러한 API를 호출할 수 있습니다.

다음 샘플은 AppLovin의 AndroidiOS SDK에서 Dart의 개인 정보 보호 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에 대한 자세한 내용은 AndroidiOS 문서를 참조하세요.