OC--UIApplication基础知识整理
2017-03-08 本文已影响19人
啊哈呵
UIApplication
UIApplication属性和方法
//获得UIApplication
UIApplication *application=[UIApplication sharedApplication];
//获得UIApplication Delegate对象
[application delegate];
//UIWindow数组
[application windows];
//UIWindow数组中最后调用makeKeyAndVisible方法的UIWindow对象
[application keyWindow];
[application beginIgnoringInteractionEvents];//开始忽略Event
//...中间调用动画等操作
[application endIgnoringInteractionEvents];//结束忽略Event
application.applicationSupportsShakeToEdit = YES; //晃动是否有撤销或者重做动作
NSURL *URL=nil;
//不设置options,启动Safari方式打开这个Link的代表的网站
[application openURL:URL options:@{} completionHandler:nil];
//设置options @{UIApplicationOpenURLOptionUniversalLinksOnly : @YES}
//打开Universal Link所对应的App
[application openURL:URL options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @YES} completionHandler:nil];
//不让手机休眠
application.idleTimerDisabled = YES;
//隐藏状态条
[application setStatusBarHidden:YES];
[application setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
//设置状态条的样式
[application setStatusBarStyle:UIStatusBarStyleDefault];
[application statusBarStyle];
//设置状态条的方向
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
//状态条的Frame
[application statusBarFrame];
//网络是否可见--小菊花
[application isNetworkActivityIndicatorVisible];
//badge数字--icon小红点
application.applicationIconBadgeNumber = 2;
//屏幕的方向
[application userInterfaceLayoutDirection];
//远程通知
UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:mySettings]; //注册远程推送通知
[application registerForRemoteNotifications];//注册
[application unregisterForRemoteNotifications];//注销
//本地通知 IOS10已经修改,由于项目没用本地推送,没有整理
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = date; //时间
localNotif.timeZone = [NSTimeZone localTimeZone]; //时区
localNotif.repeatInterval = NSCalendarUnitMinute; //间隔
localNotif.soundName = UILocalNotificationDefaultSoundName; //声音
localNotif.alertBody = @"Local Test"; //通知内容
localNotif.applicationIconBadgeNumber = 1; //数字标示
localNotif.userInfo = @{@"key":@"test"}; //info
[application scheduleLocalNotification:localNotif]; //注册通知
[application presentLocalNotificationNow:localNotif]; //立即通知
[application cancelAllLocalNotifications]; //取消所有通知
[application cancelLocalNotification:localNotif]; //取消特定的通知
NSArray *arr = [application scheduledLocalNotifications];
//后台运行相关
[application applicationState]; //app状态
[application setMinimumBackgroundFetchInterval:3600]; //设置后台运行时间
NSTimeInterval remainTime = [application backgroundTimeRemaining]; //app后台运行的时间
NSLog(@"remainTIme = %f",remainTime);
int state = [application backgroundRefreshStatus]; //后台刷新的状态
NSLog(@"state = %d",state);
[application beginBackgroundTaskWithName:@"taskOne" expirationHandler:^{}];
[application beginBackgroundTaskWithExpirationHandler:^{}];
[application endBackgroundTask:1];
//远程的控制相关
[application beginReceivingRemoteControlEvents];
[application endReceivingRemoteControlEvents];
UIApplicationDelegate方法
// 程序启动将要完成的时候调用
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions{
//被其他应用调用--UIApplicationLaunchOptionsURLKey
NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
if(url) {//做出相应的判断
if ([[url scheme] isEqualToString:@"myapp"]) {
//处理链接
}
}
//若用户直接启动,lauchOptions内无数据;
//若由其他应用程序通过openURL:启动,则UIApplicationLaunchOptionsURLKey对应的对象为启动URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey对应启动的源应用程序的bundle ID (NSString);
//若由本地通知启动,则UIApplicationLaunchOptionsLocalNotificationKey对应的是为启动应用程序的的本地通知对象(UILocalNotification);
//若由远程通知启动,则UIApplicationLaunchOptionsRemoteNotificationKey对应的是启动应用程序的的远程通知信息userInfo(NSDictionary);
return YES;
}
// 程序启动完成的时候调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions{return YES;}
// 当应用程序获取焦点的时候调用--恢复数据
- (void)applicationDidBecomeActive:(UIApplication *)application{}
// 当应用程序失去焦点的时候调用--保存数据
- (void)applicationWillResignActive:(UIApplication *)application{}
// 当应用程序进入后台的时候调用,
- (void)applicationDidEnterBackground:(UIApplication *)application{}
// 当应用程序进入进台的时候调用
- (void)applicationWillEnterForeground:(UIApplication *)application{}
// 程序内存警告,可以一些内存的释放来抢救程序,如SD的缓存清除
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{}
// 当应用程序关闭的时候调用
- (void)applicationWillTerminate:(UIApplication *)application{}
/**
以上是UIApplication基本的周期方法,会有很多种情况:
1、程序启动:
willFinishLaunchingWithOptions -> didFinishLaunchingWithOptions -> applicationDidBecomeActive
2、程序从前台退出到后台
applicationWillResignActive -> applicationDidEnterBackground
3、程序从后台进入到前台
applicationWillEnterForeground -> applicationDidBecomeActive
4、双击home键切换程序:
applicationWillResignActive -> applicationDidEnterBackground
5、在前台双击home键杀死程序
applicationWillResignActive -> applicationDidEnterBackground -> applicationWillTerminate
6、从其他程序前台双击home键杀死后台程序
applicationWillTerminate
7、下拉通知栏或者上拉工具栏
applicationWillResignActive
*/
// 当系统时间发生改变时执行
- (void)applicationSignificantTimeChange:(UIApplication *)application{}
//StatusBar相关
// 当StatusBar的方向要将变化时执行
- (void)application:(UIApplication *)application willChangeStatusBarOrientation:(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration {}
// 当StatusBar的方向要将变化时执行
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation {}
// 当StatusBar的frame要将变化时执行
- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame{}
// 当StatusBar的frame已经变化时执行
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame {}
//处理SchemeURL [application openURL:url]; IOS8
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {return YES;}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation { return YES;}
//处理SchemeURL IOS9
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
}
推送这块苹果一直改,IOS7改到IOS10了,苹果爹爹厉害,年年在修改推送,项目已经一堆判断,理解并总结iOS7-10推送通知流程
最后还是用第三方(极光、个推),感谢他们😁