UIApplication實用方法
前言:
本文介紹的方法每一個人在項目都應用過,只是有的時候容易忘記每次都要去百度。因為有些方法在整個項目中可能就只會寫一次,基於此我只是做個筆記。
1. 每一個應用程序都有一個UIApplication對象,是一個單例。在程序中只能通過[UIApplication sharedApplication]來獲得這個單例對象,不能通過alloc去新建一個UIApplication對象
2. 利用UIApplication可以進行的應用級別操作舉例
(1)設置應用程序圖標右上角的紅色提醒數字(applocationIconBadgeNumber)
實例代碼:
//獲取到應用程序中UIApplication單例對象
//設置應用程序圖標右上角的紅色提醒數字
// 獲取當前應用程序的UIApplication對象
UIApplication *app = [UIApplication sharedApplication];
[UIApplication sharedApplication].applicationIconBadgeNumber = @"20";
//在IOS8以后要想設置applicationIconBadgeNumber,需要用戶的授權,在IOS8以后,需要加上下面的代碼
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 8.0) {
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[app registerUserNotificationSettings:settings];
}
效果圖:
(2)設置聯網指示器的可見:
netWworkActivityIndicatorVisible
實例代碼:
設置聯網指示器可見
app.networkActivityIndicatorVisible = YES;
效果圖:
(3)管理狀態欄
從iOS7開始,系統提供了2種管理狀態欄的方式
a.通過UIViewController管理(這意味着每一個UIViewController都可以擁有自己不同的狀態欄)
在iOS7以后默認情況下,狀態欄都是由UIViewController管理的,通過實現以下兩個方法:
狀態樣式:- (UIStatusBarStyle)preferredStatusBarStyle;
狀態欄的可見性: - (BOOL)prepersStatusBarHidden;
實例代碼:
- (UIStatusBarStyle)preferredStatusBarStyle{
//UIStatusBarStyleDefault //默認,黑色
//UIStatusBarStyleLightContent //白色
return UIStatusBarStyleLightContent;
}
//狀態欄是否隱藏
- (BOOL)prefersStatusBarHidden{
return YES;
}
效果圖:
通過UIApplication管理(一個應用程序的所有界面的狀態欄都由它統一管理)
如果想利用UIApplication來管理狀態欄,首先得修改Info.plist的設置(添加屬性View controller-based status bar appearance 並且設置其BOOL值為NO),然后在程序中實現以下代碼:
//設置狀態欄的樣式
app.statusBarStyle = UIStatusBarStyleDefault; //默認(黑色)
//白色
app.statusBarStyle = UIStatusBarStyleLightContent;
//設置是否隱藏狀態欄
app.statusBarHidden = NO;
//系統方法,當從tabbar push過來的時候不隱藏tabbar
- (BOOL)hidesBottomBarWhenPushed{
return NO;
}
#pragma mark - 禁止橫屏幕
- (BOOL)shouldAutorotate
{
return NO;
}
/**當前是否支持哪些轉屏*/
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
/**顯示狀態欄*/
- (BOOL)prefersStatusBarHidden
{
return NO;
}
(4)openURL:方法
UIApplication有個功能十分強大的openURL:方法
-(BOOL)openURL:(NSURL *)url;
//openURL:方法的部分功能有
//打電話:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“tel://18812345678”]];
//發短信:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“sms://18812345678"]];
//發郵件:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“mailto://marlonxlj@163.com”]];
//打開網頁:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@“http://www.baidu.com”]];
//調用谷歌地圖(Google Maps)
NSString *searchQuery = @"1 Infinite Loop, Cupertino, CA 95014";
searchQuery = [addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString=[NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", searchQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: urlString]];
//調用應用商店(AppStore),這個地址是可以官網上查到的
NSURL *appStoreUrl= [NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291589999&mt=8"];
[[UIApplication sharedApplication] openURL:appStoreUrl];
//調用appstore中程序的評論
NSString *str = [NSString stringWithFormat:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d",
m_appleID ];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];