iOS AppDelegate 代理详解(启动,打开App,推送,通知)

App将要启动
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions{

    return YES;
}

//App已经启动

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //若由其他应用程序通过openURL:启动
    NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
    if(url){

    }
    //启动的源应用程序的 bundle ID
    NSString *bundleId = [launchOptions objectForKey:UIApplicationLaunchOptionsSourceApplicationKey];
    if(bundleId){

    }

    //若由本地通知启动
    UILocalNotification * localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if(localNotification){


    }

    //若由远程通知启动
    NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(remoteNotification){


    }

    return YES;
}

//App将要进入前台

- (void)applicationWillResignActive:(UIApplication *)application {

}

//App已经进入前台
- (void)applicationDidBecomeActive:(UIApplication *)application {


}

//App将要进入后台

- (void)applicationWillEnterForeground:(UIApplication *)application {

}

//App已经进入后台

- (void)applicationDidEnterBackground:(UIApplication *)application {

}

//App将要退出

- (void)applicationWillTerminate:(UIApplication *)application {


}

//App内存警告

-  (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    NSLog(@"系统内存不足");
}

/*

这个方法是其它应用通过url唤醒你的应用时调用的

另外,需要在plist中添加自己的url协议
第一步:找到Info中的 “URL types”
第二步:增加一个URL identifier,建议用反域名(com.jingjin.myApp),URL Schemes(myApp),URL Schemes就是唤醒自己应用的URL的开头。

*/

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{

    if (!url) {//如果时没有链接

        return NO;

    }

    NSLog(@"handleOpenURL: %@", [url absoluteString]);

    //判断能否打开你的应用
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"myapp://"]]) {

        //做处理

        return YES;

    } else {

        return NO;

    }

    return YES;
}
StatusBar框方向将要变化
-  (void)application:(UIApplication*)application willChangeStatusBarOrientation:
(UIInterfaceOrientation)newStatusBarOrientation duration:(NSTimeInterval)duration
{

}
StatusBar框方向已经变化
-  (void)application:(UIApplication*)application  didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation{
}

//StatusBar框坐标将要变化

-  (void)application:(UIApplication*)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame{


}

//StatusBar框坐标已经变化

-  (void)application:(UIApplication*)application didChangeSetStatusBarFrame:(CGRect)oldStatusBarFrame{
}

//当系统时间发生改变时执行

- (void)applicationSignificantTimeChange:(UIApplication *)application{

}

//已经注册远远程通知

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{

}

//当应用程序成功的注册一个推送服务

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *) deviceToken{

    NSString *tokenStr = [NSString stringWithFormat:@"%@",deviceToken];
    //将其中的<>去掉
    tokenStr = [tokenStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    //将其中的空格去掉
    tokenStr = [tokenStr stringByReplacingOccurrencesOfString:@" " withString:@""];
}

//当 APS无法成功的完成向 程序进程推送时

-(void) application:(UIApplication *) application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error{

}

//当一个运行着的应用程序收到一个本地的通知

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{



}

//程序运收远程通知

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{

    if (application.applicationState == UIApplicationStateActive) {//程序当前正处于前台


    }else if(application.applicationState == UIApplicationStateInactive){//程序处于后台


    }

    AudioServicesPlaySystemSound(1007);//系统的通知声音
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);//震动

    //自定义声音
    NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"];
    //组装并播放音效
    SystemSoundID soundID;
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
    AudioServicesPlaySystemSound(soundID);
    //声音停止
    AudioServicesDisposeSystemSoundID(soundID);

    /*
    关于userInfo的结构,参照苹果的官方结构:
    {
        "aps" : {
            "alert" : "",
            "badge" : 10,//推送信息
            "sound" : ""//app的icon右上角的数字
        },
        "acme1" : "bar",
        "acme2" : 42//两个参数
    }
     */

}

//

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler{


}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler{


}
处理本地通知
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler{


}

你可能感兴趣的:(iOS AppDelegate 代理详解(启动,打开App,推送,通知))