iOS AppDelegate 代理詳解(啟動,開啟App,推送,通知)
阿新 • • 發佈:2018-11-10
//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{
}