# AdMoreSDK-flutter
**Repository Path**: juquanyou/ad-more-sdk-flutter
## Basic Information
- **Project Name**: AdMoreSDK-flutter
- **Description**: AdMoreSDK-flutter
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2023-09-27
- **Last Updated**: 2023-11-12
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# AdMoreSDK-flutter
## 集成插件
1. 拷贝示例工程中 `lib/plugins` 的 `admore_flutter` 插件到开发工程的 `lib/plugins` 目录下。
2. 拷贝示例工程中 `lib/page` 文件夹到开发工程的 `lib` 目录下。
3. 配置 `pbuspec.yaml` 集成插件
```
dependencies:
flutter:
sdk: flutter
admore_flutter:
path: lib/plugins/admore_flutter
```
## 安卓配置
1. 拷贝libs下所有文件到项目android/libs下 修改 minSdkVersion 21 并配置build.gradle如下
```
android{
dependencies {
implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.android.support:appcompat-v7:28.0.0'
}
repositories {
flatDir {
dirs 'libs'
}
}
}
```
2. 新建 App.java 继承FlutterApplication如项目示例并配置AndroidManifest
App如下
```
public class App extends FlutterApplication{
onCreate(Bundel save){
...
MultiDex.install(this)
}
}
```
AndroidManifest文件如下
```
...
```
3. 混淆配置
拷贝 android/app 目录下 proguard-rules.pro 到你项目中对应的 android/app 目录下
配置混淆文件
```
buildTypes {
release {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
debug {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
```
4. Android的广告位slotId和初始化appkId不与iOS共用,见下表
| 广告类型 | appId | 5407100 |
| :--------: | :----: | :-------: |
| banner广告 | slotId | 102383511 |
| 插屏广告 | slotId | 102382906 |
| 激励广告 | slotId | 102381673 |
| 开屏广告 | slotId | 102381470 |
| 开屏广告 | ritId | 888355782 |
5. json config 配置
Android的配置文件config,自动加载,放置到项目assets根目录下,并命名为 admore_android_config.json
## iOS配置
1. 执行flutter pub get后,进入`ios`文件夹执行pod install
2. 在AppDelegate里注册
**swift版本**
```swift
import UIKit
import Flutter
//需要添加的头文件
import AdSupport
import AppTrackingTransparency
import admore_flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
//注册组件
IOSFlutterPlatformViewFactory.register(with: self.registrar(forPlugin: "flutter_plugin_ad_banner")!)
let path = Bundle.main.path(forResource: "admore_ios_config", ofType: nil);
AdmoreFlutterPlugin.shareInstance().initSDK(self, with: self.window, configPath: path);
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationDidBecomeActive(_ application: UIApplication) {
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization(completionHandler: { status in
if(status == .authorized)
{
let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString;
print(idfa);
}else
{
print("请在设置-隐私-跟踪中允许App请求跟踪");
}
})
} else {
if(ASIdentifierManager.shared().isAdvertisingTrackingEnabled)
{
let idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString;
print(idfa);
}else
{
print("请在设置-隐私-跟踪中允许App请求跟踪");
}
}
}
}
```
**OC版本**
```objc
#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"
//需要添加的头文件
#import
#import
#import
#import
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *configPath = [[NSBundle mainBundle] pathForResource:@"admore_ios_config" ofType:nil];
[[AdmoreFlutterPlugin shareInstance] initSDK:self withWindow:self.window configPath:configPath];
[IOSFlutterPlatformViewFactory registerWithRegistrar:[self registrarForPlugin:@"flutter_plugin_ad_banner"]];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (@available(iOS 14, *)) {
[ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) {
if (status == ATTrackingManagerAuthorizationStatusAuthorized) {
NSString *idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
NSLog(@"%@",idfa);
} else {
NSLog(@"请在设置-隐私-跟踪中允许App请求跟踪");
}
}];
} else {
if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
NSString *idfa = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
NSLog(@"%@",idfa);
} else {
NSLog(@"请在设置-隐私-广告中打开广告跟踪功能");
}
}
}
@end
```
3. iOS的广告位slotId和初始化appkId不与安卓共用,见下表
| 广告类型 | appId | 5407557 |
| :--------: | :----: | :-------: |
| banner广告 | slotId | 102381576 |
| 插屏广告 | slotId | 102382333 |
| 激励广告 | slotId | 102382433 |
| 开屏广告 | slotId | 102381758 |
| 开屏广告 | ritId | 888358139 |
4. 参照demo中的ios/Runner/info.plist文件配置,将对应的键值对添加到自己的项目的info.plist文件中。
| SKAdNetworkItems |
| ----------------------------------------------------------- |
| App Transport Security Settings |
| Privacy - Location Always and When In Use Usage Description |
| Privacy - Location When In Use Usage Description |
| Privacy - Location Always Usage Description |
| Privacy - Tracking Usage Description |
5. 将**admore_ios_config** 文件添加到自己的项目中。(文件名不改,如果修改了,Appdelegate中的名字也要跟着修改)
6. 模拟器运行报错``头文件找不到的情况
[CP] Unable to find matching .xcframework slice in 'ios-x86_64-simulator ios-arm64' for the current build architectures (arm64 x86_64).
解决步骤:
1.先到ios文件夹找到Podfile文件,在Podfile文件的最下面修改如下代码
```
//添加前
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
end
//添加后
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |build_configuration|
build_configuration.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64 i386'
end
end
end
```
2. Podfile文件最上面`platform :ios, '11.0'`打开
3. 执行`flutter clean && flutter pub get`
4. 执行`cd ios`
5. 执行`arch -x86_64 pod update`
6. 执行`arch -x86_64 pod install`
## 广告使用
可以直接参考 `lib/main.dart` 文件
### 监听事件回调
在sdk初始化之前,先调用监听事件回调的方法,详情参考 `lib/main.dart - setAdEvent()` ,事件类型和事件来源可参考`lib/plugins/admore_flutter/lib/admore_flutter_ad_event.dart`
```dart
//监听事件回调
Future setAdEvent() async {
setState(() {
_adEvent = '设置成功';
});
AdmoreFlutter.onEventListener((event)
{
setState(() {
_adEvent = 'from:${event.from} type:${event.eventType} msg:${event.msg}';
//SDK初始化成功
if (event.from == 'sdkManager' && event.eventType == "onSDKLoadSuccess") {
//预加载广告: type固定4种 bannerAd interstitialAd rewardVideoAd splashAd
//可自行选择需要进行缓存的广告
List ads = [{
'type':'bannerAd',
'slotId':bannerId
},{
'type':'interstitialAd',
'slotId':fullScreenInterstitialId
},{
'type':'rewardVideoAd',
'slotId':rewardVideoID
},{
'type':'splashAd',
'slotId':splashId,
'ritId':splashRitId
}];
//预加载
preLoadAd(ads);
//加载开屏广告
showSplashAd();
}
print(_adEvent);
});
});
}
```
### 初始化
```
详情参考 /lib/main.dart- init()
建议一进来app的`initState` 生命周期就要初始化sdk(必须在调用广告之前)**
AdmoreFlutter.initAd(appId)
```
## Banner广告
```
详情参考 /lib/page/banner_page.dart 或者 lib/main.dart
BannerAdWidget(slotId: slotId)
```
##插屏广告
```dart
/// 展示插屏广告
Future showInterstitialAd() async {
try {
bool result = await AdmoreFlutter.showInterstitialAd(fullScreenInterstitialId);
_result = "插屏广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result = "插屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
```
## 视频激励广告
```dart
//展示激励视频
Future showRewardVideoAd() async {
try {
bool result = await AdmoreFlutter.showRewardVideoAd(rewardVideoID);
_result = "激励视频广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result = "激励视频广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
```
## 开屏广告
```dart
//展示开屏
Future showSplashAd() async {
try {
bool result = await AdmoreFlutter.showSplashAd(splashId,splashRitId);
_result = "开屏广告${result ? '成功' : '失败'}";
} on PlatformException catch (e) {
_result = "开屏广告失败 code:${e.code} msg:${e.message} details:${e.details}";
}
setState(() {});
}
```