UIApplication

  1、UIApplication 是 iPhone 应用程序的开始并且负责初始化并显示 UIWindow,并负责加载应用程序的第一个 UIView 到 UIWindow 窗体中。

  UIApplication 的另一个任务是帮助管理应用程序的生命周期,而 UIApplication 通过一个名字为 UIApplicationDelegate 的代理类来履行这个任务。

  尽管 UIApplication 会负责接收事件,而 UIApplicationDelegate 则决定应用程序如何去响应这些事件,UIApplicationDelegate 可以处理的事件包括应用程序的生命周期事件(比如程序启动和关闭)、系统事件(比如来电、记事项警 告)。

  Application 是一个单例对象

  iOS程序创建的第一个对象就是UIApplication对象

  通过[UIApplication sharedApplication]可以获得这个单例对象

 

2、默认创建一个single模板时,系统自动创建了一个UIApplication的对象

 

这里的nil其实是@"UIApplication",在UIApplicationMain中,同时又设置了application的代理:@"AppDelegate"

 // argc:系统传入参数的个数
 // argv:系统传入参数的值的列表
 // principalClassName:传入主要的类名
 // 第四个参数表示:给应用程序"application"指定一个代理对象
 return UIApplicationMain(argc, argv, @"UIApplication", @"AppDelegate");

 

应用程序启动之后,先创建Application,再创建它的代理AppDelegate,之后创建UIwindow,再创建控制器,创建控制器的view,然后将控制器的view添加到UIWindow上。

 

3.UIApplication常用属性:

1-applicationIconBadgeNumber设置应用图标右上角的提示数字

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIApplication *app = [UIApplication sharedApplication];
    
    /**
     iOS8.0之后通知消息要进行注册
     set to 0 to hide. default is 0. In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.
     */
    if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0){
        //注册
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
                                               
        [app registerUserNotificationSettings:setting];
    }
    app.applicationIconBadgeNumber = 20;
    
}

 

2-设置应用联网状态

@property(nonatomic,getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible __TVOS_PROHIBITED; // showing network spinning gear in status bar. default is NO

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIApplication *app = [UIApplication sharedApplication];
    
    app.networkActivityIndicatorVisible = YES;
}

 

4、常用方法

打开URL

- (BOOL)openURL:(NSURL*)url NS_EXTENSION_UNAVAILABLE_IOS("");

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UIApplication *app = [UIApplication sharedApplication];
    
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    [app openURL:url];
}

 

posted @ 2016-08-16 23:28  以梦为码的诗人  阅读(239)  评论(0编辑  收藏  举报