From 3e8437ae559487362fae3525beb79c534c213a51 Mon Sep 17 00:00:00 2001
From: 单军华
Date: Thu, 12 Jul 2018 13:44:34 +0800
Subject: [PATCH] bug修复和功能优化

---
 screendisplay/screendisplay/Classes/Helpers/广告页/AdvertiseHelper.m |  146 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 146 insertions(+), 0 deletions(-)

diff --git "a/screendisplay/screendisplay/Classes/Helpers/\345\271\277\345\221\212\351\241\265/AdvertiseHelper.m" "b/screendisplay/screendisplay/Classes/Helpers/\345\271\277\345\221\212\351\241\265/AdvertiseHelper.m"
new file mode 100755
index 0000000..6716b92
--- /dev/null
+++ "b/screendisplay/screendisplay/Classes/Helpers/\345\271\277\345\221\212\351\241\265/AdvertiseHelper.m"
@@ -0,0 +1,146 @@
+//
+//  AdvertiseHelper.m
+//  MobileProject ���������������������
+//
+//  Created by wujunyang on 16/6/14.
+//  Copyright �� 2016��� wujunyang. All rights reserved.
+//
+
+#import "AdvertiseHelper.h"
+
+static NSString *const adImageName = @"adImageName";
+@implementation AdvertiseHelper
+
+static AdvertiseHelper* _instance = nil;
++ (instancetype)sharedInstance
+{
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        _instance = [[self alloc] init];
+    });
+    return _instance;
+}
+
+- (instancetype)init
+{
+    self = [super init];
+    if (self) {
+        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(adClick:) name:NotificationContants_Advertise_Key object:nil];
+    }
+    return self;
+}
+
+
++ (void)showAdvertiserView:(NSArray<NSString *> *)imageArray
+{
+    // 1.���������������������������������������������������������������������
+    NSString *filePath = [[AdvertiseHelper sharedInstance] getFilePathWithImageName:[NSUserDefaults.standardUserDefaults valueForKey:adImageName]];
+    
+    BOOL isExist = [[AdvertiseHelper sharedInstance] isFileExistWithFilePath:filePath];
+    if (isExist) {// ������������
+        AdvertiseView *advertiseView = [[AdvertiseView alloc] initWithFrame:UIScreen.mainScreen.bounds];
+        advertiseView.filePath = filePath;
+        [advertiseView show];
+    }
+    
+    // 2.������������������������������������������������������������������������������������������������������
+    [[AdvertiseHelper sharedInstance] getAdvertisingImage:imageArray];
+}
+
+
+/**
+ *  ���������������������
+ */
+- (void)getAdvertisingImage:(NSArray<NSString *> *)imageArray
+{
+    //���������������
+    NSString *imageUrl = imageArray[arc4random() % imageArray.count];
+    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];
+    }
+}
+
+/**
+ *  ������������������������
+ */
+- (BOOL)isFileExistWithFilePath:(NSString *)filePath
+{
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    BOOL isDirectory = FALSE;
+    return [fileManager fileExistsAtPath:filePath isDirectory:&isDirectory];
+}
+
+
+/**
+ *  ���������������
+ */
+- (void)downloadAdImageWithUrl:(NSString *)imageUrl imageName:(NSString *)imageName
+{
+    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+        
+        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
+        NSString *filePath = [self getFilePathWithImageName:imageName]; // ���������������������
+        
+        if ([data writeToFile:filePath atomically:YES]) {// ������������
+            NSLog(@"������������");
+            [self deleteOldImage];
+            [NSUserDefaults.standardUserDefaults setValue:imageName forKey:adImageName];
+            [NSUserDefaults.standardUserDefaults synchronize];
+            // ������������������������������������������������������
+        }else{
+            NSLog(@"������������");
+        }
+        
+    });
+}
+
+/**
+ *  ���������������
+ */
+- (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;
+}
+
+//NotificationContants_Advertise_Key
+- (void)adClick:(NSNotification *)noti
+{
+    NSString *url = @"https://github.com/NJHu";
+    if (!TWIsEmpty(url)) {
+        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:url]]) {
+            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{@"username" : @"njhu"} completionHandler:^(BOOL success) {
+                NSLog(@"%zd", success);
+            }];
+        }
+    }
+}
+
+- (void)dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+@end

--
Gitblit v1.8.0