如果无法下载数据库,请退出 iOS 应用程序

quit iOS application if not able to download database

如果无法从服务器下载数据库,我想退出应用程序。我尝试使用 NSURLSession 下载数据库。如果没有,则显示警报视图。

当用户单击警报中的确定按钮时,我想退出该应用程序。由于没有数据库,它将崩溃以继续。为了避免崩溃,我想以编程方式退出应用程序。

我想实现类似 android 的 finish()。

我有一个 util.h 和 util.m 可以完成所有下载显示警告框的工作。它扩展了 NSObject。它就像一个实用程序而不是控制器。

Util.h

#import <Foundation/Foundation.h>


@interface Util : NSObject {


NSURLSession *session;
//NSDictionary *plistDictionary;
NSURLSessionTask *task1;
NSURLSessionTask *task2;
NSURLSessionTask *task3;
UIAlertView *alertViewSpin;
NSMutableData *receivedData;

}

我在SO中看到过post..

 [self dismissViewControllerAnimated:YES completion:nil];
 [self.navigationController popViewControllerAnimated: YES];

但没有帮助。 我该怎么做?

已在post中回答:- Proper way to exit iPhone application?

使用前请阅读本指南:-

Never quit an iOS app programmatically. People tend to interpret this as a crash. If something prevents your app from functioning as intended, you need to tell users about the situation and explain what they can do about it. APPLE Guideline

对于您的情况,请自行承担风险:-

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

   if (buttonIndex != 0)  // 0 == the cancel button
   {

      exit(0); // Close the app
  }
}

How do I programmatically quit my iOS application?

A: There is no API provided for gracefully terminating an iOS application.

In iOS, the user presses the Home button to close applications. Should your application have conditions in which it cannot provide its intended function, the recommended approach is to display an alert for the user that indicates the nature of the problem and possible actions the user could take — turning on WiFi, enabling Location Services, etc. Allow the user to terminate the application at their own discretion.

Warning: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen.

请注意,使用 exit(0)[[NSThread mainThread] exit] 可能会导致您的应用在应用商店提交中被拒绝

More information

正如其他人所说,不可能也不建议退出该应用程序。

或者,呈现一个模态视图控制器如何,它告诉用户发生了什么?

我见过优步做过一次,告诉我如果我不升级,我将无法再使用这个应用程序。

IMO, 仅仅因为下载失败就杀死应用程序是最糟糕的主意...

我的建议是,如果下载失败,提醒用户要使用该应用程序,必须从服务器下载内容。并显示、重试和关闭应用程序按钮。当用户重试时,尝试再次下载。因此,通过这种方式,您可以在不终止应用程序的情况下控制您想要的内容。只有下载完成后才会显示内容

如果您需要更多详细信息,请告诉我。