iOS Application

2018-08-13  本文已影响0人  迷了路_yy

认识UIApplication

UIApplication的常用属性

@property(nonatomic) NSInteger applicationIconBadgeNumber;

iOS 10前的使用方式

    UIApplication *app = [UIApplication sharedApplication];
    //App图标右上角数字
    app.applicationIconBadgeNumber = 12;
    
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    //需要注册通知对象
    [app registerUserNotificationSettings:setting];

iOS 10后的使用方式

    UIApplication *app = [UIApplication sharedApplication];
    //App图标右上角数字
    app.applicationIconBadgeNumber = 12;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
        
    }];
@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible __TVOS_PROHIBITED; // showing network spinning gear in status bar. default is NO
app.networkActivityIndicatorVisible = YES;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        app.networkActivityIndicatorVisible = NO;
    });
@property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;

iOS 9以后使用prefersStatusBarHidden方法隐藏状态栏

- (BOOL)prefersStatusBarHidden{
    return YES;
}
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://123456"] options:0 completionHandler:^(BOOL success) {
}];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"] options:0 completionHandler:^(BOOL success) {
}];

UIApplication的代理方法AppDelegate

//App启动完毕的时候调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"App启动完毕的时候调用");
    return YES;
}

//当App失去焦点的时候调用
- (void)applicationWillResignActive:(UIApplication *)application {
    //当应用程序即将从活动状态转移到非活动状态时发送。这可能发生在某些类型的临时中断(如来电或SMS消息)或用户退出应用程序并开始过渡到后台状态时。
    //使用此方法暂停正在进行的任务,禁用计时器,并使图形呈现回调失效。游戏应该使用这种方法暂停游戏。
    NSLog(@"App失去焦点");
}

//App进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
   //使用此方法释放共享资源、保存用户数据、使计时器失效,并存储足够的应用程序状态信息,以便在以后终止应用程序时将其恢复到当前状态
    NSLog(@"App已经进入后台");
}

//App将要从后台进入前台的时候调用
- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"App将要后台进入前台的时候调用");
}

//当App完全获取到焦点的时候调用,只有当App完全获取到焦点,才能与用户交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"App完全获取到焦点");
}

//当应用程序即将终止时调用。如果适当的保存数据
- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"应用程序即将终止时调用");
}

//App接收到内存警告的时候调用
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    NSLog(@"App内存警告");
}

main()函数的UIApplicationMain函数底层实现

int main(int argc, char * argv[]) {
    @autoreleasepool {
        //第三个参数:UIApplication的类名或子类的名称, nil == @"UIApplication"
        //第四个参数:UIApplication代理的代理名称,NSStringFromClass([AppDelegate class] == @"AppDelegate"
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

UIApplicationMain函数实现

  1. 根据第三个参数principalClassName传递的类名创建UIApplication对象
  2. 根据第四个参数delegateClassName创建AppDelegate代理对象,给UIAplication设置代理
  3. 开启run loop主事件循环,开始监听处理事件
  4. 加载Info.plist文件,判断是否指定main,如果指定了就会去加载.
上一篇下一篇

猜你喜欢

热点阅读