iOS获取获取AppIcon and LaunchImage's name(app图标和启动图片名字图标和启动图片名字)
主要介绍了iOS获取AppIcon and LaunchImage's name(app图标和启动图片名字)的相关资料,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧
在某种场景下,可能我们需要获取app的图标名称和启动图片的名称。比如说app在前台时,收到了远程通知但是通知栏是不会有通知提醒的,这时我想做个模拟通知提示,需要用到icon名称;再
比如在加载某个控制器时,想设置该控制器的背景图片为启动图片,需要用到启动图片名称。
而事实上icon图片放在系统AppIcon文件夹里,启动图片放在系统LaunchImage文件夹里,取这些图片的名称和其他一般资源图片名称不一样。
为了方便举例子,咱们先简单粗暴点
假设当前项目只支持iPhone设备,并且只支持竖屏;而且当前项目里已经设置好了AppIcon图标和启动图片,
如何获取如何获取icon图标名称和启动图片名称呢图标名称和启动图片名称呢 ??
上代码和打印日志:
/** 获取app的icon图标名称 */
- (void)getAppIconName{
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
//获取app中所有icon名字数组
NSArray *iconsArr = infoDict[@"CFBundleIcons"][@"CFBundlePrimaryIcon"][@"CFBundleIconFiles"];
//取最后一个icon的名字
NSString *iconLastName = [iconsArr lastObject];
//打印icon名字
NSLog(@"iconsArr: %@", iconsArr);
NSLog(@"iconLastName: %@", iconLastName);
/*
打印日志:
iconsArr: (
AppIcon29x29,
AppIcon40x40,
AppIcon60x60
)
iconLastName: AppIcon60x60
*/
}
/** 获取app的启动图片名称,并设置为本控制器背景图片 */
- (void)getLaunchImageName{
NSString *launchImageName = @""; //启动图片名称变量
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
//获取与当前设备匹配的启动图片名称
if (screenHeight == 480){ //4,4S
launchImageName = @"LaunchImage-700";
}
else if (screenHeight == 568){ //5, 5C, 5S, iPod
launchImageName = @"LaunchImage-700-568h";
}
else if (screenHeight == 667){ //6, 6S
launchImageName = @"LaunchImage-800-667h";
}
else if (screenHeight == 736){ // 6Plus, 6SPlus
launchImageName = @"LaunchImage-800-Landscape-736h";
}
if (launchImageName.length < 1) return;
//设备启动图片为控制器的背景图片
UIImage *img = [UIImage imageNamed:launchImageName];
self.view.backgroundColor = [UIColor colorWithPatternImage:img];
}
打印当前只支持iPhone设备并且只支持竖屏场景下的所有启动图片信息:
/** 打印app里面所有启动图片名称信息 */
- (void)printAllLaunchImageInfo{
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
//获取所有启动图片信息数组
NSArray *launchImagesArr = infoDict[@"UILaunchImages"];
NSLog(@"launchImagesArr: %@", launchImagesArr);
/*
打印日志:启动图片的名字是固定的
launchImagesArr: (
{
UILaunchImageMinimumOSVersion = "8.0";
UILaunchImageName = "LaunchImage-800-Portrait-736h";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{414, 736}";
},
{
UILaunchImageMinimumOSVersion = "8.0";
UILaunchImageName = "LaunchImage-800-Landscape-736h";
UILaunchImageOrientation = Landscape;
UILaunchImageSize = "{414, 736}";
},
{
UILaunchImageMinimumOSVersion = "8.0";
UILaunchImageName = "LaunchImage-800-667h";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{375, 667}";
},
{
UILaunchImageMinimumOSVersion = "7.0";
UILaunchImageName = "LaunchImage-700";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{320, 480}";
},
{
UILaunchImageMinimumOSVersion = "7.0";
UILaunchImageName = "LaunchImage-700-568h";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{320, 568}";
}
)
*/
}
看到了,项目AppIcon图标和启动图片信息,都可以从 [[NSBundle mainBundle] infoDictionary] 获得,当前这里面还包含了app的其他信息如版本、app名称、设备类型、支持方向。。。
打印所有信息看看:
/** 打印app工程配置信息 */
- (void)printInfoDictionary{
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSLog(@"%@", infoDict);
/*
打印日志:
{
BuildMachineOSBuild = 15G31;
CFBundleDevelopmentRegion = en;
CFBundleExecutable = TanTest;
CFBundleIcons = {
CFBundlePrimaryIcon = {
CFBundleIconFiles = (
AppIcon29x29,