gpt4 book ai didi

ios - 向用户 iOS 显示错误信息的最佳方法

转载 作者:行者123 更新时间:2023-11-29 01:10:47 26 4
gpt4 key购买 nike

对于资深和有经验的开发人员来说,这可能是一个显而易见的问题,所以我真的不敢问。

假设我有一个移动应用程序,它为不同的数据模型调用了太多的 RESTful API。假设有可以在这些模型上增删改查的 ViewController A、B、C。现在有几件事情可能会出错。每个调用都有一个失败 block 。我正在使用 Strongloop/Loopback ios sdk。

像这样默默忽略错误的策略:

[repo logoutWithSuccess:^(){
// ... success code
} failure:CALLBACK_FAILURE_BLOCK];

CALLBACK_FAILURE_BLOCK 定义为

#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

#define CALLBACK_FAILURE_BLOCK \
^(NSError *error) { \
ALog("Callback failed: %@", error.description); \
}

在每个客户端验证都能完美运行并且可以把头埋在沙子里的理想世界中会很棒。

目前,我在每个 ViewController 中定义了一个辅助函数,当这些调用失败时,它会向用户显示警报。

[self.trip saveWithSuccess:^(){
//... success code
} failure:^(NSError *error){
[self displayErrorWithMessage:[error localizedDescription]
andTitle:NSLocalizedString(@"Service Error", @"Server Error")];
}];

- (void)displayErrorWithMessage:(NSString*)msg andTitle:(NSString*)title{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title
message:msg
preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}

我正在考虑将这个辅助函数重构到一个常见的地方,例如 AppDelegate(可能在协议(protocol)中)。

这是一个正确的策略吗?对于如何更好地设计它,您还有其他最佳实践吗?

最佳答案

最近我在我的一个应用程序中使用了这种方法,我希望它能很好地工作并且易于处理错误。

为此,我创建了 UIViewController 的 ErrorViewController 子类(在我的案例中它是 VCBaseViewController,它是所有其他 View Controller 的父类,它是 UIViewController 的子类)。您可以使用 Storyboard或 xib,由您决定。我在整个应用程序中使用了 Storyboard。

我的 ErrorViewController.h

@interface ErrorViewController : VCBaseViewController

@property (nonatomic, strong) NSString *errorTitle;
@property (nonatomic, strong) NSString *errorDescription;

@property(nonatomic,assign) ErrorPageType errorPage;

@property (weak, nonatomic) IBOutlet UILabel *lblErrorText;
@property (weak, nonatomic) IBOutlet UITextView *textViewErrorString;
@property (weak, nonatomic) IBOutlet UIButton *btnAction;

@end

ErrorViewController.m

@interface ErrorViewController ()

@end

@implementation ErrorViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self configureNavigationBarWithStyle:NavigationStyleBackButton];
[self setViewControllerTitle:@"Error View"];
}

- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];

switch (self.errorPage)
{
case ErrorTypeNetworkError:
{

}
break;

case ErrorTypeTechnicalError:
{
self.lblErrorText.text = @"Technical Error";
self.textViewErrorString.text = @"Due to some technical error\n page not exist";
}
break;

default:
{
self.lblErrorText.text = self.errorTitle;
self.textViewErrorString.text = self.errorDescription;
}
break;
}
}

- (IBAction)btnErrorAction:(id)sender{
[self errorPageAction];
}

- (void)backButtonPressed{
[self errorPageAction];
}

- (void)errorPageAction
{
switch (self.errorPage)
{
case ErrorTypeNetworkError:
{

}
break;
case ErrorTypeProductNotAvailable:
{
[self popToPreviousController];
}break;
default:
{
[self popToPreviousController];
}break;
}
}

- (void)popToPreviousController
{
NSArray* viewcontrollers = [self.navigationController viewControllers];
if(viewcontrollers.count>2)
{
int index = (int)viewcontrollers.count - 3;
[self.navigationController popToViewController:[viewcontrollers objectAtIndex:index] animated:YES];
}else{
[self.navigationController popToRootViewControllerAnimated:YES];
}
}

我的 Storyboard设计看起来像这样,其中包含错误图像错误标签以及 TextView 和后退按钮,因为我使用的是导航 Controller 。[![在此处输入图片描述][1]][1]

我的BaseViewController.h

typedef enum {
ErrorTypeNetworkError = 0,
ErrorTypeTechnicalError,
ErrorTypeNone
} ErrorPageType;

- (void)pushErrorViewcontroller:(NSError*)error;
- (void)pushErrorViewcontroller:(NSString*)errorTitle andErrorPage:(ErrorPageType)errorType;
- (void)pushErrorViewcontroller:(NSString *)errorTitle errorDescription:(NSString *)errorDescription errorType:(ErrorPageType)errorType;

BaseViewController.m

- (void)pushErrorViewcontroller:(NSString*)errorTitle andErrorPage:(ErrorPageType)errorType{
[self pushErrorViewcontroller:nil errorDescription:nil errorType:errorType];
}

- (void)pushErrorViewcontroller:(NSError*)error{
[self pushErrorViewcontroller:nil errorDescription:[error localizedDescription] errorType:ErrorTypeNone];
}

- (void)pushErrorViewcontroller:(NSString *)errorTitle errorDescription:(NSString *)errorDescription errorType:(ErrorPageType)errorType{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
ErrorViewController *errorViewController = [storyBoard instantiateViewControllerWithIdentifier:NSStringFromClass([ErrorViewController class])];
errorViewController.errorPage = errorType;
errorViewController.errorDescription = errorDescription;
errorViewController.errorTitle = errorTitle;
[self.navigationController pushViewController:errorViewController animated:YES];
}

我正在像这样在其他 View Controller 中调用 errorView Controller 方法。

- (void) ServiceCallForOrderHistoryWithCurrentPageMark: (NSString *)page
{
__block __weak VCMyOrderViewController* blockMyOrderListView = self;

[[ModelManager sharedInstance] getMyOrderDetail:[[Configuration sharedConfig] getLoggedInUserId] andPageNumber:page completionBlock:^(id result, NSError *error)
{
if (!error)
{

blockMyOrderListView.objOrderHistoryBaseClass = (OrderHistoryBaseClass *)result;

if (blockMyOrderListView.objOrderHistoryBaseClass.rESPONSE.totalOrderCount.intValue == 0)
{
[blockMyOrderListView.tblMyOrder setHidden:YES];
[blockMyOrderListView.viewNoOrders setHidden:NO];
}
else
{
[blockMyOrderListView setDataSourceMyOrders:blockMyOrderListView.objOrderHistoryBaseClass.rESPONSE];
[blockMyOrderListView.tblMyOrder reloadData];
}
}
else
{
[blockMyOrderListView pushErrorViewcontroller:error];
}
}];
}

关于ios - 向用户 iOS 显示错误信息的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35774542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com