첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
------------------------------------------------------------------------------------------------------------------------------------------------------
728x90
728x170

■ IMTAdmob 인터페이스를 사용해 애드몹(AdMob) 전면 광고를 사용하는 방법을 보여준다.

TestProject.zip
0.24MB

▶ TestProject.csproj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net6.0-windows10.0.19041.0</TargetFrameworks>
        <OutputType>Exe</OutputType>
        <RootNamespace>TestProject</RootNamespace>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>
        <ApplicationTitle>TestProject</ApplicationTitle>
        <ApplicationId>com.companyname.testproject</ApplicationId>
        <ApplicationIdGuid>307F785A-6EDE-4AEB-8813-DC0FFA886398</ApplicationIdGuid>
        <ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
        <ApplicationVersion>1</ApplicationVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
        <EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
    </PropertyGroup>
    <ItemGroup>
        <MauiIcon Include="Resources\appicon.svg" ForegroundFile="Resources\appiconfg.svg" Color="#512bd4" />
        <MauiSplashScreen Include="Resources\appiconfg.svg" Color="#512bd4" BaseSize="128,128" />
        <MauiImage Include="Resources\Images\*" />
        <MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
        <MauiFont Include="Resources\Fonts\*" />
        <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
    </ItemGroup>
    <ItemGroup Condition="'$(TargetFramework)' == 'net6.0-android'">
        <AndroidLibrary Include="Dependencies\user-messaging-platform-2.0.0.aar" Bind="false" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="Plugin.MauiMTAdmob" Version="1.0.1" />
    </ItemGroup>
</Project>

 

▶ Platforms/Android/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:allowBackup="true"
        android:icon="@android:mipmap/sym_def_app_icon"
        android:roundIcon="@mipmap/appicon_round"
        android:supportsRtl="true">
        <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-3940256099942544~3347511713" />
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET"             />
</manifest>

 

▶ Platforms/Android/MainActivity.cs

using Android.App;
using Android.Content.PM;
using Android.Gms.Ads;
using Android.OS;

namespace TestProject;

/// <summary>
/// 메인 액티비티
/// </summary>
[
    Activity
    (
        Theme                = "@style/Maui.SplashTheme",
        MainLauncher         = true,
        ConfigurationChanges = ConfigChanges.ScreenSize         |
                               ConfigChanges.Orientation        |
                               ConfigChanges.UiMode             |
                               ConfigChanges.ScreenLayout       |
                               ConfigChanges.SmallestScreenSize |
                               ConfigChanges.Density
    )
]
public class MainActivity : MauiAppCompatActivity
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Protected

    #region 생성시 처리하기 - OnCreate(savedInstanceState)

    /// <summary>
    /// 생성시 처리하기
    /// </summary>
    /// <param name="savedInstanceState">저장 인스턴스 상태</param>
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        MobileAds.Initialize(this);
    }

    #endregion
}

 

▶ Platforms/iOS/AppDelegate.cs

using Foundation;
using Google.MobileAds;
using UIKit;

namespace TestProject;

/// <summary>
/// 앱 대리자
/// </summary>
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 런칭 완료시 처리하기 - FinishedLaunching(application, launchOptions)

    /// <summary>
    /// 런칭 완료시 처리하기
    /// </summary>
    /// <param name="application">애플리케이션</param>
    /// <param name="launchOptions">런치 옵션</param>
    /// <returns>처리 결과</returns>
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        MobileAds.SharedInstance.Start(CompletionHandler);

        return base.FinishedLaunching(application, launchOptions);
    }

    #endregion

    ////////////////////////////////////////////////////////////////////////////////////////// Protected

    #region MUI 앱 생성하기 - CreateMauiApp()

    /// <summary>
    /// MUI 앱 생성하기
    /// </summary>
    /// <returns>MUI 앱</returns>
    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

    #endregion

    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 완료 처리기 - CompletionHandler(status)

    /// <summary>
    /// 완료 처리기
    /// </summary>
    /// <param name="status">상태</param>
    private void CompletionHandler(InitializationStatus status)
    {
    }

    #endregion
}

 

▶ MauiProgram.cs

using Plugin.MauiMTAdmob;

namespace TestProject;

/// <summary>
/// MAUI 프로그램
/// </summary>
public static class MauiProgram
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Public

    #region MAUI 앱 생성하기 - CreateMauiApp()

    /// <summary>
    /// MAUI 앱 생성하기
    /// </summary>
    /// <returns>MAUI 앱</returns>
    public static MauiApp CreateMauiApp()
    {
        MauiAppBuilder builder = MauiApp.CreateBuilder();

        builder
            .UseMauiApp<App>()
            .UseMauiMTAdmob()
            .ConfigureFonts
            (
                fontCollection =>
                {
                    fontCollection.AddFont("OpenSans-Regular.ttf" , "OpenSansRegular" );
                    fontCollection.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                }
            );

        return builder.Build();
    }

    #endregion
}

 

▶ MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="TestProject.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
    <VerticalStackLayout
        HorizontalOptions="Center"
        VerticalOptions="Center"
        Spacing="10">
        <Label
            HorizontalOptions="Center"
            FontSize="32"
            FontAttributes="Bold"
            Text="애드몹 전면 광고" />
        <Button x:Name="showButton"
            HorizontalOptions="Center"
            Text="광고 표시하기" />
    </VerticalStackLayout>
</ContentPage>

 

▶ MainPage.xaml.cs

using Plugin.MauiMTAdmob;
using Plugin.MauiMTAdmob.Extra;

namespace TestProject;

/// <summary>
/// 메인 페이지
/// </summary>
public partial class MainPage : ContentPage
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainPage()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainPage()
    {
        InitializeComponent();

        CrossMauiMTAdmob.Current.TagForChildDirectedTreatment = MTTagForChildDirectedTreatment.TagForChildDirectedTreatmentUnspecified;
        CrossMauiMTAdmob.Current.TagForUnderAgeOfConsent      = MTTagForUnderAgeOfConsent.TagForUnderAgeOfConsentUnspecified;
        CrossMauiMTAdmob.Current.MaxAdContentRating           = MTMaxAdContentRating.MaxAdContentRatingG;

        this.showButton.Clicked += showButton_Clicked;
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private

    #region 광고 표시하기 버튼 클릭시 처리하기 - showButton_Clicked(sender, e)

    /// <summary>
    /// 광고 표시하기 버튼 클릭시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void showButton_Clicked(object sender, EventArgs e)
    {
        bool isLoaded = CrossMauiMTAdmob.Current.IsInterstitialLoaded();

        if(!isLoaded)
        {
            CrossMauiMTAdmob.Current.LoadInterstitial("ca-app-pub-3940256099942544/1033173712");
        }

        CrossMauiMTAdmob.Current.ShowInterstitial();
    }

    #endregion
}
728x90
그리드형(광고전용)
Posted by icodebroker
,