基础
UIApplication
的父类为UIResponder
,在main函数中执行了以下的语句创建了UIApplication
单例
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
在iOS中一个UIApplication
就代表一个应用程序。
UIApplication的简单使用
- 获取单例对象
UIApplication *application = [UIApplication sharedAplication];
- 程序全局Badge
application.applicationIconBadgeNumber = 4;
苹果为了增强用户体验,在iOS8以后我们需要创建通知才能实现图标右上角提醒,iOS8之前直接设置applicationIconBadgeNumber
的值即可。
app.applicationIconBadgeNumber = 10;
// 创建通知对象
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
// 注册用户通知
[app registerUserNotificationSettings:setting];
- 联网指示器
application.networkActivityIndicatorVisible=YES;
- openURL使用
[application openURL:[NSURL URLWithString:@"tel://10086"]];
还可以跳转到一些设置项,他们的URL如下:
[app openURL:[NSURL URLWithString:@"prefs:root=WIFI"]];
About — prefs:root=General&path=About
Accessibility — prefs:root=General&path=ACCESSIBILITY
Airplane Mode On — prefs:root=AIRPLANE_MODE
Auto-Lock — prefs:root=General&path=AUTOLOCK
Brightness — prefs:root=Brightness
Bluetooth — prefs:root=General&path=Bluetooth
Date & Time — prefs:root=General&path=DATE_AND_TIME
FaceTime — prefs:root=FACETIME
General — prefs:root=General
Keyboard — prefs:root=General&path=Keyboard
iCloud — prefs:root=CASTLE
iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP
International — prefs:root=General&path=INTERNATIONAL
Location Services — prefs:root=LOCATION_SERVICES
Music — prefs:root=MUSIC
Music Equalizer — prefs:root=MUSIC&path=EQ
Music Volume Limit — prefs:root=MUSIC&path=VolumeLimit
Network — prefs:root=General&path=Network
Nike + iPod — prefs:root=NIKE_PLUS_IPOD
Notes — prefs:root=NOTES
Notification — prefs:root=NOTIFICATIONS_ID
Phone — prefs:root=Phone
Photos — prefs:root=Photos
Profile — prefs:root=General&path=ManagedConfigurationList
Reset — prefs:root=General&path=Reset
Safari — prefs:root=Safari
Siri — prefs:root=General&path=Assistant
Sounds — prefs:root=Sounds
Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK
Store — prefs:root=STORE
Twitter — prefs:root=TWITTER
Usage — prefs:root=General&path=USAGE
VPN — prefs:root=General&path=Network/VPN
Wallpaper — prefs:root=Wallpaper
Wi-Fi — prefs:root=WIFI
- 手势识别
application.applicationSupportsShakeToEdit = YES;
- 设置屏幕不熄灭
application.idleTimerDisabled = YES;
- 在map上显示一个地址
NSString addressText = @"1 Infinite Loop, Cupertino, CA 95014";
-
addressText = [addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
-
NSString urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
-
[application openURL:[NSURLURLWithString:urlText]];
事件处理
UIApplication
接收到任何的消息都会传送给UIApplicationDelegate
,
UIApplication
实例化之后会先调用
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions
iOS App的声明周期