简体   繁体   English

iPhone应用程序生命周期中的最后一个功能是什么

[英]What is the last function in iPhone application lifecycle

Before my application is going to be closed I have to logout user from web service. 在关闭我的应用程序之前,我必须从Web服务注销用户。 And I can't find the very last function that is invoked before application die? 我找不到在应用程序终止之前调用的最后一个函数吗?

-(void)LogoutUser
{    
    int userId = [[GlobalData sharedMySingleton] getUserId];

    NSString *soapMsg = 
    [NSString stringWithFormat:
     @"<?xml version=\"1.0\" encoding=\"utf-8\"?>...", userId
     ];

    NSURL *url = [NSURL URLWithString: @"http://....asmx"];     

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];    
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];

    [req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];   
    [req addValue:@"http://..." forHTTPHeaderField:@"SOAPAction"];  
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];   
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    if (conn) 
    {
        webData = [[NSMutableData data] retain];
    }     

}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response 
{
    [webData setLength: 0];
}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{
    [webData appendData:data];  
}

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{   
    [webData release];    
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{   
    NSString *theXML = [[NSString alloc] 
                        initWithBytes: [webData mutableBytes] 
                        length:[webData length] 
                        encoding:NSUTF8StringEncoding];    


    [theXML release];    

    [connection release];
    [webData release];   
}

There are two places you'll need to trigger your logout code from, both of which are detailed in the UIApplicationDelegate Protocol Reference documentation. 您需要从两个地方触发注销代码,这两个地方在UIApplicationDelegate协议参考文档中都有详细说明。

For pre-iOS 4 devices (and to cover other circumstances) you should use: 对于iOS 4之前的设备(并涵盖其他情况),您应该使用:

- (void)applicationWillTerminate:(UIApplication *)application

As Apple puts it: 正如苹果所说:

For applications that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the application. 对于不支持后台执行或与iOS 3.x或更早版本链接的应用程序,总是在用户退出应用程序时调用此方法。 For applications that support background execution, this method is generally not called when the user quits the application because the application simply moves to the background in that case. 对于支持后台执行的应用程序,当用户退出应用程序时通常不会调用此方法,因为在这种情况下,应用程序只是移至后台。 However, this method may be called in situations where the application is running in the background (not suspended) and the system needs to terminate it for some reason. 但是,在应用程序在后台运行(未挂起)并且系统出于某种原因需要终止它的情况下,可以调用此方法。

However, you'll need to use... 但是,您需要使用...

- (void)applicationDidEnterBackground:(UIApplication *)application

...on iOS 4+ devices, as (once again from the Apple docs): ...在iOS 4或更高版本的设备上,如(再次来自Apple文档):

In iOS 4.0 and later, this method is called instead of the applicationWillTerminate: method when the user quits an application that supports background execution 在iOS 4.0及更高版本中,当用户退出支持后台执行的应用程序时,将调用此方法而不是applicationWillTerminate:方法

That said, irrespective of all the above, you'll most likely want to logout of the web service when your app is backgrounded and log back in when it's "woken up" as well. 就是说,无论以上哪种情况,您都极有可能希望在应用程序后台运行时注销Web服务,并在其“唤醒”时重新登录。 See the above mentioned applicationDidEnterBackground: method and the applicationWillEnterForeground: method documentation for more details. 有关更多详细信息,请参见上述applicationDidEnterBackground:方法和applicationWillEnterForeground:方法文档。

- (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, called instead of applicationWillTerminate: when the user quits.
 */
}

this may be not a last function but. 这可能不是最后一个功能,但是。 you can do logout here. 您可以在此处注销。

For typical apps under iOS 4.x, applicationWillResignActive and perhaps applicationDidEnterBackground will be called both before your app is terminated (at some unknown time in the future), and at other times as well when the app isn't being terminated. 对于iOS 4.x下的典型应用程序,将在您的应用程序终止之前(在将来的某个未知时间)以及在应用程序未终止的其他时间调用applicationWillResignActive以及也许applicationDidEnterBackground。 However it might be a good idea to log out here, as your app may never get any further CPU run time. 但是,最好在此处注销,因为您的应用可能永远无法获得更多的CPU运行时间。

If you have pending network activity, such as trying to logout, you might want to use the multitasking call beginBackgroundTaskWithExpirationHandler: to request a bit of additional time in the background to finish the log out process, such as handshaking with any network callbacks required. 如果您有未决的网络活动(例如尝试注销),则可能要使用多任务调用beginBackgroundTaskWithExpirationHandler:在后台请求额外的时间来完成注销过程,例如与所需的任何网络回调进行握手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM