郑州营销型网站推广工具,网站卖了对方做违法,深圳网站做的好的公司名称,wordpress分页模板iOS开发-实现获取下载主题配置动态切换主题
iOS开发-实现获取下载主题配置更切换主题#xff0c;主要是通过请求服务端配置的主题配置、下载主题、解压保存到本地。通知界面获取对应的图片及颜色等。
比如新年主题风格#xff0c;常见的背景显示红色氛围图片、tabbar显示新…iOS开发-实现获取下载主题配置动态切换主题
iOS开发-实现获取下载主题配置更切换主题主要是通过请求服务端配置的主题配置、下载主题、解压保存到本地。通知界面获取对应的图片及颜色等。
比如新年主题风格常见的背景显示红色氛围图片、tabbar显示新年风格的按钮样式、导航条显示红色样式等。
一、主题Json对应的model
这里使用JsonModel将主题转成model
model代码如下
SDAppThemeConfigViewModel.h
#import Foundation/Foundation.h
#import UIKit/UIKit.h/**Navigation主题样式*/
interface SDAppThemeConfigNavViewModel : NSObjectproperty (nonatomic, strong) NSString *backgroundColor;
property (nonatomic, strong) NSString *backgroundImage;property (nonatomic, strong) UIImage *t_backgroundImage;property (nonatomic, strong) NSString *btnImageColor;
property (nonatomic, strong) NSString *btnTitleColor;
property (nonatomic, strong) NSString *navTitleColor;property (nonatomic, strong) NSString *showLine;
property (nonatomic, strong) NSString *lineColor;end/**单个tab按钮样式*/
interface SDAppThemeConfigTabItemViewModel : NSObjectproperty (nonatomic, strong) NSString *title;
property (nonatomic, strong) NSString *titleColor;
property (nonatomic, strong) NSString *selectedTitleColor;
property (nonatomic, strong) NSString *icon;
property (nonatomic, strong) NSString *selectedIcon;property (nonatomic, strong) UIImage *t_icon;
property (nonatomic, strong) UIImage *t_selectedIcon;end/**tabbar样式*/
interface SDAppThemeConfigTabViewModel : NSObjectproperty (nonatomic, strong) NSString *backgroundColor;
property (nonatomic, strong) NSString *backgroundImage;
property (nonatomic, strong) NSString *showLine;
property (nonatomic, strong) NSString *lineColor;
property (nonatomic, strong) NSString *badgeBgColor;property (nonatomic, strong) UIImage *t_backgroundImage;property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *lianlian;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *guangguang;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *message;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *shop;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *mine;end/**将本地的主题config.json转成viewmodel*/
interface SDAppThemeConfigViewModel : NSObjectproperty (nonatomic, strong) NSString *globalColor;
property (nonatomic, strong) NSString *globalImage;
property (nonatomic, strong) SDAppThemeConfigNavViewModel *navigation;
property (nonatomic, strong) SDAppThemeConfigTabViewModel *tabbar;property (nonatomic, strong) UIImage *t_globalImage; (SDAppThemeConfigViewModel *)themeViewModel:(NSString *)themeJson; (SDAppThemeConfigViewModel *)defautThemeViewModel;endSDAppThemeConfigViewModel.m
#import SDAppThemeConfigViewModel.h
#import NSObjectYYModel.h/**Navigation主题样式*/
implementation SDAppThemeConfigNavViewModelend/**单个tab按钮样式*/
implementation SDAppThemeConfigTabItemViewModelend/**tabbar样式*/
implementation SDAppThemeConfigTabViewModelend/**将本地的主题config.json转成viewmodel*/
implementation SDAppThemeConfigViewModel (SDAppThemeConfigViewModel *)themeViewModel:(NSString *)themeJson {return [SDAppThemeConfigViewModel modelWithJSON:themeJson];
} (SDAppThemeConfigViewModel *)defautThemeViewModel {SDAppThemeConfigViewModel *viewModel [[SDAppThemeConfigViewModel alloc] init];SDAppThemeConfigNavViewModel *navConfigViewModel [[SDAppThemeConfigNavViewModel alloc] init];navConfigViewModel.backgroundColor 171013;navConfigViewModel.btnImageColor ffffff;navConfigViewModel.btnTitleColor ffffff;navConfigViewModel.navTitleColor ffffff;viewModel.navigation navConfigViewModel;return viewModel;
}end二、实现下载解压主题
2.1 AFNetworking下载
下载使用的是AFNetworking下载功能。AFNetworking是一个轻量级的iOS网络通信类库。
下载代码
#pragma mark - Http download
/**请求下载param aUrl aurlparam aSavePath aSavePathparam aFileName aFileNameparam aTag aTagparam downloadprogress downloadprogressparam success successparam failure failure*/
- (void)downloadFileURL:(NSString *)aUrlsavePath:(NSString *)aSavePathfileName:(NSString *)aFileNametag:(NSInteger)aTagdownloadProgress:(void(^)(CGFloat progress))downloadprogresssuccess:(void(^)(NSURLResponse *response,NSString *filePath))successfailure:(void(^)(HttpError * e))failure {NSFileManager *fileManger [NSFileManager defaultManager];if ([fileManger fileExistsAtPath:[aSavePath stringByAppendingPathComponent:aFileName]]) {//文件存在return;}//2.确定请求的URL地址NSString *requestUrl [self requestUrlWithPath:aUrl clientType:HttpClientTypeWithOut];NSMutableURLRequest *request [self.httpManager.requestSerializer requestWithMethod:GET URLString:requestUrl parameters:nil error:nil];__block NSURLSessionDownloadTask *downloadTask nil;downloadTask [self.httpManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {dispatch_async(dispatch_get_main_queue(), ^{downloadprogress(1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);});} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {return [NSURL fileURLWithPath:aSavePath];} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {if(error nil) {success(response,[filePath path]);} else {//下载失败NSHTTPURLResponse *httpResponse (NSHTTPURLResponse *)response;HttpError *e [self httpRequestFailure:httpResponse error:error];failure(e);}}];[downloadTask resume];
}2.2 判断主题版本是否已经下载
在获取主题版本时候需要判断主题是否存在如果存在则直接获取保存的地址。不存在下载解压当前版本的主题包。
//判断当前主题版本号下是否存在资源文件夹BOOL curThemeExist [self hasAppThemeVersion:themeModel.curVersion];if (!curThemeExist) {//如果不存在重新下载解压__block NSString *saveZipPath [self saveThemeTargetBasePath:themeModel.curVersion];__block NSString *themeUnZipPath [self saveThemeDirBasePath:themeModel.curVersion];__block NSString *curDownloadurl themeModel.curDownloadurl;//下载成功NSString *afileName [[NSURL URLWithString:curDownloadurl] lastPathComponent];__block NSString *afilePath [NSString pathWithComponents:[saveZipPath, afileName]];[[INHttpClientUtil sharedInstance] downloadFileURL:curDownloadurl savePath:afilePath fileName:afilePath tag:[afilePath hash] downloadProgress:^(CGFloat progress) {} success:^(NSURLResponse *response, NSString *filePath) {//下载成功NSString *fileName [[NSURL URLWithString:curDownloadurl] lastPathComponent];NSString *selFilePath [NSString pathWithComponents:[saveZipPath, fileName]];//准备执行解压方法[self onFileSelected:selFilePath unZipPath:themeUnZipPath];} failure:^(HttpError *e) {NSLog(failure request :%,e);}];} else {//如果存在直接显示__block NSString *saveZipPath [self saveThemeTargetBasePath:themeModel.curVersion];__block NSString *themeUnZipPath [self saveThemeDirBasePath:themeModel.curVersion];__block NSString *curDownloadurl themeModel.curDownloadurl;//下载成功NSString *fileName [[NSURL URLWithString:curDownloadurl] lastPathComponent];NSString *filePath [NSString pathWithComponents:[saveZipPath, fileName]];//准备执行解压方法[self unzipCompltion:themeUnZipPath];}2.2 解压zip主题包
将下载的主题包解压zip包进行解压。
// 解压
- (void)releaseZipFilesWithUnzipFileAtPath:(NSString *)zipPath destination:(NSString *)unzipPath {NSError *error;// 如果解压成功if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:error delegate:self]) {// 存储主题的色调[self unzipCompltion:unzipPath];} else {NSLog(%,error);}
}解压完成后得到解压的目录。
2.3 获取到解压的目录地址将配置的json文件转成对应的model
获取到解压的目录地址将配置的json文件转成对应的model
/**调用解压文件param unzipPath 获取地址*/
- (void)unzipCompltion:(NSString *)unzipPath {// 存储主题的色调// 已经存储主题tabbar图片、navigationbar图片、配置文件等等资源NSArray *folders [[NSFileManager defaultManager] contentsOfDirectoryAtPath:unzipPath error:NULL];NSString *selectedFilePath unzipPath;NSString *aPath [folders lastObject];NSString *fullPath [unzipPath stringByAppendingPathComponent:aPath];selectedFilePath fullPath;NSString *configPath [NSString stringWithFormat:%/config.json,selectedFilePath];NSFileHandle *fh [NSFileHandle fileHandleForReadingAtPath:configPath];NSData *data [fh readDataToEndOfFile];NSString *jsonStr [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSMutableDictionary *dict [[NSMutableDictionary alloc] initWithDictionary:[SDJsonUtil dictionaryWithJsonString:jsonStr]];NSLog(theme config.json:%,dict);SDAppThemeConfigViewModel *themeViewModel [SDAppThemeConfigViewModel themeViewModel:jsonStr];themeViewModel.t_globalImage [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.globalImage];themeViewModel.navigation.t_backgroundImage [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.navigation.backgroundImage];themeViewModel.tabbar.t_backgroundImage [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.backgroundImage];themeViewModel.tabbar.lianlian.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.lianlian.icon];themeViewModel.tabbar.lianlian.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.lianlian.selectedIcon];themeViewModel.tabbar.guangguang.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.guangguang.icon];themeViewModel.tabbar.guangguang.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.guangguang.selectedIcon];themeViewModel.tabbar.message.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.message.icon];themeViewModel.tabbar.message.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.message.selectedIcon];themeViewModel.tabbar.mine.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.mine.icon];themeViewModel.tabbar.mine.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.mine.selectedIcon];//配置全局主题[SDAppThemeManager shareInstance].configViewModel themeViewModel;[[NSNotificationCenter defaultCenter] postNotificationName:K_APP_THEME_CHANGED object:nil userInfo:nil];
}之后通知界面切换对应的图片及风格图。
2.4 界面添加通知Observer
界面接收到通知后更新到对应的图片及颜色。 我这里是就写一个切换Tabbar新年主题风格图片。
[[NSNotificationCenter defaultCenter] addObserver:self selector:selector(systemAppThemeChanged:) name:K_APP_THEME_CHANGED object:nil];切换Tabbar新年主题风格图片
- (void)updateThemeConfig {//主题可以更改tabbar样式SDAppThemeConfigViewModel *themeConfigViewModel [SDAppThemeManager shareInstance].configViewModel;UIImage *backgroundImage;if (themeConfigViewModel.tabbar.t_backgroundImage) {backgroundImage themeConfigViewModel.tabbar.t_backgroundImage;} else {NSString *bgColor themeConfigViewModel.tabbar.backgroundColor;backgroundImage [UIImage imageWithColor:[UIColor colorWithHexString:bgColor] size:CGSizeMake(20.0, 20.0)];backgroundImage [backgroundImage stretchableImageWithLeftCapWidth:backgroundImage.leftCapWidth*0.5 topCapHeight:backgroundImage.topCapHeight*0.5];}self.sdTabbar.bgroundImage backgroundImage;NSString *showLine themeConfigViewModel.tabbar.showLine;self.sdTabbar.showLine [showLine boolValue];self.sdTabbar.lineColor [UIColor colorWithHexString:themeConfigViewModel.tabbar.lineColor];UIColor *badgeBGColor [UIColor colorWithHexString:themeConfigViewModel.tabbar.badgeBgColor];SDTabbarItem *homeItem [self themeTabbarItem:themeConfigViewModel.tabbar.lianlian];homeItem.identifier home;homeItem.badgeColor badgeBGColor;SDTabbarItem *addressbookItem [self themeTabbarItem:themeConfigViewModel.tabbar.message];addressbookItem.identifier addressbook;addressbookItem.badgeColor badgeBGColor;SDTabbarItem *discoveryItem [self themeTabbarItem:themeConfigViewModel.tabbar.guangguang];discoveryItem.identifier discovery;discoveryItem.badgeColor badgeBGColor;SDTabbarItem *mineItem [self themeTabbarItem:themeConfigViewModel.tabbar.mine];mineItem.identifier mine;mineItem.badgeColor badgeBGColor;[self.sdTabbar updateTabbarStyle:homeItem];[self.sdTabbar updateTabbarStyle:addressbookItem];[self.sdTabbar updateTabbarStyle:discoveryItem];[self.sdTabbar updateTabbarStyle:mineItem];
}- (void)systemAppThemeChanged:(NSNotification *)notification {[self updateThemeConfig];
}- (SDTabbarItem *)themeTabbarItem:(SDAppThemeConfigTabItemViewModel *)itemViewModel {SDTabbarItem *tabbarItem [[SDTabbarItem alloc] init];tabbarItem.title itemViewModel.title;tabbarItem.titleColor [UIColor colorWithHexString:itemViewModel.titleColor];tabbarItem.selectedTitleColor [UIColor colorWithHexString:itemViewModel.selectedTitleColor];tabbarItem.image itemViewModel.t_icon;tabbarItem.selectedImage itemViewModel.t_selectedIcon;return tabbarItem;
}tabbar的按钮更新根据对应的identifier切换到对应的图片及颜色配置。
/**更新tabbar样式param tabbarItem item*/
- (void)updateTabbarStyle:(SDTabbarItem *)tabbarItem {for (UIView *subView in self.subviews) {if ([subView isKindOfClass:[SDTabbarButton class]]) {SDTabbarButton *tabbarButton (SDTabbarButton *)subView;SDTabbarItem *item tabbarButton.tabbarItem;if (tabbarItem.identifier [tabbarItem.identifier isEqualToString:item.identifier]) {//更新tabbar[item copyClone:tabbarItem];tabbarButton.tabbarItem item;break;}}}
}三、将model序列化存储到本地目录
将主题数据序列号存储到本地目录方便下次打开APP进行获取。
SDAppThemeConfigDbManager.h
#import Foundation/Foundation.h
#import SDAppThemeManager.hinterface SDAppThemeConfigDbManager : NSObject (id)shareInstance;- (SDAppThemeViewModel *)getAppThemeViewModelFromDb;- (void)saveAppThemeViewModelToDb:(SDAppThemeViewModel *)info;endSDAppThemeConfigDbManager.m
#import SDAppThemeConfigDbManager.hstatic NSString *appThemeConfigPath sdAppThemeConfigPath;
static SDAppThemeConfigDbManager *instance nil;implementation SDAppThemeConfigDbManager (id)shareInstance
{static dispatch_once_t predicate;dispatch_once(predicate,^{instance [[self alloc] init];});return instance;
}- (NSString *)getAppThemeConfigPath
{NSArray *paths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentPath [paths objectAtIndex:0];NSString *path [documentPath stringByAppendingPathComponent:appThemeConfigPath];return path;
}- (SDAppThemeViewModel *)getAppThemeViewModelFromDb {NSString *dataFile [self getAppThemeConfigPath];try {SDAppThemeViewModel *viewModel [NSKeyedUnarchiver unarchiveObjectWithFile:dataFile];if (viewModel) {return viewModel;}} catch (NSException *e) {}return nil;
}- (void)saveAppThemeViewModelToDb:(SDAppThemeViewModel *)info {NSData *data [NSKeyedArchiver archivedDataWithRootObject:info];NSString *dataFile [self getAppThemeConfigPath];BOOL isSave [data writeToFile:dataFile atomically:YES];if (isSave) {NSLog(存储成功);} else {NSLog(存储失败);}
}end四、完整实现代码
需要用到主题ManagerSDAppThemeManager
SDAppThemeManager.h
#import Foundation/Foundation.h
//#import SDThemeConfigRequest.h
#import SDAppThemeConfigViewModel.h/**获取的app主题modelapp主题颜色版本号*/
interface SDAppThemeViewModel : NSObjectNSCodingproperty (nonatomic, strong) NSString *curDownloadurl; //当前版本的下载地址
property (nonatomic, strong) NSString *curVersion; //当前app主题颜色版本号property (nonatomic, strong) NSString *nextDownloadurl; //下一版本的下载地址
property (nonatomic, strong) NSString *nextVersion; //下一版本app主题颜色版本号endinterface SDAppThemeManager : NSObject (instancetype)shareInstance;property (nonatomic, strong) SDAppThemeConfigViewModel *configViewModel; //当前版本控制文件/**从服务器端加载APP主题接口*/
- (void)loadAppThemeConfig;/**加载系统主题资源*/
- (void)loadCacheThemeResource;endSDAppThemeManager.m
#import SDAppThemeManager.h
#import SDAppThemeConfigDbManager.h
#import SDAppThemeDownloadManager.h
#import INHttpClientUtil.himplementation SDAppThemeViewModel- (id)init {self [super init];if (self) {}return self;
}- (id)initWithCoder:(NSCoder *)aDecoder
{self [super init];if (self) {self.curDownloadurl [aDecoder decodeObjectForKey:kAppThemeCurDownloadurl];self.curVersion [aDecoder decodeObjectForKey:kAppThemeCurVersion];self.nextDownloadurl [aDecoder decodeObjectForKey:kAppThemeNextDownloadurl];self.nextVersion [aDecoder decodeObjectForKey:kAppThemeNextVersion];}return self;
}- (void)encodeWithCoder:(NSCoder *)aCoder
{[aCoder encodeObject:_curDownloadurl forKey:kAppThemeCurDownloadurl];[aCoder encodeObject:_curVersion forKey:kAppThemeCurVersion];[aCoder encodeObject:_nextDownloadurl forKey:kAppThemeNextDownloadurl];[aCoder encodeObject:_nextVersion forKey:kAppThemeNextVersion];
}- (void)clear {self.curDownloadurl nil;self.curVersion nil;self.nextDownloadurl nil;self.nextVersion nil;
}endstatic SDAppThemeManager *shareInstance nil;implementation SDAppThemeManager (instancetype)shareInstance {static dispatch_once_t onceToken;dispatch_once(onceToken, ^{shareInstance [[SDAppThemeManager alloc] init];shareInstance.configViewModel [SDAppThemeConfigViewModel defautThemeViewModel];});return shareInstance;
}/**从服务器端加载APP主题接口*/
- (void)loadAppThemeConfig {[[INHttpClientUtil sharedInstance] getWithClientType:HttpClientTypeDefault url:/v1/api/theme/system params:nil success:^(id responseObj) {NSString *code [NSString stringWithFormat:%,responseObj[code]];if ([0 isEqualToString:code] responseObj[data]) {NSString *curDownloadurl responseObj[data][curDownloadurl];NSString *curVersion responseObj[data][curVersion];NSString *nextDownloadurl responseObj[data][nextDownloadurl];NSString *nextVersion responseObj[data][nextVersion];SDAppThemeViewModel *themeViewModel [[SDAppThemeViewModel alloc] init];themeViewModel.curDownloadurl nextDownloadurl;themeViewModel.curVersion 2016.10.27;themeViewModel.nextDownloadurl nextDownloadurl;themeViewModel.nextVersion nextVersion;[[SDAppThemeConfigDbManager shareInstance] saveAppThemeViewModelToDb:themeViewModel];[[SDAppThemeDownloadManager shareInstance] downloadThemeZipPackage:themeViewModel];}} failure:^(HttpError *e) {DLog(request:%,e);}];
}/**加载系统主题资源*/
- (void)loadCacheThemeResource {[[SDAppThemeDownloadManager shareInstance] loadCacheThemeResource];
}end需要用到主题下载类SDAppThemeDownloadManager
SDAppThemeDownloadManager.h
#import Foundation/Foundation.h
//#import SDThemeConfigRequest.h
#import SDAppThemeManager.h
#import SDAppThemeConfigViewModel.h
#import SDAppThemeConfigDbManager.h#define K_APP_THEME_CHANGED K_APP_THEME_CHANGED
#define K_DEFAULT_APP_THEME_VERSION 1.0.0interface SDAppThemeDownloadManager : NSObject (instancetype)shareInstance;- (void)downloadThemeZipPackage:(SDAppThemeViewModel *)themeModel;/**加载系统主题资源*/
- (void)loadCacheThemeResource;endSDAppThemeDownloadManager.m
#import SDAppThemeDownloadManager.h
#import SDJsonUtil.h
//#import NSStringext.h
#import SSZipArchive/SSZipArchive.h
#import SDAppThemeManager.h
#import INHttpClientUtil.hstatic SDAppThemeDownloadManager *manager nil;interface SDAppThemeDownloadManager () SSZipArchiveDelegateendimplementation SDAppThemeDownloadManager (instancetype)shareInstance {static dispatch_once_t onceToken;dispatch_once(onceToken, ^{manager [[SDAppThemeDownloadManager alloc] init];});return manager;
}- (void)downloadThemeZipPackage:(SDAppThemeViewModel *)themeModel {//判断当前主题版本号下是否存在资源文件夹BOOL curThemeExist [self hasAppThemeVersion:themeModel.curVersion];if (!curThemeExist) {//如果不存在重新下载解压__block NSString *saveZipPath [self saveThemeTargetBasePath:themeModel.curVersion];__block NSString *themeUnZipPath [self saveThemeDirBasePath:themeModel.curVersion];__block NSString *curDownloadurl themeModel.curDownloadurl;//下载成功NSString *afileName [[NSURL URLWithString:curDownloadurl] lastPathComponent];__block NSString *afilePath [NSString pathWithComponents:[saveZipPath, afileName]];[[INHttpClientUtil sharedInstance] downloadFileURL:curDownloadurl savePath:afilePath fileName:afilePath tag:[afilePath hash] downloadProgress:^(CGFloat progress) {} success:^(NSURLResponse *response, NSString *filePath) {//下载成功NSString *fileName [[NSURL URLWithString:curDownloadurl] lastPathComponent];NSString *selFilePath [NSString pathWithComponents:[saveZipPath, fileName]];//准备执行解压方法[self onFileSelected:selFilePath unZipPath:themeUnZipPath];} failure:^(HttpError *e) {NSLog(failure request :%,e);}];} else {//如果存在直接显示__block NSString *saveZipPath [self saveThemeTargetBasePath:themeModel.curVersion];__block NSString *themeUnZipPath [self saveThemeDirBasePath:themeModel.curVersion];__block NSString *curDownloadurl themeModel.curDownloadurl;//下载成功NSString *fileName [[NSURL URLWithString:curDownloadurl] lastPathComponent];NSString *filePath [NSString pathWithComponents:[saveZipPath, fileName]];//准备执行解压方法[self unzipCompltion:themeUnZipPath];}/*//判断下一主题版本号下是否存在资源文件夹中BOOL nextThemeExist [self hasAppThemeVersion:themeModel.nextVersion];if (!nextThemeExist) {//如果不存在重新下载解压__block NSString *saveZipPath [self saveThemeTargetBasePath:themeModel.nextVersion];[[HttpClient sharedInstance] downloadFileURL:themeModel.curDownloadurl savePath:saveZipPath fileName:saveZipPath tag:[saveZipPath hash] success:^(id responseObj) {//下载成功} failure:^(HttpException *e) {//下载失败}];}*/
}// 解压
- (void)releaseZipFilesWithUnzipFileAtPath:(NSString *)zipPath destination:(NSString *)unzipPath {NSError *error;// 如果解压成功if ([SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath overwrite:YES password:nil error:error delegate:self]) {// 存储主题的色调[self unzipCompltion:unzipPath];} else {NSLog(%,error);}
}/**调用解压文件param unzipPath 获取地址*/
- (void)unzipCompltion:(NSString *)unzipPath {// 存储主题的色调// 已经存储主题tabbar图片、navigationbar图片、配置文件等等资源NSArray *folders [[NSFileManager defaultManager] contentsOfDirectoryAtPath:unzipPath error:NULL];NSString *selectedFilePath unzipPath;NSString *aPath [folders lastObject];NSString *fullPath [unzipPath stringByAppendingPathComponent:aPath];selectedFilePath fullPath;NSString *configPath [NSString stringWithFormat:%/config.json,selectedFilePath];NSFileHandle *fh [NSFileHandle fileHandleForReadingAtPath:configPath];NSData *data [fh readDataToEndOfFile];NSString *jsonStr [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSMutableDictionary *dict [[NSMutableDictionary alloc] initWithDictionary:[SDJsonUtil dictionaryWithJsonString:jsonStr]];NSLog(theme config.json:%,dict);SDAppThemeConfigViewModel *themeViewModel [SDAppThemeConfigViewModel themeViewModel:jsonStr];themeViewModel.t_globalImage [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.globalImage];themeViewModel.navigation.t_backgroundImage [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.navigation.backgroundImage];themeViewModel.tabbar.t_backgroundImage [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.backgroundImage];themeViewModel.tabbar.lianlian.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.lianlian.icon];themeViewModel.tabbar.lianlian.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.lianlian.selectedIcon];themeViewModel.tabbar.guangguang.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.guangguang.icon];themeViewModel.tabbar.guangguang.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.guangguang.selectedIcon];themeViewModel.tabbar.message.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.message.icon];themeViewModel.tabbar.message.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.message.selectedIcon];themeViewModel.tabbar.mine.t_icon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.mine.icon];themeViewModel.tabbar.mine.t_selectedIcon [self imageWithDocumentoryName:[NSString stringWithFormat:%,selectedFilePath] imageName:themeViewModel.tabbar.mine.selectedIcon];//配置全局主题[SDAppThemeManager shareInstance].configViewModel themeViewModel;[[NSNotificationCenter defaultCenter] postNotificationName:K_APP_THEME_CHANGED object:nil userInfo:nil];
}/**准备执行解压方法param selectedPath 原先文件路径param unZipPath 解压文件路径*/
- (void)onFileSelected:(NSString *)selectedPath unZipPath:(NSString *)unZipPath {NSURL *fileURL [NSURL fileURLWithPath:selectedPath];NSString *fileNameComponent fileURL.lastPathComponent;// 获取文件的扩展名NSString *extension [[fileNameComponent pathExtension] lowercaseString];// 如果是zip类型的压缩包文件则进行解压if ([extension isEqualToString:zip]) {// 设置解压路径[self releaseZipFilesWithUnzipFileAtPath:selectedPath destination:unZipPath];}
}/**判断当前path路径是否存在param themeVersion 主题版本号return 是否文件*/
- (BOOL)hasAppThemeVersion:(NSString *)themeVersion {NSString *pathOfLibrary [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];NSString *path [pathOfLibrary stringByAppendingPathComponent:[NSString stringWithFormat:dftheme-%,themeVersion]];NSArray *folders [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];if (!(folders folders.count 0)) {return NO;}NSString *aPath [folders lastObject];NSString *fullPath [path stringByAppendingPathComponent:aPath];NSFileManager *fileManager [NSFileManager defaultManager];BOOL result [fileManager fileExistsAtPath:fullPath];return result;
}/**主题未解压下载目录param themeVersion 主题版本号return 最后path*/
- (NSString *)saveThemeTargetBasePath:(NSString *)themeVersion {NSString *pathOfLibrary [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];NSString *path [pathOfLibrary stringByAppendingPathComponent:[NSString stringWithFormat:dfThemesZip-%,themeVersion]];[self createDirectory:path];return path;
}/**主题解压目录param themeVersion 主题版本号return 最后path*/
- (NSString *)saveThemeDirBasePath:(NSString *)themeVersion {NSString *pathOfLibrary [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];NSString *path [pathOfLibrary stringByAppendingPathComponent:[NSString stringWithFormat:dftheme-%,themeVersion]];[self createDirectory:path];return path;
}- (void)createDirectory:(NSString *)path {NSError *error nil;[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YESattributes:nil error:error];if (error) {NSLog(Create directory error: %, error);}
}/**获取使用存储在沙盒里的图片param documentoryName 文件目录param imageName 图片名字return 图片*/
- (UIImage *)imageWithDocumentoryName:(NSString *)documentoryNameimageName:(NSString *)imageName {// 如果文件名不存在或者文件名为空则返回空if (!imageName || [imageName isEqualToString:]) {return nil;}NSString *imgPath [documentoryName stringByAppendingPathComponent:[NSString stringWithFormat:%,imageName]];UIImage *image [UIImage imageWithContentsOfFile:imgPath];if (image) {return image;}return [UIImage imageNamed:imageName];
}/**加载系统主题资源*/
- (void)loadCacheThemeResource {SDAppThemeViewModel *themeVersionViewModel [[SDAppThemeConfigDbManager shareInstance] getAppThemeViewModelFromDb];if (themeVersionViewModel ![K_DEFAULT_APP_THEME_VERSION isEqualToString:themeVersionViewModel.curVersion]) {[self downloadThemeZipPackage:themeVersionViewModel];}
}end
需要用到主题数据SDAppThemeConfigViewModel
SDAppThemeConfigViewModel.h
#import Foundation/Foundation.h
#import UIKit/UIKit.h/**Navigation主题样式*/
interface SDAppThemeConfigNavViewModel : NSObjectproperty (nonatomic, strong) NSString *backgroundColor;
property (nonatomic, strong) NSString *backgroundImage;property (nonatomic, strong) UIImage *t_backgroundImage;property (nonatomic, strong) NSString *btnImageColor;
property (nonatomic, strong) NSString *btnTitleColor;
property (nonatomic, strong) NSString *navTitleColor;property (nonatomic, strong) NSString *showLine;
property (nonatomic, strong) NSString *lineColor;end/**单个tab按钮样式*/
interface SDAppThemeConfigTabItemViewModel : NSObjectproperty (nonatomic, strong) NSString *title;
property (nonatomic, strong) NSString *titleColor;
property (nonatomic, strong) NSString *selectedTitleColor;
property (nonatomic, strong) NSString *icon;
property (nonatomic, strong) NSString *selectedIcon;property (nonatomic, strong) UIImage *t_icon;
property (nonatomic, strong) UIImage *t_selectedIcon;end/**tabbar样式*/
interface SDAppThemeConfigTabViewModel : NSObjectproperty (nonatomic, strong) NSString *backgroundColor;
property (nonatomic, strong) NSString *backgroundImage;
property (nonatomic, strong) NSString *showLine;
property (nonatomic, strong) NSString *lineColor;
property (nonatomic, strong) NSString *badgeBgColor;property (nonatomic, strong) UIImage *t_backgroundImage;property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *lianlian;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *guangguang;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *message;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *shop;
property (nonatomic, strong) SDAppThemeConfigTabItemViewModel *mine;end/**将本地的主题config.json转成viewmodel*/
interface SDAppThemeConfigViewModel : NSObjectproperty (nonatomic, strong) NSString *globalColor;
property (nonatomic, strong) NSString *globalImage;
property (nonatomic, strong) SDAppThemeConfigNavViewModel *navigation;
property (nonatomic, strong) SDAppThemeConfigTabViewModel *tabbar;property (nonatomic, strong) UIImage *t_globalImage; (SDAppThemeConfigViewModel *)themeViewModel:(NSString *)themeJson; (SDAppThemeConfigViewModel *)defautThemeViewModel;endSDAppThemeConfigViewModel.m
#import SDAppThemeConfigViewModel.h
#import NSObjectYYModel.h/**Navigation主题样式*/
implementation SDAppThemeConfigNavViewModelend/**单个tab按钮样式*/
implementation SDAppThemeConfigTabItemViewModelend/**tabbar样式*/
implementation SDAppThemeConfigTabViewModelend/**将本地的主题config.json转成viewmodel*/
implementation SDAppThemeConfigViewModel (SDAppThemeConfigViewModel *)themeViewModel:(NSString *)themeJson {return [SDAppThemeConfigViewModel modelWithJSON:themeJson];
} (SDAppThemeConfigViewModel *)defautThemeViewModel {SDAppThemeConfigViewModel *viewModel [[SDAppThemeConfigViewModel alloc] init];SDAppThemeConfigNavViewModel *navConfigViewModel [[SDAppThemeConfigNavViewModel alloc] init];navConfigViewModel.backgroundColor 171013;navConfigViewModel.btnImageColor ffffff;navConfigViewModel.btnTitleColor ffffff;navConfigViewModel.navTitleColor ffffff;viewModel.navigation navConfigViewModel;return viewModel;
}end主题配置序列化存储本地SDAppThemeConfigDbManager
SDAppThemeConfigDbManager.h
#import Foundation/Foundation.h
#import SDAppThemeManager.hinterface SDAppThemeConfigDbManager : NSObject (id)shareInstance;- (SDAppThemeViewModel *)getAppThemeViewModelFromDb;- (void)saveAppThemeViewModelToDb:(SDAppThemeViewModel *)info;endSDAppThemeConfigDbManager.m
#import SDAppThemeConfigDbManager.hstatic NSString *appThemeConfigPath sdAppThemeConfigPath;
static SDAppThemeConfigDbManager *instance nil;implementation SDAppThemeConfigDbManager (id)shareInstance
{static dispatch_once_t predicate;dispatch_once(predicate,^{instance [[self alloc] init];});return instance;
}- (NSString *)getAppThemeConfigPath
{NSArray *paths NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentPath [paths objectAtIndex:0];NSString *path [documentPath stringByAppendingPathComponent:appThemeConfigPath];return path;
}- (SDAppThemeViewModel *)getAppThemeViewModelFromDb {NSString *dataFile [self getAppThemeConfigPath];try {SDAppThemeViewModel *viewModel [NSKeyedUnarchiver unarchiveObjectWithFile:dataFile];if (viewModel) {return viewModel;}} catch (NSException *e) {}return nil;
}- (void)saveAppThemeViewModelToDb:(SDAppThemeViewModel *)info {NSData *data [NSKeyedArchiver archivedDataWithRootObject:info];NSString *dataFile [self getAppThemeConfigPath];BOOL isSave [data writeToFile:dataFile atomically:YES];if (isSave) {NSLog(存储成功);} else {NSLog(存储失败);}
}end至此实现获取下载主题配置更切换主题的代码实现完成。
五、小结
iOS开发-实现获取下载主题配置更切换主题主要是通过请求服务端配置的主题配置、下载主题、解压保存到本地。通知界面获取对应的图片及颜色等。
学习记录每天不停进步。 文章转载自: http://www.morning.ryfpx.cn.gov.cn.ryfpx.cn http://www.morning.rljr.cn.gov.cn.rljr.cn http://www.morning.mjdbd.cn.gov.cn.mjdbd.cn http://www.morning.benqc.com.gov.cn.benqc.com http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.xjbtb.cn.gov.cn.xjbtb.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.rbylq.cn.gov.cn.rbylq.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.qnklx.cn.gov.cn.qnklx.cn http://www.morning.dtlnz.cn.gov.cn.dtlnz.cn http://www.morning.nyhtf.cn.gov.cn.nyhtf.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.ydwsg.cn.gov.cn.ydwsg.cn http://www.morning.pylpd.cn.gov.cn.pylpd.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.jfqqs.cn.gov.cn.jfqqs.cn http://www.morning.xyjlh.cn.gov.cn.xyjlh.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.dbrpl.cn.gov.cn.dbrpl.cn http://www.morning.cknws.cn.gov.cn.cknws.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.ndngj.cn.gov.cn.ndngj.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.fwdln.cn.gov.cn.fwdln.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.bsghk.cn.gov.cn.bsghk.cn http://www.morning.xbckm.cn.gov.cn.xbckm.cn http://www.morning.xzrbd.cn.gov.cn.xzrbd.cn http://www.morning.rxxdk.cn.gov.cn.rxxdk.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.wyjhq.cn.gov.cn.wyjhq.cn http://www.morning.dtzsm.cn.gov.cn.dtzsm.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.dqrhz.cn.gov.cn.dqrhz.cn http://www.morning.bsjpd.cn.gov.cn.bsjpd.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.tsflw.cn.gov.cn.tsflw.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.fwgnq.cn.gov.cn.fwgnq.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.lkkgq.cn.gov.cn.lkkgq.cn http://www.morning.slysg.cn.gov.cn.slysg.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.chbcj.cn.gov.cn.chbcj.cn http://www.morning.gwsfq.cn.gov.cn.gwsfq.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.rnmdp.cn.gov.cn.rnmdp.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.pcngq.cn.gov.cn.pcngq.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.hgscb.cn.gov.cn.hgscb.cn http://www.morning.wpcfh.cn.gov.cn.wpcfh.cn http://www.morning.swkzr.cn.gov.cn.swkzr.cn http://www.morning.bqppr.cn.gov.cn.bqppr.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn