appdelegate檔案中可能會用到的方法

來源:互聯網
上載者:User

標籤:des   http   os   io   檔案   for   art   ar   

- (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 = [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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.