在 iOS 中 - 生产和登台服务器包含在单个构建中

In iOS - Production and staging server included in single build

我有一个应用程序有两个服务器 STAGING 和 PRODUCTION。我过去常常通过更改编码部分的服务器来发布两个版本。但是现在我的客户要求提供一个单一的版本,我们可以在应用程序设置或 phone 设置中提供一个选项来更改 URL.

我在堆栈溢出中进行了很多研究,并了解到在运行时,当我们在提供构建时 select debug/release 模式时,它是可能的,但无论如何它也会受到影响给出两个构建的过程。

我想要一个单一的版本,用户可以在其中更改 Staging/Producetion 的选项。请帮我。可能吗。 ?

您只需在应用程序的某处添加一个开关即可打开暂存模式(关闭 = 生产)。此开关的状态保存在 NSUserDefaults 中。然后,根据此状态,您在代码中选择服务器的正确 URL。

你可以这样做-

1.In 登录页面取决于您用于进入暂存或生产的字段 url ,打开一个 UIAction sheet 当字段(在我的情况下我是使用 tableview 单元格,您可以使用文本字段或按钮) 单击然后显示如下所示的字段 -

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 2) {
    [self showActionSheet];

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}

在 didselectrow 中调用上面的操作 sheet,这将打开下面的操作 sheet

- (void)showActionSheet
{
NSString *actionSheetTitle = @"Choose Connection";
NSString *other1 = @"development";
NSString *other2 = @"staging";
NSString *other3 = @"production";
NSString *cancelTitle = @"Cancel";

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:actionSheetTitle
                                                         delegate:self
                                                cancelButtonTitle:cancelTitle
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:other1, other2, other3, nil];

[actionSheet showInView:self.view];
}


 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Get the name of the current pressed button
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];

if ([buttonTitle isEqualToString:@"development"]) {

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:nil forKey:@"UserId"];

    NSLog(@"Destructive pressed --> Delete Something");
}
if ([buttonTitle isEqualToString:@"staging"]) {
    NSLog(@"Other 1 pressed");
}
if ([buttonTitle isEqualToString:@"production"]) {
    NSLog(@"Other 2 pressed");
}

}

最后在点击按钮索引中使用 NSUserDefaults 像上面那样保存用户状态。