单军华
2018-03-29 89d748b77b478905732e60f0b4c5807c274b6565
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//
//  AppDelegate.m
//  BaseProject
//
//  Created by WindShan on 2016/12/5.
//  Copyright © 2016年 WindShan. All rights reserved.
//
 
#import "AppDelegate.h"
 
// test page
#import "NetworkTestPage.h"
#import "MJExampleViewController.h"
#import "SegmentedPage.h"
#import "CommonReqModel.h"
#import "NetworkSingleton.h"
#import "UserModel.h"
#import "LoginPage.h"
#import "HomePage.h"
#import "SegmentedPage.h"
 
#import "CBMoralManager.h"
#import "SettingPage.h"
#import "PhotosPage.h"
#import "MinePage.h"
#import "DITableViewController.h"
#import "RadiaDetectionPage.h"
#import "HistoryRecordPage.h"
 
@interface AppDelegate ()
{
    NSMutableArray *notifyingCharacteristicsArray;
}
 
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
 
 
@end
 
@implementation AppDelegate
 
 
 
+ (AppDelegate *)AppInstance;
{
    return  (AppDelegate *)[UIApplication sharedApplication].delegate;
}
 
- (void)showHomePage
{
    
    HomePage *page = [[HomePage alloc] init];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = page;
    
    [self.window makeKeyAndVisible];
}
 
- (void)showHomePage2
{
    // 单页
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    HistoryRecordPage *root = [[HistoryRecordPage alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:root];//先将root添加在navigation上
    nav.navigationBarHidden = YES;
    [self.window setRootViewController:nav];//navigation加在window上
    
    [self.window makeKeyAndVisible];
}
 
- (void)showLoginPage
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    LoginPage *root = [[LoginPage alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:root];//先将root添加在navigation上
    nav.navigationBarHidden = YES;
    [self.window setRootViewController:nav];//navigation加在window上
    [self.window makeKeyAndVisible];
}
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    /*
     Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the method reachabilityChanged will be called.
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    
    //Change the host name here to change the server you want to monitor.
 
    // 要使用百度地图,请先启动BaiduMapManager
    self.mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [self.mapManager start:@"ZiqQxvKGjZVNbITouYn7dIwDjb1CbKcy" generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    
    // 添加网络监听
    self.hostReachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    [self.hostReachability startNotifier];
    [self updateInterfaceWithReachability:self.hostReachability];
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];
    [self updateInterfaceWithReachability:self.internetReachability];
    
    // Override point for customization after application launch.
    UIUserNotificationType types = UIUserNotificationTypeBadge |
    UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    
    UIUserNotificationSettings *mySettings =
    [UIUserNotificationSettings settingsForTypes:types categories:nil];
    
    if(mySettings != nil)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    }
    
    //_currentDevice = _selectedDevice.mac;
    
 
    [self showLoginPage];
    
    //异常对象:NSException
    //参数:将函数的地址给它
    NSSetUncaughtExceptionHandler(&getException);
    
    // Override point for customization after application launch.
    return YES;
}
 
/*!
 * Called by Reachability whenever status changes.
 */
- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
}
 
 
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
    if (reachability == self.hostReachability)
    {
        NetworkStatus netStatus = [reachability currentReachabilityStatus];
        BOOL connectionRequired = [reachability connectionRequired];
        
        NSString* baseLabelText = @"";
        
        if (connectionRequired)
        {
            baseLabelText = NSLocalizedString(@"Cellular data network is available.\nInternet traffic will be routed through it after a connection is established.", @"Reachability text if a connection is required");
        }
        else
        {
            baseLabelText = NSLocalizedString(@"Cellular data network is active.\nInternet traffic will be routed through it.", @"Reachability text if a connection is not required");
        }
        
        switch (netStatus)
        {
            case NotReachable:
                
                break;
                
            default:
                break;
        }
        BASE_INFO_FUN(baseLabelText);
    }
    
}
 
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
 
 
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
    if (!notifyingCharacteristicsArray)
    {
        notifyingCharacteristicsArray = [NSMutableArray array];
    }
    
    /* Stop notification of characteristics while enter in background */
    for (CBService *service in [[CBMoralManager sharedManager] foundServices])
    {
        for (CBCharacteristic *characteristic in service.characteristics)
        {
            if (characteristic.isNotifying  && ![characteristic.UUID isEqual:BOOT_LOADER_CHARACTERISTIC_UUID])
            {
                [[[CBMoralManager sharedManager] myPeripheral] setNotifyValue:NO forCharacteristic:characteristic];
                [notifyingCharacteristicsArray addObject:characteristic];
            }
        }
        
    }
}
 
 
- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    
    /* Enable notification of characteristics while enter in foreground */
    
    if (notifyingCharacteristicsArray)
    {
        for (CBCharacteristic *characteristic in notifyingCharacteristicsArray)
        {
            if (![characteristic.UUID isEqual:BOOT_LOADER_CHARACTERISTIC_UUID])
            {
                [[[CBMoralManager sharedManager] myPeripheral] setNotifyValue:YES forCharacteristic:characteristic];
            }
        }
    }
}
 
 
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    NSLog(@"Registered to receive local notification...");
}
 
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
 
 
- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
 
//获得异常的C函数
void getException(NSException *exception)
{
    NSLog(@"名字:%@",exception.name);
    NSLog(@"原因:%@",exception.reason);
    NSLog(@"用户信息:%@",exception.userInfo);
    NSLog(@"栈内存地址:%@",exception.callStackReturnAddresses);
    NSLog(@"栈描述:%@",exception.callStackSymbols);
    //每次启动的时候将,捕获的异常信息,反馈给服务器
    //获取当前设备
    UIDevice*divice=[UIDevice currentDevice];
    //1.系统版本
    NSString*systemVersion=divice.systemVersion;
    //2.app当前版本
    //先获取当前infoplist文件数据
    NSDictionary*infoDic=[[NSBundle mainBundle] infoDictionary];
    //然后从字典中取出版本号
    NSString*version=[infoDic objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"系统版本%@",version);
    //3.系统时间
    NSDate*date=[NSDate date];
    //4.设备种类
    NSString*model=divice.model;
    //将捕获的异常数据进行保存,保存到本地
    //可以在下一次启动的时候将数据发给服务器
}
 
-(void)getUserInfo
{
    
    //[UserDefault stringForKey:@"user_id"]
    NSString *path = [[NSString alloc] initWithFormat:USER_GETINFO];
    
    CommonReqModel * model = [[CommonReqModel alloc] init];
    model.phone = [UserDefault stringForKey:@"phone"];
    
    MPWeakSelf(self);
    [NetworkSingleton networkingGetMethod:model.toDic urlName:path success:^(id responseBody)
     {
         MPStrongSelf(self);
         BaseResModel * resModel = [Global toBaseModel:responseBody];
         
         if(resModel.code == 0)
         {
             //发送成功提示
             if(![Global isEmptyObject:resModel.content])
             {
                 // 成功处理
                 _loginUser = [resModel.content mutableCopy];
                 
                 UserModel * userModel = [UserModel mj_objectWithKeyValues:resModel.content];
                 
                 // 存储用户信息
                 [UserDefault setObject:@"1" forKey:@"isLogin"];
                 [UserDefault setObject:userModel._id forKey:@"user_id"];
                 [UserDefault setObject:userModel.phone forKey:@"phone"];
                 [UserDefault setObject:userModel.password forKey:@"password"];
                 [UserDefault setObject:userModel.nickname forKey:@"nickname"];
                 [UserDefault setObject:userModel.refresh_frequency forKey:@"refresh_frequency"];
                 [UserDefault setObject:userModel.is_open_upload forKey:@"is_open_upload"];
                 [UserDefault setObject:userModel.video forKey:@"video"];
                 [UserDefault synchronize];//使用synchronize强制立即将数据写入磁盘,防止在写完NSUserDefaults后程序退出导致的数据丢失
                 
                 // 跳转主界面
                 [GetAppDelegate showHomePage];
             }
             else
             {
                 // 出错处理
                 [UserDefault setObject:@"0" forKey:@"isLogin"];
             }
 
         }
         else
         {
             [UserDefault setObject:@"0" forKey:@"isLogin"];
             //[Global alertMessageEx:resModel.desc title:@"提示信息" okTtitle:nil cancelTitle:@"OK" delegate:self];
         }
     }
       failure:^(NSString *error)
     {
         [UserDefault setObject:@"0" forKey:@"isLogin"];
         //[Global alertMessageEx:error title:@"获取失败" okTtitle:nil cancelTitle:@"OK" delegate:self];
     }];
    
}
 
 
@end