实用小技巧(十):晃动手机换后台服务器网络

版本记录

版本号 时间
V1.0 2017.06.28

前言

在app中,我们很多时候需要在内网和外网之间进行切换,也需要在外部测试服务器和外部生产服务器之间进行切换。这一篇,就仿照实现头条的模糊效果。感兴趣的可以看看我写的其他小技巧。
1. 实用小技巧(一):UIScrollView中上下左右滚动方向的判断

2. 实用小技巧(二):屏幕横竖屏的判断和相关逻辑
3.实用小技巧(三):点击手势屏蔽子视图的响应
4.实用小技巧(四):动态的增删标签视图
5.实用小技巧(五):通过相册或者相机更改图标
6.实用小技巧(六):打印ios里所有字体
7. 实用小技巧(七):UITableViewCell自适应行高的计算
8. 实用小技巧(八):数字余额显示的分隔
9.实用小技巧(九):类头条模糊背景的实现

实现目标

在app中,我们很多时候需要在内网和外网之间进行切换,也需要在外部测试服务器和外部生产服务器之间进行切换。这里我们就说一下晃动手机切换服务器的方法。

实现过程

下面我们就直接看代码即可。

//在登录界面
1.JJLoginVC.h

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{    
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"切换服务器" message:[JJAppInfo shareAppInfo].currentServerTypeString preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * reBoServerAction = [UIAlertAction actionWithTitle:@"正式服务器" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        DDLogVerbose(@"正式");
        [userDefault setObject:@0 forKey:kServerType];
        [userDefault synchronize];
        exit(0);
    }];
    
    UIAlertAction * testServerAction = [UIAlertAction actionWithTitle:@"测试服务器" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        DDLogVerbose(@"测试");
        [userDefault setObject:@1 forKey:kServerType];
        [userDefault synchronize];
        exit(0);
    }];
    
    UIAlertAction * onlineTestServerAction = [UIAlertAction actionWithTitle:@"线上测试服务器" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        DDLogVerbose(@"线上测试");
        [userDefault setObject:@2 forKey:kServerType];
        [userDefault synchronize];
        exit(0);
    }];
    
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertController addAction:reBoServerAction];
    [alertController addAction:testServerAction];
    [alertController addAction:onlineTestServerAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

//这个类存储app的基本信息
2. JJAppInfo.h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, NSSeverUrlType) {
    NSSeverUrlTypeOnline,
    NSSeverUrlTypeOnlineTest,
    NSSeverUrlTypeTest,
};

@interface JJAppInfo : NSObject

#pragma mark - serverl

@property (nonatomic, strong) NSString *serverUrl;
@property (nonatomic, strong) NSString *payUrl;
@property (nonatomic, strong) NSString *chatUrl;

- (void)switchServerUrl:(NSSeverUrlType)serverType;

- (NSString *)currentServerTypeString;

//单例
+ (instancetype)shareAppInfo;

+ (BOOL) isLogin;

+ (void) clearTmpCache;

@end

3. JJAppInfo.m

#import "JJAppInfo.h"

@implementation JJAppInfo

#pragma mark - 单例

+ (instancetype)shareAppInfo
{
    static JJAppInfo *appInfo;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        appInfo = [[JJAppInfo alloc] init];
    });
    return appInfo;
}

+ (BOOL) isLogin 
{
    if ([JJConfig myProfile].token == nil) {
        return NO;
    } else {
        return YES;
    }
}

#pragma mark - ServerUrl

- (NSString *)serverUrl 
{
    if (!_serverUrl) {
        _serverUrl = kOnlineserverUrl;
    }
    return _serverUrl;
}

- (NSString *)payUrl 
{
    if (!_payUrl) {
        _payUrl = kOnlinePayUrl;
    }
    return _payUrl;
}

- (NSString *)chatUrl 
{
    if (!_chatUrl) {
        _chatUrl = kOnlineChatUrl;
    }
    return _chatUrl;
}

- (void)switchServerUrl:(NSSeverUrlType)serverType 
{
    //选择服务器
    
    switch (serverType) {
        case 0:
            //正式服务器
            _serverUrl = kOnlineserverUrl;
            _payUrl = kOnlinePayUrl;
            _chatUrl = kOnlineChatUrl;
            break;
        case 1:
            //测试服务器
            _serverUrl = ktestserverUrl;
            _payUrl = ktestPayUrl;
            _chatUrl = ktestChatUrl;
            break;
        case 2:
            //预发布服务器
            _serverUrl = kOnlineTestserverUrl;
            _payUrl = kOnlineTestPayUrl;
            _chatUrl = kOnlineTestChatUrl;
            break;
        default:
            _serverUrl = kOnlineserverUrl;
            _payUrl = kOnlinePayUrl;
            _chatUrl = kOnlineChatUrl;
            break;
    }
    
}

- (NSString *)currentServerTypeString 
{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSSeverUrlType serverType = [[userDefault objectForKey:kServerType] integerValue];
    NSString * serverStr;
    switch (serverType) {
        case NSSeverUrlTypeOnline:
            serverStr = @"正式服务器";
            break;
        case NSSeverUrlTypeTest:
            serverStr = @"测试服务器";
            break;
        case NSSeverUrlTypeOnlineTest:
            serverStr = @"线上测试服务器";
            break;
        default:
            serverStr = @"正式服务器";
            break;
    }
    return [NSString stringWithFormat:@"当前是: %@",serverStr];
}

+ (void) clearTmpCache 
{
    NSString *libPath = NSTemporaryDirectory();
    // 实例化NSFileManager
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 获取Caches文件夹下的所有文件及文件夹
    NSArray *array = [fileManager contentsOfDirectoryAtPath:libPath error:nil];
    // 循环删除Caches下的所有文件及文件夹
    for (NSString *fileName in array) {
        [fileManager removeItemAtPath:[libPath stringByAppendingPathComponent:fileName] error:nil];
    }  
}

@end

//在app的launch方法里面
4. AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    NSInteger serverType = [[userDefault objectForKey:kServerType] integerValue];
#ifdef DEBUG
    [[JJAppInfo shareAppInfo] switchServerUrl:serverType];
#else
    [[JJAppInfo shareAppInfo] switchServerUrl:NSSeverUrlTypeOnline];
#endif
    DDLogWarn(@"服务器类型: %ld",(long)serverType);

    //other code written here...
}

实现结果

下面我们看一下实现结果。

实现结果

可见,可以实现切换服务的功能,同时在网络工具里面封装好根地址,方便调试。

后记

未完,待续~~~~

冬之美
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容