From cc8598064c25f9adffbfbbc2ea76bb138aeb8958 Mon Sep 17 00:00:00 2001 From: fengshuo <963787902@qq.com> Date: Mon, 31 Oct 2022 12:15:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=97=AA=E5=B1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xplan-ios.xcodeproj/project.pbxproj | 26 +++ .../Appdelegate/AppDelegate+ThirdConfig.h | 4 + .../Appdelegate/AppDelegate+ThirdConfig.m | 79 +++++++ xplan-ios/Appdelegate/AppDelegate.m | 2 + xplan-ios/Base/UI/Adbvertise/AdvertiseModel.h | 24 ++ xplan-ios/Base/UI/Adbvertise/AdvertiseModel.m | 12 + xplan-ios/Base/UI/Adbvertise/XPAdImageTool.h | 48 ++++ xplan-ios/Base/UI/Adbvertise/XPAdImageTool.m | 145 ++++++++++++ .../Base/UI/Adbvertise/XPAdvertiseView.h | 25 +++ .../Base/UI/Adbvertise/XPAdvertiseView.m | 206 ++++++++++++++++++ xplan-ios/Base/UI/UIImage/UIImage+Utils.h | 3 + xplan-ios/Base/UI/UIImage/UIImage+Utils.m | 22 ++ .../Main/Mine/View/SubViews/XPMineHeadView.m | 8 +- xplan-ios/Main/Tabbar/ClientDataModel.h | 4 +- xplan-ios/Main/Tabbar/ClientDataModel.m | 9 +- .../Main/Tabbar/View/TabbarViewController.m | 3 + 16 files changed, 616 insertions(+), 4 deletions(-) create mode 100644 xplan-ios/Base/UI/Adbvertise/AdvertiseModel.h create mode 100644 xplan-ios/Base/UI/Adbvertise/AdvertiseModel.m create mode 100644 xplan-ios/Base/UI/Adbvertise/XPAdImageTool.h create mode 100644 xplan-ios/Base/UI/Adbvertise/XPAdImageTool.m create mode 100644 xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.h create mode 100644 xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.m diff --git a/xplan-ios.xcodeproj/project.pbxproj b/xplan-ios.xcodeproj/project.pbxproj index 75490b86..ac0e32cc 100644 --- a/xplan-ios.xcodeproj/project.pbxproj +++ b/xplan-ios.xcodeproj/project.pbxproj @@ -615,6 +615,9 @@ E81D58822720082A003063FE /* MicroWaveView.m in Sources */ = {isa = PBXBuildFile; fileRef = E81D58812720082A003063FE /* MicroWaveView.m */; }; E81DCCCD282B63B40039E5C5 /* XPMonentsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E81DCCCC282B63B40039E5C5 /* XPMonentsViewController.m */; }; E81DCCD0282B63FD0039E5C5 /* XPMonentsRecommendViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E81DCCCF282B63FD0039E5C5 /* XPMonentsRecommendViewController.m */; }; + E81E09C9290F71BF00A1F410 /* XPAdvertiseView.m in Sources */ = {isa = PBXBuildFile; fileRef = E81E09C8290F71BF00A1F410 /* XPAdvertiseView.m */; }; + E81E09CC290F732600A1F410 /* XPAdImageTool.m in Sources */ = {isa = PBXBuildFile; fileRef = E81E09CB290F732600A1F410 /* XPAdImageTool.m */; }; + E81E09CF290F750800A1F410 /* AdvertiseModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E81E09CE290F750800A1F410 /* AdvertiseModel.m */; }; E81EF6DC279AB5CC00FF3EDE /* XPLittleGameRoomOpenView.m in Sources */ = {isa = PBXBuildFile; fileRef = E81EF6DB279AB5CC00FF3EDE /* XPLittleGameRoomOpenView.m */; }; E81EF6E0279AB7D900FF3EDE /* XPCreateLittleGameCollectionViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E81EF6DF279AB7D900FF3EDE /* XPCreateLittleGameCollectionViewCell.m */; }; E81EF6E4279AB92E00FF3EDE /* LittleGameInfoModel.m in Sources */ = {isa = PBXBuildFile; fileRef = E81EF6E3279AB92E00FF3EDE /* LittleGameInfoModel.m */; }; @@ -2286,6 +2289,12 @@ E81DCCCC282B63B40039E5C5 /* XPMonentsViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XPMonentsViewController.m; sourceTree = ""; }; E81DCCCE282B63FD0039E5C5 /* XPMonentsRecommendViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPMonentsRecommendViewController.h; sourceTree = ""; }; E81DCCCF282B63FD0039E5C5 /* XPMonentsRecommendViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XPMonentsRecommendViewController.m; sourceTree = ""; }; + E81E09C7290F71BF00A1F410 /* XPAdvertiseView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPAdvertiseView.h; sourceTree = ""; }; + E81E09C8290F71BF00A1F410 /* XPAdvertiseView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XPAdvertiseView.m; sourceTree = ""; }; + E81E09CA290F732500A1F410 /* XPAdImageTool.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPAdImageTool.h; sourceTree = ""; }; + E81E09CB290F732600A1F410 /* XPAdImageTool.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XPAdImageTool.m; sourceTree = ""; }; + E81E09CD290F750800A1F410 /* AdvertiseModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AdvertiseModel.h; sourceTree = ""; }; + E81E09CE290F750800A1F410 /* AdvertiseModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AdvertiseModel.m; sourceTree = ""; }; E81EF6DA279AB5CC00FF3EDE /* XPLittleGameRoomOpenView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPLittleGameRoomOpenView.h; sourceTree = ""; }; E81EF6DB279AB5CC00FF3EDE /* XPLittleGameRoomOpenView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = XPLittleGameRoomOpenView.m; sourceTree = ""; }; E81EF6DE279AB7D900FF3EDE /* XPCreateLittleGameCollectionViewCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = XPCreateLittleGameCollectionViewCell.h; sourceTree = ""; }; @@ -3866,6 +3875,7 @@ 189DD56C26DF5B5400AB55B1 /* UI */ = { isa = PBXGroup; children = ( + E81E09C6290F719C00A1F410 /* Adbvertise */, E80EC74A28ACD84000D133C5 /* InputView */, E8998D932859DD3F00C68558 /* UIViewCorner */, E8998D772859782C00C68558 /* SVGA */, @@ -5973,6 +5983,19 @@ path = Model; sourceTree = ""; }; + E81E09C6290F719C00A1F410 /* Adbvertise */ = { + isa = PBXGroup; + children = ( + E81E09C7290F71BF00A1F410 /* XPAdvertiseView.h */, + E81E09C8290F71BF00A1F410 /* XPAdvertiseView.m */, + E81E09CA290F732500A1F410 /* XPAdImageTool.h */, + E81E09CB290F732600A1F410 /* XPAdImageTool.m */, + E81E09CD290F750800A1F410 /* AdvertiseModel.h */, + E81E09CE290F750800A1F410 /* AdvertiseModel.m */, + ); + path = Adbvertise; + sourceTree = ""; + }; E81EF6D9279AB59100FF3EDE /* LittleGame */ = { isa = PBXGroup; children = ( @@ -9298,6 +9321,7 @@ E8950183282CAC6A007E459A /* XPMonentsPhotoView.m in Sources */, 9BCF58562798FE86008401A4 /* XPSkillCardPropModel.m in Sources */, E8AC721E26F472BF007D6E91 /* XPMineSettingProtocol.h in Sources */, + E81E09C9290F71BF00A1F410 /* XPAdvertiseView.m in Sources */, 9BC5C91F277C902B007C8719 /* XPReleaseRadioView.m in Sources */, E84843AF27F59E7E0050D365 /* XPRoomPKResultView.m in Sources */, E83DB47A27462C4500D8CBD1 /* XPGiftBigPrizeModel.m in Sources */, @@ -9932,6 +9956,7 @@ E8A3540428FEB7100014A784 /* XPWishGiftHistoryViewController.m in Sources */, E801275527E3326000BAC3F2 /* XPRoomPKUserView.m in Sources */, E8D4824D278D2CE4003C1D08 /* XPAcrossRoomPKInviteResultView.m in Sources */, + E81E09CC290F732600A1F410 /* XPAdImageTool.m in Sources */, 9BD8D4E628911F7700AE03FF /* XPMineCollectRoomListPresenter.m in Sources */, E866B6E52759F96F009B002A /* XPMiniRoomView.m in Sources */, E818E34B286ECABF005EDF68 /* XPMonentsPublishPresenter.m in Sources */, @@ -10005,6 +10030,7 @@ E8A03DF62763367F0098D9EA /* XPCandyTreeEmptyableViewCell.m in Sources */, E8AEAEED27141AE20017FCE0 /* XPRoomBackContainerView.m in Sources */, E88B5CC126FB407B00DA9178 /* XPMineUserInfoViewController.m in Sources */, + E81E09CF290F750800A1F410 /* AdvertiseModel.m in Sources */, E852D74428633A08001465ED /* MonentsCommentModel.m in Sources */, E8C1CD6D27D8938C00376F83 /* XPRoomFaceTitleCollectionViewCell.m in Sources */, E8C1CD7627D8AE3D00376F83 /* XPRoomFacePresenter.m in Sources */, diff --git a/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.h b/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.h index 5a7fbf4e..cc29d661 100644 --- a/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.h +++ b/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.h @@ -12,6 +12,10 @@ NS_ASSUME_NONNULL_BEGIN @interface AppDelegate (ThirdConfig) /// 初始化一些第三方配置 - (void)initThirdConfig; +/** + 设置广告页 + */ +- (void)setupLaunchADView; @end NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.m b/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.m index 8ea2a710..f56e8910 100644 --- a/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.m +++ b/xplan-ios/Appdelegate/AppDelegate+ThirdConfig.m @@ -16,8 +16,16 @@ #import "XPConstant.h" #import "CustomAttachmentDecoder.h" #import "QEmotionHelper.h" +#import "XPAdvertiseView.h" +#import "XPAdImageTool.h" +#import "XPMacro.h" +#import "AdvertiseModel.h" +#import "XPWebViewController.h" +#import "XPRoomViewController.h" +#import "XCCurrentVCStackManager.h" UIKIT_EXTERN NSString * kYinyouPrivateKey; +UIKIT_EXTERN NSString * adImageName; @implementation AppDelegate (ThirdConfig) @@ -116,4 +124,75 @@ UIKIT_EXTERN NSString * kYinyouPrivateKey; faceManager.emotionArray = array; } +#pragma mark - 广告 + +/** + 设置广告页 + */ +- (void)setupLaunchADView { + NSUserDefaults * kUserDefaults = NSUserDefaults.standardUserDefaults; + // 判断沙盒中是否存在广告图片,如果存在,直接显示 + NSString *filePath = [XPAdImageTool.shareImageTool getFilePathWithImageName:[kUserDefaults valueForKey:adImageName]]; + BOOL isExist = [XPAdImageTool.shareImageTool isFileExistWithFilePath:filePath]; + NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults]; + + if ([userDefault integerForKey:@"adShow"]) { + [userDefault setInteger:[userDefault integerForKey:@"adShow"]+1 forKey:@"adShow"]; + } else { + [userDefault setInteger:1 forKey:@"adShow"]; + } + + if (isExist) {// 图片存在 + + if ([userDefault integerForKey:@"adShow"] > 4) { + @kWeakify(self); + NSString *imageName = [kUserDefaults valueForKey:adImageName]; + AdvertiseModel *info = [XPAdImageTool.shareImageTool getAdInfoFromCacheInMainWith:imageName]; + XPAdvertiseView *advertiseView = [[XPAdvertiseView alloc] initWithFrame:self.window.bounds]; + advertiseView.filePath = filePath; + advertiseView.dismissHandler = ^(BOOL shouldJump) { + @kStrongify(self) + if (!shouldJump || info == nil) { + return; + } + [self performSelectorOnMainThread:@selector(advertiseJumpHandleWithInfo:) withObject:info waitUntilDone:NO]; + }; + [advertiseView show]; + } + } +} + + +/// 闪屏广告跳转处理 +- (void)advertiseJumpHandleWithInfo:(AdvertiseModel *)info { + if (UIApplication.sharedApplication.keyWindow != self.window) { + //当前窗口不是主控制器所在窗口时,拦截跳转(目前可能情况时,闪屏后出现新人引导 + return; + } + + switch (info.type) { + case SplashInfoSkipTypeRoom: { + if (![[XPAdImageTool shareImageTool] isImLogin]) { + return; // 必须登录后才可以跳转 + } + // 跳转房间 + if (info.link.length > 0) { + [XPRoomViewController openRoom:info.link viewController:[XCCurrentVCStackManager shareManager].getCurrentVC]; + } + } + break; + case SplashInfoSkipTypeWeb: { + // 跳转 H5 + if (info.link.length > 0) { + XPWebViewController *webView = [[XPWebViewController alloc]init]; + webView.url = info.link; + [[[XCCurrentVCStackManager shareManager]currentNavigationController] pushViewController:webView animated:YES]; + } + } + break; + default: + break; + } +} + @end diff --git a/xplan-ios/Appdelegate/AppDelegate.m b/xplan-ios/Appdelegate/AppDelegate.m index 9e61b44a..91561d07 100644 --- a/xplan-ios/Appdelegate/AppDelegate.m +++ b/xplan-ios/Appdelegate/AppDelegate.m @@ -26,6 +26,8 @@ self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.window.rootViewController = bnc; [self.window makeKeyAndVisible]; + ///设置广告页 + [self setupLaunchADView]; ///初始化一些 sdk配置 [self initThirdConfig]; return YES; diff --git a/xplan-ios/Base/UI/Adbvertise/AdvertiseModel.h b/xplan-ios/Base/UI/Adbvertise/AdvertiseModel.h new file mode 100644 index 00000000..e0ddc72c --- /dev/null +++ b/xplan-ios/Base/UI/Adbvertise/AdvertiseModel.h @@ -0,0 +1,24 @@ +// +// AdvertiseModel.h +// xplan-ios +// +// Created by 冯硕 on 2022/10/31. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +typedef NS_ENUM(NSInteger, SplashInfoSkipType) { + SplashInfoSkipTypePage = 1, + SplashInfoSkipTypeRoom = 2, + SplashInfoSkipTypeWeb = 3, +}; + +@interface AdvertiseModel : NSObject +@property (nonatomic, strong) NSString *link; +@property (nonatomic, assign) SplashInfoSkipType type;// 1跳app页面,2跳聊天室,3跳h5页面 +@property (nonatomic, copy) NSString *pict; +@end + +NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Base/UI/Adbvertise/AdvertiseModel.m b/xplan-ios/Base/UI/Adbvertise/AdvertiseModel.m new file mode 100644 index 00000000..5365312c --- /dev/null +++ b/xplan-ios/Base/UI/Adbvertise/AdvertiseModel.m @@ -0,0 +1,12 @@ +// +// AdvertiseModel.m +// xplan-ios +// +// Created by 冯硕 on 2022/10/31. +// + +#import "AdvertiseModel.h" + +@implementation AdvertiseModel + +@end diff --git a/xplan-ios/Base/UI/Adbvertise/XPAdImageTool.h b/xplan-ios/Base/UI/Adbvertise/XPAdImageTool.h new file mode 100644 index 00000000..4599e4ff --- /dev/null +++ b/xplan-ios/Base/UI/Adbvertise/XPAdImageTool.h @@ -0,0 +1,48 @@ +// +// XPAdImageTool.h +// xplan-ios +// +// Created by 冯硕 on 2022/10/31. +// + +#import +#import "AdvertiseModel.h" +NS_ASSUME_NONNULL_BEGIN + +@interface XPAdImageTool : NSObject ++ (instancetype)shareImageTool; + +///是否登录成功im +@property (nonatomic,assign)BOOL isImLogin; + +//去除广告信息 +- (AdvertiseModel *)getAdInfoFromCacheInMainWith:(NSString *)link; +///保存信息 +- (void)saveAdInfo:(AdvertiseModel *)adInfo; +/** + * 判断文件是否存在 + */ +- (BOOL)isFileExistWithFilePath:(NSString *)filePath; + +/** + * 初始化广告页面 + */ +- (void)getAdvertisingImage; + +/** + * 下载新图片 + */ +- (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName; + +/** + * 删除旧图片 + */ +- (void)deleteOldImage; + +/** + * 根据图片名拼接文件路径 + */ +- (NSString *)getFilePathWithImageName:(NSString *)imageName; +@end + +NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Base/UI/Adbvertise/XPAdImageTool.m b/xplan-ios/Base/UI/Adbvertise/XPAdImageTool.m new file mode 100644 index 00000000..c3320e16 --- /dev/null +++ b/xplan-ios/Base/UI/Adbvertise/XPAdImageTool.m @@ -0,0 +1,145 @@ +// +// XPAdImageTool.m +// xplan-ios +// +// Created by 冯硕 on 2022/10/31. +// + +#import "XPAdImageTool.h" +#import + +#define CACHENAME @"XPUserCache" + +UIKIT_EXTERN NSString * const adImageName; +@interface XPAdImageTool () +@property (nonatomic, strong) YYCache *yyCache; +///广告信息 +@property (nonatomic,strong) AdvertiseModel *infoModel; +@end + +static XPAdImageTool* tool; +@implementation XPAdImageTool + ++ (instancetype)shareImageTool { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + tool = [[XPAdImageTool alloc] init]; + }); + return tool; +} + +- (instancetype)init +{ + self = [super init]; + if (self) { + self.yyCache = [YYCache cacheWithName:CACHENAME]; + } + return self; +} + +//去除广告信息 +- (AdvertiseModel *)getAdInfoFromCacheInMainWith:(NSString *)link { + if (link.length > 0) { + if ([self.yyCache containsObjectForKey:link]) { + return [self.yyCache objectForKey:link]; + }else { + return nil; + } + }else { + return nil; + } + return nil; +} + + +///保存信息 +- (void)saveAdInfo:(AdvertiseModel *)adInfo { + self.infoModel = adInfo; + NSArray *stringArr = [adInfo.pict componentsSeparatedByString:@"/"]; + NSString *key = stringArr.lastObject; + [self.yyCache setObject:adInfo forKey:key withBlock:^{ + + }]; + + [self getAdvertisingImage]; +} + +/** + * 判断文件是否存在 + */ +- (BOOL)isFileExistWithFilePath:(NSString *)filePath { + NSFileManager *fileManager = [NSFileManager defaultManager]; + BOOL isDirectory = FALSE; + return [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory]; +} + +/** + * 初始化广告页面 + */ +- (void)getAdvertisingImage { + + if (self.infoModel.pict.length > 0) { + NSString *imageUrl = self.infoModel.pict; + // 获取图片名 + NSArray *stringArr = [imageUrl componentsSeparatedByString:@"/"]; + NSString *imageName = stringArr.lastObject; + + // 拼接沙盒路径 + NSString *filePath = [self getFilePathWithImageName:imageName]; + BOOL isExist = [self isFileExistWithFilePath:filePath]; + if (!isExist){// 如果该图片不存在,则删除老图片,下载新图片 + [self downloadAdImageWithUrl:imageUrl imageName:imageName]; + } + }else { + [self deleteOldImage]; + } +} + +/** + * 下载新图片 + */ +- (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + + NSString *encode = [imageUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; + NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:encode]]; + UIImage *image = [UIImage imageWithData:data]; + + NSString *filePath = [self getFilePathWithImageName:imageName]; // 保存文件的名称 + if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {// 保存成功 + [self deleteOldImage]; + [[NSUserDefaults standardUserDefaults] setValue:imageName forKey:adImageName]; + [[NSUserDefaults standardUserDefaults] synchronize]; + }else{ + [self deleteOldImage]; + } + + }); +} + +/** + * 删除旧图片 + */ +- (void)deleteOldImage { + NSString *imageName = [[NSUserDefaults standardUserDefaults] valueForKey:adImageName]; + if (imageName) { + NSString *filePath = [self getFilePathWithImageName:imageName]; + NSFileManager *fileManager = [NSFileManager defaultManager]; + [fileManager removeItemAtPath:filePath error:nil]; + } +} + +/** + * 根据图片名拼接文件路径 + */ +- (NSString *)getFilePathWithImageName:(NSString *)imageName { + if (imageName) { + NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES); + NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:imageName]; + + return filePath; + } + + return nil; +} +@end diff --git a/xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.h b/xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.h new file mode 100644 index 00000000..584c970d --- /dev/null +++ b/xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.h @@ -0,0 +1,25 @@ +// +// XPAdvertiseView.h +// xplan-ios +// +// Created by 冯硕 on 2022/10/31. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface XPAdvertiseView : UIView + +/** 显示广告页面方法*/ +- (void)show; + +/** 图片路径*/ +@property (nonatomic, copy) NSString *filePath; + +@property (nonatomic, strong) UIImage *adImage; + +@property (nonatomic, copy) void(^dismissHandler)(BOOL shouldJump); //闪屏消失回调 +@end + +NS_ASSUME_NONNULL_END diff --git a/xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.m b/xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.m new file mode 100644 index 00000000..d68f1721 --- /dev/null +++ b/xplan-ios/Base/UI/Adbvertise/XPAdvertiseView.m @@ -0,0 +1,206 @@ +// +// XPAdvertiseView.m +// xplan-ios +// +// Created by 冯硕 on 2022/10/31. +// + +#import "XPAdvertiseView.h" +#import "AppDelegate.h" +//tool +#import +#import +#import "XPMacro.h" +///Tool +#import "UIImage+Utils.h" +NSString *const adImageName = @"adImageName"; +NSString *const adUrl = @"adUrl"; +// 广告显示的时间 +static int const showtime = 3; + +@interface XPAdvertiseView() + +@property (nonatomic, strong) UIImageView *adView;//广告图片 + +@property (nonatomic, strong) UIButton *countdownButton;//倒计时、跳过按钮 + +@property (nonatomic, strong) NSTimer *countTimer; + +@property (nonatomic, assign) int count; + +@property (nonatomic, strong) UIWindow *window; + +@end + + +@implementation XPAdvertiseView + +#pragma mark - Initialize Methods + +- (instancetype)initWithFrame:(CGRect)frame { + if (self = [super initWithFrame:frame]) { + + _adView = [[UIImageView alloc] initWithFrame:frame]; + _adView.userInteractionEnabled = YES; + _adView.contentMode = UIViewContentModeScaleAspectFill; + _adView.clipsToBounds = YES; + [self addSubview:_adView]; + + UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapAdViewAction)]; + [_adView addGestureRecognizer:tap]; + + // 因为倒数跳秒问题,导致无法及时响应事件,经测试提案说无法接受此结果。所以就做成和安卓一样,去掉倒计时和跳过 + if ([self needCountDownBtn]) { + [self addSubview:self.countdownButton]; + } + } + return self; +} + +#pragma mark - Public Methods + +- (void)show { + // 倒计时方法1:GCD + [self gcdCoundownHander]; + + UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; + [self.window.rootViewController.view addSubview:self]; + [self.window makeKeyAndVisible]; + [keyWindow makeKeyWindow]; +} + + +#pragma mark - Event Response + +/// 点击闪屏操作 +- (void)onTapAdViewAction { + [self dismissWithJumpHandle:YES]; +} + +/// 点击跳过按钮 +- (void)onClickSkipButton:(UIButton *)sender { + [self dismissWithJumpHandle:NO]; +} + + +#pragma mark - Privite Method + +// GCD倒计时方法 +- (void)gcdCoundownHander { + __block int timeout = showtime; + dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); + dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); + dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0); //每秒执行 + + dispatch_source_set_event_handler(_timer, ^{ + + if (timeout <= 0) { //倒计时结束,关闭 + dispatch_source_cancel(_timer); + dispatch_async(dispatch_get_main_queue(), ^{ + [self dismissWithJumpHandle:NO]; + }); + } else { + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.countdownButton) { + [self.countdownButton setTitle:@"跳过" forState:UIControlStateNormal]; + } + }); + timeout--; + } + }); + dispatch_resume(_timer); +} + +// 移除广告页面 +- (void)dismissWithJumpHandle:(BOOL)shouldJump { + + if (self.countTimer) { + [self.countTimer invalidate]; + self.countTimer = nil; + } + + @kWeakify(self) + [UIView animateWithDuration:0.5f animations:^{ + @kStrongify(self) + self.window.hidden = YES; + + } completion:^(BOOL finished) { + @kStrongify(self) + + [self removeFromSuperview]; + self.window = nil; + + !self.dismissHandler ?: self.dismissHandler(shouldJump); + }]; +} + + +- (NSString *)deviceName { + // Gets a string with the device model + size_t size; + int nR = sysctlbyname("hw.machine", NULL, &size, NULL, 0); + char *machine = (char *)malloc(size); + nR = sysctlbyname("hw.machine", machine, &size, NULL, 0); + NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; + free(machine); + return platform; +} + +- (BOOL)needCountDownBtn { + + NSString *platform = [self deviceName]; + BOOL needBtn = YES; + if ([platform isEqualToString:@"iPhone6,1"] || + [platform isEqualToString:@"iPhone6,2"] || + [platform isEqualToString:@"iPhone7,1"] || + [platform isEqualToString:@"iPhone7,2"] || + [platform isEqualToString:@"iPhone8,1"] || + [platform isEqualToString:@"iPhone8,2"] || + [platform isEqualToString:@"iPhone8,4"]) { + needBtn = NO; + } + return needBtn; +} + +#pragma mark - Setter +- (void)setFilePath:(NSString *)filePath { + _filePath = filePath; + UIImage *image = [UIImage imageWithContentsOfFile:filePath]; + _adView.image = [image cutImage:[UIScreen mainScreen].bounds.size]; +} + +- (void)setAdImage:(UIImage *)adImage { + _adImage = adImage; + _adView.image = [adImage cutImage:[UIScreen mainScreen].bounds.size]; +} + + +#pragma mark - Getter + +- (UIWindow *)window { + if (_window == nil) { + _window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; + _window.windowLevel = UIWindowLevelAlert; + _window.userInteractionEnabled = YES; + _window.rootViewController = [[UIViewController alloc] init]; + } + return _window; +} + +- (UIButton *)countdownButton { + if (_countdownButton == nil) { + CGFloat btnW = 60; + CGFloat btnH = 30; + _countdownButton = [UIButton buttonWithType:UIButtonTypeCustom]; + _countdownButton.frame = CGRectMake(KScreenWidth - btnW - 24, btnH + 10, btnW, btnH); + [_countdownButton addTarget:self action:@selector(onClickSkipButton:) forControlEvents:UIControlEventTouchUpInside]; + [_countdownButton setTitle:@"跳过" forState:UIControlStateNormal]; + _countdownButton.titleLabel.font = [UIFont systemFontOfSize:15]; + [_countdownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; + _countdownButton.backgroundColor = [UIColor colorWithRed:38 /255.0 green:38 /255.0 blue:38 /255.0 alpha:0.6]; + _countdownButton.layer.cornerRadius = 4; + } + return _countdownButton; +} + +@end diff --git a/xplan-ios/Base/UI/UIImage/UIImage+Utils.h b/xplan-ios/Base/UI/UIImage/UIImage+Utils.h index 19b272d9..6dd232a0 100644 --- a/xplan-ios/Base/UI/UIImage/UIImage+Utils.h +++ b/xplan-ios/Base/UI/UIImage/UIImage+Utils.h @@ -45,4 +45,7 @@ typedef NS_ENUM(NSUInteger, GradientType) { + (CGSize)sizeWithImageOriginSize:(CGSize)originSize minSize:(CGSize)imageMinSize maxSize:(CGSize)imageMaxSize; + +///裁剪图片 +- (UIImage *)cutImage:(CGSize)newSize; @end diff --git a/xplan-ios/Base/UI/UIImage/UIImage+Utils.m b/xplan-ios/Base/UI/UIImage/UIImage+Utils.m index bf28f974..f6098a00 100644 --- a/xplan-ios/Base/UI/UIImage/UIImage+Utils.m +++ b/xplan-ios/Base/UI/UIImage/UIImage+Utils.m @@ -356,4 +356,26 @@ return size; } +- (UIImage *)cutImage:(CGSize)newSize{ + + CGFloat scale = newSize.height / self.size.height; + UIImage *scaleImage = [self originImage:self scaleToSize:CGSizeMake(self.size.width*scale, self.size.height*scale)]; + //裁剪暂时有问题 + return scaleImage; +} + +- (UIImage*) originImage:(UIImage *)image scaleToSize:(CGSize)size { + + UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale); + + [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; + + UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); + + UIGraphicsEndImageContext(); + + return scaledImage; +} + + @end diff --git a/xplan-ios/Main/Mine/View/SubViews/XPMineHeadView.m b/xplan-ios/Main/Mine/View/SubViews/XPMineHeadView.m index f453fd9a..cff4ab4a 100644 --- a/xplan-ios/Main/Mine/View/SubViews/XPMineHeadView.m +++ b/xplan-ios/Main/Mine/View/SubViews/XPMineHeadView.m @@ -291,8 +291,12 @@ } else { NSURL *imgUrl = [NSURL URLWithString:imageUrl]; UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]]; - CGFloat scale = myImage.size.width / myImage.size.height; - imageView.bounds = CGRectMake(0, 0, 20 * scale, 20); + if (myImage) { + CGFloat scale = myImage.size.width / myImage.size.height; + imageView.bounds = CGRectMake(0, 0, 20 * scale, 20); + } else { + imageView.bounds = CGRectMake(0, 0, 20, 20); + } } imageView.layer.masksToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFit; diff --git a/xplan-ios/Main/Tabbar/ClientDataModel.h b/xplan-ios/Main/Tabbar/ClientDataModel.h index 35021391..6c2f1384 100644 --- a/xplan-ios/Main/Tabbar/ClientDataModel.h +++ b/xplan-ios/Main/Tabbar/ClientDataModel.h @@ -7,7 +7,7 @@ #import #import "ClientRedPacketModel.h" - +#import "AdvertiseModel.h" NS_ASSUME_NONNULL_BEGIN @interface ClientDataModel : NSObject @@ -27,6 +27,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, assign) NSInteger linearlyPoolOpenLevel; ///红包配置 @property (nonatomic, strong) ClientRedPacketModel *redEnvelopeConfig; +///启动图 +@property (nonatomic,strong) AdvertiseModel *splashVo; @end diff --git a/xplan-ios/Main/Tabbar/ClientDataModel.m b/xplan-ios/Main/Tabbar/ClientDataModel.m index 478dcc8b..2dd3782f 100644 --- a/xplan-ios/Main/Tabbar/ClientDataModel.m +++ b/xplan-ios/Main/Tabbar/ClientDataModel.m @@ -6,7 +6,14 @@ // #import "ClientDataModel.h" - +#import "XPAdImageTool.h" @implementation ClientDataModel +- (void)setSplashVo:(AdvertiseModel *)splashVo { + _splashVo = splashVo; + if (_splashVo) { + [XPAdImageTool.shareImageTool saveAdInfo:splashVo]; + } +} + @end diff --git a/xplan-ios/Main/Tabbar/View/TabbarViewController.m b/xplan-ios/Main/Tabbar/View/TabbarViewController.m index a5f94a33..6a7de2a7 100644 --- a/xplan-ios/Main/Tabbar/View/TabbarViewController.m +++ b/xplan-ios/Main/Tabbar/View/TabbarViewController.m @@ -28,6 +28,7 @@ #import "XPConstant.h" #import "StatisticsServiceHelper.h" #import "Api+Monents.h" +#import "XPAdImageTool.h" ///Model #import "AccountModel.h" #import "RoomInfoModel.h" @@ -191,6 +192,7 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey; ///邀请码进房 [self.presenter checkInviteUserInfo:self.inviteCode]; } + XPAdImageTool.shareImageTool.isImLogin = YES; } /// 获取用户信息后检查:1、是否绑定手机号;2、是否需要完善用户信息。 @@ -264,6 +266,7 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey; BaseNavigationController * nav = [[BaseNavigationController alloc] initWithRootViewController:lvc]; nav.modalPresentationStyle = UIModalPresentationFullScreen; [self.navigationController presentViewController:nav animated:YES completion:nil]; + XPAdImageTool.shareImageTool.isImLogin = NO; } - (void)completeUserInfo {