首页 > 代码库 > appdelegate文件中可能会用到的方法

appdelegate文件中可能会用到的方法

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

{

    // Override point for customization after application launch.

    //TODO:connect to socket.io server

    //load server configuration

    //load advertisement

    //Enabling keyboard manager

    return YES;

}

 

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

    NSLog(@"deviceToken: %@", deviceToken);

    

    NSString *strDev = [[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]stringByReplacingOccurrencesOfString: @">" withString: @""] stringByReplacingOccurrencesOfString: @" " withString: @""];

    

    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];

    [defs setValue:strDev forKey:@"deviceToken"];

    NSLog(@"%@",strDev);

    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppUploadDeviceToken" object:nil];

}

 

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

    NSLog(@"Error in registration. Error: %@", error);

}

 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

    

    NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);

//    if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {

//        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"

//                                                        message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]

//                                                       delegate:self

//                                              cancelButtonTitle:@" 关闭"

//                                              otherButtonTitles:@" 更新状态",nil];

//        [alert show];

//    }

}

 

- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

 

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppDisconnectChatServer" object:nil];

    

    NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];

//    [pref setValue:@"" forKey:@"pwd"];

    

    //服务器端推出登录;

    NSString *strToken = [pref stringForKey:@"logintoken"];

    NSString *strUrl = [NSString stringWithFormat:@"%@/appLogout?token=%@",WEB_SERVER_HOST,strToken];

    

    NSURL *url = [NSURL URLWithString:strUrl];

    __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    [request setCompletionBlock:^{

        NSString *responseString = [request responseString];

        NSLog(@"退出登录 %@",responseString);

        NSError *error = nil;

        NSData *jsonData = http://www.mamicode.com/[responseString dataUsingEncoding:NSUTF8StringEncoding];

        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];

        NSString *msgCode = [dict objectForKey:@"code"];

        if ([msgCode isEqualToString:@"200"]) {

            NSLog(@"成功退出!");

            //            UIAlertView *alert =[ [UIAlertView alloc] initWithTitle:nil message:@"成功退出" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

            //            [alert show];

        }

        

    }];

    [request setFailedBlock:^{

        NSError *error = [request error];

        NSLog(@"退出登录失败 error is %@",error.description);

    }];

    [request startAsynchronous];

}

 

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

//    NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];

//    NSString* strUid = [pref stringForKey:@"uid"];

//    NSString* strPwd = [pref stringForKey:@"pwd"];

//    

//    NSString* strlogin = [NSString stringWithFormat:@"{\"type\":\"login\",\"uid\":\"%@\",\"pwd\":\"%@\"}",strUid,strPwd];

//    

//    

//    

//    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppLogin" object:strlogin];

 

}

 

- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    

    NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];

    NSString* strUid = [pref stringForKey:@"uid"];

    NSString* strPwd = [pref stringForKey:@"pwd"];

    

    NSString* strlogin = [NSString stringWithFormat:@"{\"type\":\"login\",\"uid\":\"%@\",\"pwd\":\"%@\"}",strUid,strPwd];

   

 

    

    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppLogin" object:strlogin];

    

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

}

 

- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

 

@end