OC UIApplication基礎知識整理

韋家冰發表於2017-12-13

####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推送通知流程 最後還是用第三方(極光、個推),感謝他們?

相關文章