AppDelegate.m
19.2 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
// AppDelegate.m
// CNLiveScioLive
//
// Created by CNLive-zxw on 2018/9/17.
// Copyright © 2018年 CNLive. All rights reserved.
//
#import "AppDelegate.h"
#import "AvoidCrash.h"
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#import <UserNotifications/UserNotifications.h>
#endif
@interface AppDelegate ()<XGPushDelegate>
@property (nonatomic, assign) BOOL isPush; //区分普通启动还是推送
@property (nonatomic, copy) NSString *type; //区分首页还是直播 还是id
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
#pragma mark - 打包注意
// [AvoidCrash makeAllEffective];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//收藏
[[DBAction sharedDBAction] createTable];
//分享
[MobClick startWithAppkey:uMengKey reportPolicy:BATCH channelId:nil];
[MobClick setLogEnabled:NO];
[self regiestShareSDK];
//语言
[self setApplicationLanguage];
//推送
[self registerRemoteNotification];
[self registerNotification:launchOptions];
[self initializeSDKConfiguration];
if(IS_CHINESE){
CNHomeRightViewController *rightVC = [[CNHomeRightViewController alloc]init];
CNLiveTabBarController *liveTab = [[CNLiveTabBarController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:liveTab];
//设置左右菜单
slideMenu.slideEnabled = NO;
rightVC.block = ^(NSInteger row) {
CNHomeViewController *vc = (CNHomeViewController *)liveTab.viewControllers[0];
vc.segmentControl.current = row;
};
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
}else{
CNLiveENTabBarController *liveTab = [[CNLiveENTabBarController alloc] init];
self.window.rootViewController = liveTab;
}
[self.window makeKeyAndVisible];
return YES;
}
//语言
#pragma mark - 语言
- (void)setApplicationLanguage{
//优先考虑本地存储语言
if ([User_Defaults objectForKey:Language_Local]) {
if ([[User_Defaults objectForKey:Language_Local] isEqualToString:@"en"]) {
self.lan = @"en";
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"Done";
}
if ([[User_Defaults objectForKey:Language_Local] isEqualToString:@"zh-Hans"]) {
self.lan = @"zh-Hans";
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"完成";
}
}
else{
//获取系统语言
if (IS_CHINESE) {
self.lan =@"zh-Hans";
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"完成";
}else{
self.lan =@"en";
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"Done";
}
}
}
- (NSString *)showText:(NSString *)key{
NSString *path = [[NSBundle mainBundle] pathForResource:_lan ofType:@"lproj"];
return [[NSBundle bundleWithPath:path] localizedStringForKey:key value:nil table:@"Localizable"];
}
- (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.
_isPush = NO;
}
- (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.
if (_isPush) {
if([self.window.rootViewController isKindOfClass:[UITabBarController class]]){
XLSlideMenu *menu = (XLSlideMenu *)self.window.rootViewController;
CNLiveTabBarController *tabVC = (CNLiveTabBarController *)menu.rootViewController;
//直播
if([self.type isEqualToString:@"1"]){
tabVC.selectedIndex = 1;
}else{
//1选中答记者问或者国新视界
tabVC.selectedIndex = 0;
//2请求跳转
[self getDataById:_type];
}
}
}
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"[XGDemo] register APNS fail.\n[XGDemo] reason : %@", error);
[[NSNotificationCenter defaultCenter] postNotificationName:@"registerDeviceFailed" object:nil];
}
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
//当APP在前台运行时,不做处理
if( [UIApplication sharedApplication].applicationState == UIApplicationStateActive){
}
//当APP在后台运行时,当有通知栏消息时,点击它,就会执行下面的方法跳转到相应的页面
else if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive){
NSLog(@"收到推送:%@",userInfo);
for (NSString *tfStr in userInfo){
if ([tfStr isEqualToString:@"careline"]) {
}
}
}
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#pragma mark - ShareSDK 注册SDK
- (void)regiestShareSDK {
[ShareSDK registerApp:@"134f952a9e004" activePlatforms:@[@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformSubTypeWechatSession),
@(SSDKPlatformSubTypeWechatTimeline),
@(SSDKPlatformTypeQQ)]
onImport:^(SSDKPlatformType platformType)
{
switch (platformType)
{
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class]];
break;
case SSDKPlatformTypeQQ:
[ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
break;
case SSDKPlatformTypeSinaWeibo:
[ShareSDKConnector connectWeibo:[WeiboSDK class]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
{
switch (platformType)
{
case SSDKPlatformTypeSinaWeibo:
//设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权
[appInfo SSDKSetupSinaWeiboByAppKey:@"604714517"
appSecret:@"542573f3d166742a230634d5c2576157"
redirectUri:@"https://api.weibo.com/oauth2/default.html"
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeWechat:
[appInfo SSDKSetupWeChatByAppId:@"wx1eb184b8425c2503"
appSecret:@"4d250e1ef3b7f5f1bc3579237fd4ee62"];
break;
case SSDKPlatformTypeQQ:
[appInfo SSDKSetupQQByAppId:@"1105424006"
appKey:@"EneqtYjiKSswk05g"
authType:SSDKAuthTypeBoth];
break;
default:
break;
}
}];
}
#pragma mark - 推送
- (void)registerNotification:(NSDictionary *)launchOptions{
[[XGPush defaultManager] setEnableDebug:YES];
XGNotificationAction *action1 = [XGNotificationAction actionWithIdentifier:@"xgaction001" title:@"xgAction1" options:XGNotificationActionOptionNone];
XGNotificationAction *action2 = [XGNotificationAction actionWithIdentifier:@"xgaction002" title:@"xgAction2" options:XGNotificationActionOptionDestructive];
if (action1 && action2) {
XGNotificationCategory *category = [XGNotificationCategory categoryWithIdentifier:@"xgCategory" actions:@[action1, action2] intentIdentifiers:@[] options:XGNotificationCategoryOptionNone];
XGNotificationConfigure *configure = [XGNotificationConfigure configureNotificationWithCategories:[NSSet setWithObject:category] types:XGUserNotificationTypeAlert|XGUserNotificationTypeBadge|XGUserNotificationTypeSound];
if (configure) {
[[XGPush defaultManager] setNotificationConfigure:configure];
}
}
[[XGPush defaultManager] startXGWithAppID:2200300438 appKey:@"IF5M37BQ66BM" delegate:self];
[[XGPush defaultManager] setXgApplicationBadgeNumber:0];
[[XGPush defaultManager] reportXGNotificationInfo:launchOptions];
}
- (void)registerRemoteNotification{
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
#pragma mark - 数据请求
- (void)getDataById:(NSString *)idStr{
if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
[AlertViewTool showHUDViewText:CustomStr(@"NetworkiInterruption")];
return;
}
NSString *strUrl = @"http://app.phonemovie.cnlive.com/gxb/api/programByContentId";
NSString *parameter = [NSString stringWithFormat:@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"38_iuvkou3xi05\",\"cmsContentId\":\"%@\"}",idStr];
NSDictionary *parameters = @{@"params":parameter};
[AlertViewTool showHUDView];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:strUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 请求成功,解析数据
NSError *err;
NSDictionary *dicData = [NSJSONSerialization JSONObjectWithData:responseObject
options:NSJSONReadingMutableContainers
error:&err];
NSDictionary *dic = dicData[@"program"];
if(dic){
if([self.window.rootViewController isKindOfClass:[UITabBarController class]] && self.window.rootViewController.childViewControllers.count > 0){
UINavigationController *nav = self.window.rootViewController.childViewControllers[0];
//判断推送消息是中文还是英文(初始化sdk配置)
[self initializeSDKConfigurationByLanguage:YES];
//视频
if([@"program" isEqualToString:dic[@"type"]]){
OldVideoDetailViewController *vc = [[OldVideoDetailViewController alloc] initWithDictionary:dic];
vc.title = dic[@"cmsColumnName"];
vc.hidesBottomBarWhenPushed = YES;
[nav pushViewController:vc animated:YES];
}
//文本
else if([@"article" isEqualToString:dic[@"type"]]){
OldArticleDetailViewController *vc = [[OldArticleDetailViewController alloc] initWithDictionary:dic];
vc.title = dic[@"cmsColumnName"];
vc.hidesBottomBarWhenPushed = YES;
[nav pushViewController:vc animated:YES];
}
}
}
[AlertViewTool hideHUDView];
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// 请求失败
NSLog(@"%@", [error localizedDescription]);
[AlertViewTool hideHUDView];
[AlertViewTool showHUDViewText:CustomStr(@"ServerBusy")];
}];
}
- (NSString *)type{
if (!_type) {
_type = [NSString stringWithFormat:@"0"];
}
return _type;
}
/**
收到通知消息的回调,通常此消息意味着有新数据可以读取(iOS 7.0+)
@param application UIApplication 实例
@param userInfo 推送时指定的参数
@param completionHandler 完成回调
*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSLog(@"[XGDemo] receive slient Notification");
NSLog(@"[XGDemo] userinfo %@", userInfo);
//当APP在前台运行时,不做处理
if( [UIApplication sharedApplication].applicationState == UIApplicationStateActive){
}
//当APP在后台运行时,当有通知栏消息时,点击它,就会执行下面的方法跳转到相应的页面
else if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive){
// self.type = response.notification.request.content.categoryIdentifier;
}
[[XGPush defaultManager] reportXGNotificationInfo:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
// iOS 10 新增 API
// iOS 10 会走新 API, iOS 10 以前会走到老 API
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// App 用户点击通知
// App 用户选择通知中的行为
// App 用户在通知中心清除消息
// 无论本地推送还是远程推送都会走这个回调
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
//当APP在前台运行时,不做处理
if( [UIApplication sharedApplication].applicationState == UIApplicationStateActive){
}
//当APP在后台运行时,当有通知栏消息时,点击它,就会执行下面的方法跳转到相应的页面
else if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive)
{
NSString *str = response.notification.request.content.categoryIdentifier;
self.type = str;
self.isPush = YES;
}
NSLog(@"[XGDemo] click notification");
if ([response.actionIdentifier isEqualToString:@"xgaction001"]) {
NSLog(@"click from Action1");
} else if ([response.actionIdentifier isEqualToString:@"xgaction002"]) {
NSLog(@"click from Action2");
}
[[XGPush defaultManager] reportXGNotificationResponse:response];
completionHandler();
}
// App 在前台弹通知需要调用这个接口
- (void)xgPushUserNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[[XGPush defaultManager] reportXGNotificationInfo:notification.request.content.userInfo];
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
#endif
#pragma mark - XGPushDelegate
- (void)xgPushDidFinishStart:(BOOL)isSuccess error:(NSError *)error {
NSLog(@"%s, result %@, error %@", __FUNCTION__, isSuccess?@"OK":@"NO", error);
// UIViewController *ctr = [self.window rootViewController];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"启动信鸽服务%@",isSuccess ? @"成功":@"失败"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
}
- (void)xgPushDidFinishStop:(BOOL)isSuccess error:(NSError *)error {
// UIViewController *ctr = [self.window rootViewController];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"启动信鸽服务%@",isSuccess ? @"成功":@"失败"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
}
- (void)xgPushDidRegisteredDeviceToken:(NSString *)deviceToken error:(NSError *)error {
NSLog(@"token:%@",deviceToken);
NSLog(@"%s, result %@, error %@", __FUNCTION__, error?@"NO":@"OK", error);
}
- (void)initializeSDKConfiguration{
//注册APP
[CNLiveStat registerApp:CNLiveAppId appKey:CNLiveAppKey];
//启动事件
[CNLiveStat event:@"start"];
//初始化直播SDK
[[CNLiveWebcastManager manager] initWithAppId:CNLiveAppId appKey:CNLiveAppKey];
//用户SDK初始化
// [CNLiveUserSystemCenter setAppId:CNLiveAppId appKey:CNLiveAppKey appSecret:CNLiveAppSecret];
[CNLiveUserSystemCenter setAppId:@"38_ivkou3xi05" appKey:@"a38d0547d9699d3c3b2cb4482c7166f313f484cc3262fe" appSecret:@"02243d3cdc1ee0650ee12fcf0cd4f1ec1ee9b8af018618"];
}
- (void)initializeSDKConfigurationByLanguage:(BOOL)isChinese{
if(isChinese){
//注册APP
[CNLiveStat registerApp:@"38_ivkou3xi05" appKey:@"a38d0547d9699d3c3b2cb4482c7166f313f484cc3262fe"];
//启动事件
[CNLiveStat event:@"start"];
//初始化直播SDK
[[CNLiveWebcastManager manager] initWithAppId:@"38_ivkou3xi05" appKey:@"a38d0547d9699d3c3b2cb4482c7166f313f484cc3262fe"];
//用户SDK初始化
// [CNLiveUserSystemCenter setAppId:@"38_ivkou3xi05" appKey:@"a38d0547d9699d3c3b2cb4482c7166f313f484cc3262fe" appSecret:@"02243d3cdc1ee0650ee12fcf0cd4f1ec1ee9b8af018618"];
}else{
//注册APP
[CNLiveStat registerApp:@"858_jloqel8r83" appKey:@"84d2370e021b40bc41889d4011fce1fb8af85598a9fcae"];
//启动事件
[CNLiveStat event:@"start"];
//初始化直播SDK
[[CNLiveWebcastManager manager] initWithAppId:@"858_jloqel8r83" appKey:@"84d2370e021b40bc41889d4011fce1fb8af85598a9fcae"];
//用户SDK初始化
// [CNLiveUserSystemCenter setAppId:@"858_jloqel8r83" appKey:@"84d2370e021b40bc41889d4011fce1fb8af85598a9fcae" appSecret:@"1d68c27291e847b3f4d8828420d8f5f2afbd77b492f1b8"];
}
}
@end