iPhone应用程序生命周期中的最后一个功能是什么 [英] What is the last function in iPhone application lifecycle

查看:103
本文介绍了iPhone应用程序生命周期中的最后一个功能是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在关闭我的应用程序之前,我必须从Web服务注销用户。我找不到应用程序死之前调用的最后一个函数?

Before my application is going to be closed I have to logout user from web service. 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];   
}


推荐答案

你有两个地方'我需要触发您的注销代码,这两个代码都在 UIApplicationDelegate Protocol Reference 文档。

There are two places you'll need to trigger your logout code from, both of which are detailed in the UIApplicationDelegate Protocol Reference documentation.

适用于iOS 4之前的设备(并涵盖其他情况)你应该使用:

For pre-iOS 4 devices (and to cover other circumstances) you should use:

- (void)applicationWillTerminate:(UIApplication *)application

正如Apple所说:


对于不支持
后台执行或与iOS 3.x或更早版本链接
的应用程序,当用户
退出应用程序时,始终会调用此
方法。对于支持后台
执行的
应用程序,当用户退出
应用程序时,此方法通常是
,因为应用程序
只是移动到$
$ b案。但是,在
背景中运行
应用程序(未暂停)并且
系统需要以
终止它时,此方法可能是
。原因。

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. 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

...在iOS 4+设备上,(再次来自Apple文档):

...on iOS 4+ devices, as (once again from the Apple docs):


在iOS 4.0及更高版本中,此方法是当
用户退出
支持后台执行的应用程序时,
调用而不是
applicationWillTerminate:方法

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

尽管如此,无论上述情况如何,当您的应用程序处于后台运行时,您很可能希望退出Web服务,并在唤醒时重新登录。有关更多详细信息,请参阅上面提到的 applicationDidEnterBackground:方法和 applicationWillEnterForeground:方法文档。

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. See the above mentioned applicationDidEnterBackground: method and the applicationWillEnterForeground: method documentation for more details.

这篇关于iPhone应用程序生命周期中的最后一个功能是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆