在我的应用程序关闭之前,我必须从网络服务中注销用户。我找不到应用程序死亡之前调用的最后一个函数?
-(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 协议参考 http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/applicationWillTerminate%3a文档。
对于 iOS 4 之前的设备(并涵盖其他情况),您应该使用:
- (void)applicationWillTerminate:(UIApplication *)application
正如苹果公司所说:
对于不支持的应用程序
后台执行或链接
针对 iOS 3.x 或更早版本,此
方法总是在用户调用时被调用
退出应用程序。为了
支持后台的应用程序
执行,这个方法一般是
当用户退出时不会调用
应用程序 因为应用程序
只是移动到后台
案件。然而,这种方法可能是
在以下情况下调用
应用程序正在运行
背景(未暂停)和
系统需要终止它
原因。
但是,您需要使用...
- (void)applicationDidEnterBackground:(UIApplication *)application
...在 iOS 4+ 设备上,如(再次来自 Apple 文档):
在iOS 4.0及更高版本中,该方法是
调用而不是
applicationWillTerminate:方法时
用户退出应用程序
支持后台执行
也就是说,无论上述情况如何,您很可能希望在应用程序进入后台时注销 Web 服务,并在应用程序“唤醒”时重新登录。参见上面提到的applicationDidEnterBackground:
方法和applicationWillEnterForeground:
方法文档以获取更多详细信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)