
正文
iOS:UIApplication和它对象的代理
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
<1>UIApplication的对象是单例对象 类方法:UIApplication *app = [UIApplication sharedAppplication]
<2>UIScreen的对象是单例对象 类方法:UIScreen *screen = [UIScreen mainScreen]
UIApplication的代理的协议的一些操作:

启动应用程序,代理帮助实现的程序状态的转换

UIApplication 应用程序对象的常用设置
(1)设置应用程序图标右上角的红色提醒数字(如QQ消息的时候,图标上面会显示1,2,3条新信息等) Badge
@property(nonatomic) NSInteger applicationIconBadgeNumber;
(2)设置联网指示器的可见性
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible;
(3)说明:其实状态栏的管理,每一个单独的视图控制器可以管理,也可以让唯一的UIApplication的对象统一管理。
方式一:app统一管理:

状态栏的样式 -(UIStatusBarStyle)preferredStatusBarStyle;
app.statusBarStyle=UIStatusBarStyleDefault;//默认(黑色)
状态栏的可见性 -(BOOL)prefersStatusBarHidden;
app.statusBarHidden=YES //隐藏
方式二:每一个视图控制器单独管理:
//隐藏状态栏-(BOOL)prefersStatusBarHidden
{
return NO;
}
//设置状态栏的样式
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
(4)UIApplication有个功能十分强大的openURL:方法
URL:统一资源定位符,用来唯一的表示一个资源。
URL格式:协议头://主机地址/资源路径
-(BOOL)openURL:(NSURL*)url;
-openURL:方法的部分功能有
打电话 [app openURL:[NSURLURLWithString:@"tel://10086"]];
发短信 [app openURL:[NSURLURLWithString:@"sms://10086"]];
发邮件 [app openURL:[NSURLURLWithString:@"mailto://12345@qq.com"]];
打开一个网页资源 [app openURL:[NSURLURLWithString:@"http://www.baidu.com"]];
以下是代码的具体体现:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view setBackgroundColor:[UIColor blackColor]];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//每一个视图控制器单独控制自己的状态栏
//隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
return NO;
} //设置状态栏的样式
-(UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
@end
Application对象完成的各种功能:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. //在iOS8中,必须经过用户允许才能设置badge/alert/sound
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil]; UIApplication *app = [UIApplication sharedApplication];
[app registerUserNotificationSettings:setting]; //设置应用程序图标上显示的提醒数字
app.applicationIconBadgeNumber = ; //设置联网指示器的可见性(默认是NO)
app.networkActivityIndicatorVisible = YES; //设置状态栏的样式
app.statusBarStyle = UIStatusBarStyleLightContent; //隐藏状态栏
app.statusBarHidden = NO; //openURL功能(URL统一资源定位符)
//1.打电话
[app openURL:[NSURL URLWithString:@"tel://10086"]]; //2.发短信
[app openURL:[NSURL URLWithString:@"sms://10086"]]; //3.发邮件
[app openURL:[NSURL URLWithString:@"mailto://1360074459@qq.com"]]; //4.打开网络资源
[app openURL:[NSURL URLWithString:@"http://www.baidu.com"]]; //5.打开其他的应用程序
[app openURL:[NSURL URLWithString:@"prefs:"]]; return YES;
}







