一丶介绍
The UIApplication class provides a centralized point of control and
coordination for apps running in iOS. Every app has exactly one instance
of UIApplication (or, very rarely, a subclass of UIApplication). When an
app is launched, the system calls the UIApplicationMain function; among
its other tasks, this function creates a singletonUIApplication object.
Thereafter you access the object by calling the sharedApplication class
method.
UIApplication代表一个应用,是整个应用程序的象征,通过[UIApplication sharedApplication] 获取.
- 是一个单例.
- ios程序启动后创建的第一人对象就是UIApplication对象.
UIApplication用来干什么?
1.用来设置全局性的东西
2.打电话、发短信、发邮件、打开网站
3. ......
二丶用法
1.跳转iPhone设置界面
- (BOOL)openURL:(NSURL*)url NS_EXTENSION_UNAVAILABLE_IOS("");
- (BOOL)canOpenURL:(NSURL *)url NS_AVAILABLE_IOS(3_0);
//使用:
if ([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
地址介绍:
1.跳转到自己应用设置界面:UIApplicationOpenSettingsURLString
NS_AVAILABLE_IOS(8_0);
if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0f)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}
2.设置_其他:
@"prefs:root=General&path=About",//关于本机
@"prefs:root=General&path=ACCESSIBILITY",//辅助功能
@"prefs:root=AIRPLANE_MODE",//飞行模式
@"prefs:root=General&path=AUTOLOCK",//自动锁定
@"prefs:root=General&path=DATE_AND_TIME",//日期与时间
@"prefs:root=FACETIME",//faceTime
@"prefs:root=General&path=Keyboard",//键盘
@"prefs:root=CASTLE",//iCloud
@"prefs:root=CASTLE&path=STORAGE_AND_BACKUP",//存储空间
@"prefs:root=General&path=INTERNATIONAL",//语音与地区
@"prefs:root=LOCATION_SERVICES",//定位服务
@"prefs:root=ACCOUNT_SETTINGS",//邮件-通讯录-日历
@"prefs:root=MUSIC",//音乐
@"prefs:root=MUSIC&path=EQ",//音乐
@"prefs:root=MUSIC&path=VolumeLimit",//音乐
@"prefs:root=NIKE_PLUS_IPOD",//通用
@"prefs:root=NOTES",//备忘录
@"prefs:root=NOTIFICATIONS_ID",//通知
@"prefs:root=Phone",//电话
@"prefs:root=Photos",//照片与相机
@"prefs:root=General&path=ManagedConfigurationList",//描述文件
@"prefs:root=General&path=Reset",//还原
@"prefs:root=Sounds&path=Ringtone",//电话铃声
@"prefs:root=General&path=Assistant",
@"prefs:root=Sounds",//声音
@"prefs:root=General&path=SOFTWARE_UPDATE_LINK",//软件更新
@"prefs:root=STORE",//app store
@"prefs:root=TWITTER",
@"prefs:root=General&path=USAGE",
@"prefs:root=VIDEO",//视频
@"prefs:root=General&path=Network/VPN",
@"prefs:root=Wallpaper",//墙纸
@"prefs:root=WIFI",//wifi
@"prefs:root=INTERNET_TETHERING",//个人热点
@"prefs:root=Bluetooth"//蓝牙
3.Email
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"mailto://[email protected]"]];
4.Safari
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"http://coder-lee.lofter.com/"]];
5.sms打开短信
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"sms://10086"]];
6.直接拨打电话
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"tel://10086"]];
2.keyWindow
1.收起键盘
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
3.状态栏网络状态旋转齿轮
//显示YES,隐藏NO;
[UIApplication sharedApplication].networkActivityIndicatorVisible =YES;
4.在控制台里打印view的层级
控制台
po [[[UIApplication sharedApplication] keyWindow] recursiveDescription]
指定view:
po [self.view recursiveDescription];
5.防止屏幕睡眠
// default is NO
[[UIApplication sharedApplication].idleTimerDisabled=YES;
6.改更状态栏 颜色/显隐
设置下info;
info->View controller-based status bar appearance = no
UIApplication *app = [UIApplication sharedApplication];
白色[app setStatusBarStyle:UIStatusBarStyleLightContent];
黑色[app setStatusBarStyle: UIStatusBarStyleDefault];
显隐 [app setStatusBarHidden:NO];
7.代理回调
各个程序运行状态时代理的回调:
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//告诉代理进程启动但还没进入状态保存
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//告诉代理启动基本完成程序准备开始运行
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
//当应用程序将要入非活动状态执行,在此期间,应用程序不接收消息或事件,比如来电话了
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{ //当应用程序入活动状态执行,这个刚好跟上面那个方法相反
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
//当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。
}
- (void)applicationWillTerminate:(UIApplication *)application
{
//当程序将要退出是被调用,通常是用来保存数据和一些退出前的清理工作。这个需要要设置UIApplicationExitsOnSuspend的键值。
}
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
//当程序载入后执行
}
8.判断网络类型(私有api),建议用Reachabillity
//获取状态区
UIApplication *app = [UIApplication sharedApplication];
//怎么获取状态栏所有的子视图 KVC key找Value 系统提供好的
NSArray *array = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
for (id obj in array) {
if ([obj isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {
int type = [[obj valueForKey:@"dataNetworkType"] intValue];
NSLog(@"%d",type);
switch (type) {
case 0:
NSLog(@"无网络");
break;
case 1:
NSLog(@"2G网络");
break;
case 2:
NSLog(@"3G网络");
break;
case 3:
NSLog(@"4G网络");
break;
case 4:
NSLog(@"无网络");
break;
case 5:
NSLog(@"Wifi");
break;
default:
break;
}
}
}
9.跳转AppStore应用
假如你应用id=1234567890
itms-apps://itunes.apple.com/cn/app/hua-ying-dai/id1234567890?l=en&mt=
跳转应用评分
http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1234567890&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8
三丶扩展
1.获取项目沙盒大小
@interface UIApplication (ApplicationSize)
- (NSString *)applicationSize;
@end
@implementation UIApplication (ApplicationSize)
- (NSString *)applicationSize {
unsigned long long docSize = [self sizeOfFolder:[self documentPath]];
unsigned long long libSize = [self sizeOfFolder:[self libraryPath]];
unsigned long long cacheSize = [self sizeOfFolder:[self cachePath]];
unsigned long long total = docSize + libSize + cacheSize;
NSString *folderSizeStr = [NSByteCountFormatter stringFromByteCount:total countStyle:NSByteCountFormatterCountStyleFile];
return folderSizeStr;
}
- (NSString *)documentPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths firstObject];
return basePath;
}
- (NSString *)libraryPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths firstObject];
return basePath;
}
- (NSString *)cachePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths firstObject];
return basePath;
}
-(unsigned long long)sizeOfFolder:(NSString *)folderPath
{
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil];
NSEnumerator *contentsEnumurator = [contents objectEnumerator];
NSString *file;
unsigned long long folderSize = 0;
while (file = [contentsEnumurator nextObject]) {
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[folderPath stringByAppendingPathComponent:file] error:nil];
folderSize += [[fileAttributes objectForKey:NSFileSize] intValue];
}
return folderSize;
}
@end
四丶注意
1.上面的判断网络类型是私有api再强调一次;
2.[UIApplication sharedApplication]是单例不要alloc;
...后面再继续添加吧;