UIApplication ,UIWindow等对象基础

2017-05-26  本文已影响85人  勇往直前888

UIApplication

#if UIKIT_DEFINE_AS_PROPERTIES
@property(class, nonatomic, readonly) UIApplication *sharedApplication NS_EXTENSION_UNAVAILABLE_IOS("Use view controller based solutions where appropriate instead.");
#else
+ (UIApplication *)sharedApplication NS_EXTENSION_UNAVAILABLE_IOS("Use view controller based solutions where appropriate instead.");
#endif
int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
// iOS7以前,包括iOS7
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [UIApplication sharedApplication].applicationIconBadgeNumber = 10;

    // ... 其他代码

    return YES;
}
// iOS8, iOS9
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [UIApplication sharedApplication].applicationIconBadgeNumber = 10;
    // 创建通知对象
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
    // 注册用户通知
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];

    // ... 其他代码

    return YES;
}
// iOS10之后
#import <UserNotifications/UserNotifications.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIApplication.sharedApplication.applicationIconBadgeNumber = 10;
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (!error) {
            NSLog(@"request authorization succeeded!");
        }
    }];

    // ... 其他代码

    return YES;
}

玩转 iOS 10 推送 —— UserNotifications Framework(上)

UIApplication.sharedApplication.networkActivityIndicatorVisible = YES;
联网指示器.png
打电话  [UIApplication.sharedApplication openURL:[NSURLURLWithString:@"tel://110"]];
发短信  [UIApplication.sharedApplication openURL:[NSURLURLWithString:@"sms://10086"]];
发邮件  [UIApplication.sharedApplication openURL:[NSURLURLWithString:@"mailto://xxcc@fox.com"]];
打开一个网页资源 [UIApplication.sharedApplication openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
打开其他app程序   openURL方法,可以打开其他APP。

UIApplicationDelegate

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end
// 应用程序启动完成的时候就会调用AppDelegate的方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 这里注意不要有过多代码,注意封装,保持简洁
    return YES;
}
// 当应用程序失去焦点的时候调用,比如按了home键
- (void)applicationWillResignActive:(UIApplication *)application {
}
// 当应用程序完全获取焦点的时候调用
// 只有当应用程序完全获取焦点的时候,才能够与用户交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
// 分享等处理URL跳转,这个是iOS9以后的版本
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
    // 从分享界面回来的处理;或者自己给其他APP调用
}

// 消息推送相关内容
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // deviceToken是APNS分配的唯一标识符
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    // APNS 注册失败,一般不需要做什么
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    // 收到推送消息
}

UIWindow

// 创建window,没有window,屏幕没法显示,所以一般就用屏幕大小
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 如果不设置背景色,或者[UIColor clearColor],将显示黑屏
self.window.backgroundColor = [UIColor whiteColor]; // 默认是[UIColor clearColor],将显示黑屏;根据需要设置
// 设置rootViewController,view在Controller中,会被自动加到window上,这样才能显示
UIViewController *rootViewController = [[UIViewController alloc] init]; // 可以是UIViewController的子类
// rootViewController.backgroundColor = [UIColor redColor]; // view 的颜色会覆盖window的颜色,根据需要设置
self.window.rootViewController = rootViewController;
// keyWindow只有一个,window显示,里面的内容才能显示,这句话是必要的
[self.window makeKeyAndVisible];
@property(nullable, nonatomic,readonly) UIWindow *keyWindow;
@property(nonatomic,readonly) NSArray<__kindof UIWindow *>  *windows;

iOS开发UI篇—UIWindow简单介绍

上一篇下一篇

猜你喜欢

热点阅读