zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
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
//
//  AppDelegate.m
//  IphoneBIMe
//
//  Created by zjf on 2018/7/13.
//  Copyright © 2018年 ProBIM. All rights reserved.
 
#import "AppDelegate.h"
#import "PBProjectListViewController.h"
#import "PBMapViewController.h"
#import "LinPullMainViewController.h"
#import "PBNavigationController.h"
#import <Bugly/Bugly.h>
#import <JPUSHService.h>
#import <AdSupport/AdSupport.h>
#import "PBMessageController.h"
#import "PBNavigationController.h"
#import "iOdaApplication.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
//iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
//#define APNs_AppKey @"9b3e0bc556996b5315df45dd"
//#define APNs_AppKey @"2cac797e1ccd18aeaabb6e35"
#define APNs_AppKey @"f8a09fdef202ff4d39a7250e"
#define BUGLY_APPID @"a371eb689e"
#define UMKEY @"5bd0111bf1f55631e70003e6"
//#define AMapKEY @"2ae8576f95d622c3f1b4dfde2d079f5f"12
#define AMapKEY @"89562b948967fb2ec874487d1ceb63d0"
@interface AppDelegate ()<JPUSHRegisterDelegate>
 
@end
 
@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    PBProjectListViewController *vc = [[PBProjectListViewController alloc] init];
//    PBMapViewController *vc = [[PBMapViewController alloc] init];
    LinPullMainViewController *vc = [[LinPullMainViewController alloc] init];
    PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    //初始化网络监控
    [self monitoringNetwork];
    //初始化Bugly
    [self setBugly];
    //初始化APNs
    [self InitializeAPNs];
    //初始化JPush
    [self InitializeJPushWithOptions:launchOptions];
    // U-Share 平台设置
    [UMConfigure initWithAppkey:UMKEY channel:@"App Store"];
    [self configUSharePlatforms];
    if (launchOptions) {
        NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        if(remoteNotification) {
            NSLog(@"推送消息==== %@",remoteNotification);
            [self jumprootToMesVC];
        }
    }
    //初始化DWG预览
    [[iOdaApplication getInstance] appInitialize];
//    [AMapServices sharedServices].apiKey = @"db72d4f4202dc24cbd397c8f37b47c80";
    [AMapServices sharedServices].apiKey = AMapKEY;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    return YES;
}
 
- (void)setBugly {
    [Bugly startWithAppId:BUGLY_APPID];
}
 
- (void)monitoringNetwork {
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"========未知网络");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"=======未连接");
                [YJProgressHUD showMessage:@"o(︶︿︶)o未连接网络" inView:nil];
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"==========2G/3G网络");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"==========Wi-Fi");
                break;
        }
        self.reachabilityStates = status;
        
        NSLog(@"=========网路发生变化了");
    }];
}
 
- (void) jumprootToMesVC {
    PBMessageController * mesVC = [[PBMessageController alloc]init];
    PBNavigationController * nav = [[PBNavigationController alloc]initWithRootViewController:mesVC];//这里加导航栏是因为我跳转的页面带导航栏,如果跳转的页面不带导航,那这句话请省去。
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.window.rootViewController presentViewController:nav animated:YES completion:nil];
    
}
- (void) jumpToMesVC {
    if ([[self currentViewController] isKindOfClass:[PBMessageController class]]) {
        PBMessageController *mesVC = (PBMessageController *)[self currentViewController];
        [mesVC.tableView.mj_header beginRefreshing];
    }else {
        PBMessageController * mesVC = [[PBMessageController alloc]init];
        PBNavigationController * nav = [[PBNavigationController alloc]initWithRootViewController:mesVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [[self currentViewController] presentViewController:nav animated:YES completion:nil];
    }
}
 
- (UIViewController *)currentViewController {
    UIWindow *keyWindow  = [UIApplication sharedApplication].keyWindow;
    UIViewController *vc = keyWindow.rootViewController;
    while (vc.presentedViewController) {
        vc = vc.presentedViewController;
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = [(UINavigationController *)vc visibleViewController];
        } else if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = [(UITabBarController *)vc selectedViewController];
        }
    }
    return vc;
}
 
 
- (void)InitializeAPNs {
    //Required
    //notice: 3.0.0 及以后版本注册可以这样写,也可以继续用之前的注册方式
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    if (@available(iOS 12.0, *)) {
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
    } else {
        // Fallback on earlier versions
    }
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
      // 可以添加自定义 categories
      // NSSet<UNNotificationCategory *> *categories for iOS10 or later
      // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
}
- (void)InitializeJPushWithOptions:(NSDictionary *)launchOptions {
    // Optional
    // 获取 IDFA
    // 如需使用 IDFA 功能请添加此代码并在初始化方法的 advertisingIdentifier 参数中填写对应值
    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
 
    // Required
    // init Push
    // notice: 2.1.5 版本的 SDK 新增的注册方法,改成可上报 IDFA,如果没有使用 IDFA 直接传 nil
    [JPUSHService setupWithOption:launchOptions appKey:APNs_AppKey
                          channel:@"App Store"
                 apsForProduction:YES//用于标识当前应用所使用的 APNs 证书环境。0(默认值)表示采用的是开发证书,1 表示采用生产证书发布应用。
            advertisingIdentifier:advertisingId];
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
#pragma mark- JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
  if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    //从通知界面直接进入应用
  }else{
    //从通知设置界面进入应用
  }
}
// iOS 10 Support     //当应用在打开时 收到通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  // Required
     NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
     completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert  ); // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
    [PBNoteCenter postNotificationName:@"JPushMessage" object:nil];
}
// iOS 10 Support    //当应用在后台未杀死  发送通知点击通知时会进入此方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    [self jumpToMesVC];
  // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
      [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系统要求执行这个方法
}
 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  // Required, iOS 7 Support
  [JPUSHService handleRemoteNotification:userInfo];
  completionHandler(UIBackgroundFetchResultNewData);
}
 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required, For systems with less than or equal to iOS 6
    [JPUSHService handleRemoteNotification:userInfo];
}
#pragma mark - UMShare
- (void)configUSharePlatforms {
        /* 微信 */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx0553fe2b259ca859" appSecret:@"ce5eb5a089f1c446fbfaaf835f8b9b5b"
            redirectURL:@"http://mobile.umeng.com/social"];
        /* QQ */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"101511727"  appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
    
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
        //6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
        BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
        if (!result) {
            // 其他如支付等SDK的回调
        }
        return result;
}
- (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.
}
 
 
- (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.
}
 
 
- (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:.
}
 
- (void)setPowerArr:(NSArray *)powerArr {
    _powerArr = powerArr;
}
 
@end