การใช้ API เฉพาะเครือข่าย

เครือข่ายสื่อกลางอาจให้ API ใน SDK ของ Android และ iOS ของตัวเอง ทำให้สามารถปรับแต่งโฆษณาได้มากขึ้น คุณเรียกใช้ API เหล่านี้จาก Dart Code ได้โดยใช้ช่องทางของแพลตฟอร์ม

ตัวอย่างต่อไปนี้แสดงวิธีเรียกใช้ API ความเป็นส่วนตัวใน SDK สำหรับ Android และ iOS ของ AppLovin จาก Dart

สร้างช่องเมธอดใน Dart

สร้างแชแนลของเมธอดในโค้ด Dart Code ของคุณ:

/// 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

ตั้งค่าแชแนลของเมธอดเพื่อทำการเรียก API ไปยัง AppLovin SDK ใน Android ได้ดังนี้

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

ตั้งค่าแชแนลของเมธอดเพื่อทำการเรียก API ไปยัง AppLovin SDK ใน iOS ได้ดังนี้

@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

ใช้ MethodChannel ของคุณใน Dart

คุณใช้ MyMethodChannel เพื่อเรียกใช้ Privacy API ของ AppLovin จาก Dart ได้แล้ว โดยทำดังนี้

/// 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