AppDelegate.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. //
  2. // AppDelegate.m
  3. // JSJPCoach
  4. //
  5. // Created by apple on 2017/3/10.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "AppDelegate.h"
  9. #import "ViewController.h"
  10. #import "LoginVC.h"
  11. #import "ADView.h"
  12. #import "MyMsgVC.h"
  13. #import <SMS_SDK/SMSSDK.h>
  14. #import "MyUINavigationController.h"
  15. #import "TheoryTrainVC.h"
  16. //客服
  17. #import "QMProfileManager.h"
  18. //bugly腾讯bug统计
  19. #import <Bugly/Bugly.h>
  20. @interface AppDelegate ()<UIScrollViewDelegate,UIAlertViewDelegate,BMKGeneralDelegate,GeTuiSdkDelegate, UNUserNotificationCenterDelegate>
  21. {
  22. UIScrollView* scroll;
  23. //no use
  24. NSString *pushType;
  25. NSString *pushID;
  26. }
  27. @end
  28. @implementation AppDelegate
  29. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  30. //异常统计
  31. #ifdef DEBUG
  32. //do sth1.
  33. #else
  34. [Bugly startWithAppId:BUGLY_APP_ID];
  35. #endif
  36. self.window = [[UIWindow alloc] initWithFrame:kFrame];
  37. [self.window makeKeyAndVisible];
  38. //初始化地图
  39. _mapManager = [[BMKMapManager alloc]init];
  40. // 如果要关注网络及授权验证事件,请设定 generalDelegate参数
  41. BOOL ret = [_mapManager start:@"d6Zg7kcdIcasgMKsFdUzyPLyFNhkkvXx" generalDelegate:self];
  42. if (!ret) {
  43. NSLog(@"manager start failed!");
  44. }
  45. //客服注册
  46. QMProfileManager *manger = [QMProfileManager sharedInstance];
  47. [manger loadProfile:@"sinatechcn" password:@"321456"];//8062moor 123456
  48. // 通过个推平台分配的appId、 appKey 、appSecret 启动SDK,注:该方法需要在主线程中调用
  49. [GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];
  50. // 注册 通知(本地/远程)
  51. [self registerRemoteNotification];
  52. /*
  53. 若用户点击icon启动,lauchOptions内无数据
  54. */
  55. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  56. //didReceiveNotificationResponse 统一处理
  57. #else
  58. //本地通知
  59. // 处理退出后通知的点击,程序启动后获取通知对象,如果是首次启动还没有发送通知,那第一次通知对象为空,没必要去处理通知(如跳转到指定页面)
  60. if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
  61. //ios<10的话,对应的didreceiveLoacal说是接受不到消息(要求前/后台),这一点在ios10以后解决了
  62. UILocalNotification *localNotifi = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
  63. NSDictionary *userInfo = localNotifi.userInfo;
  64. if (userInfo[@"theoryTrainVC_classId"]) {
  65. //理论计时提醒本地推送
  66. [self gotoTheory];
  67. return YES;
  68. }
  69. }
  70. //远程通知
  71. if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey])
  72. {
  73. //关于这个方法远程和进程不一样 一个是userinfo字典。一个是本地通知
  74. NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
  75. //这里不用判断重复消息
  76. NSString *msgId = [userInfo[@"_gmid_"] componentsSeparatedByString:@":"][1];
  77. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  78. [userDefaults setObject:msgId forKey:@"remoteNotifymsgId"];
  79. [userDefaults synchronize];
  80. NSString *jsonStr = userInfo[@"payload"];
  81. if ([jsonStr length] != 0) {
  82. NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
  83. if (data) {
  84. NSDictionary * payloadDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  85. pushType = [NSString stringWithFormat:@"%@",payloadDic[@"code"]];
  86. pushID = [NSString stringWithFormat:@"%@",payloadDic[@"id"]];
  87. if (pushType != 0) {
  88. [self gotoPush];
  89. }else{
  90. //其它信息
  91. [self gotoLoad];
  92. [self showMsgByArertWithTitle:payloadDic[@"title"] message:payloadDic[@"body"]];
  93. }
  94. return YES;
  95. }
  96. }
  97. }
  98. #endif
  99. loadADPaths();
  100. //判断跳转页面 这样每次更新版本 都会有导航页
  101. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  102. NSString *vesionNow = [infoDic objectForKey:@"CFBundleShortVersionString"];
  103. if (![defUser.isWelcomed isEqualToString:vesionNow]) {
  104. //刚刚下载 要加载一次广告 存本地
  105. defUser.isWelcomed = vesionNow;
  106. [self makeLaunchView];
  107. loadADPaths();
  108. }else{
  109. //在这里搞个 广告页面 加载广告
  110. [ADView showADView];
  111. @try {
  112. //是否为新版本
  113. [self onCheckVersion];
  114. }
  115. @catch (NSException *exception) {
  116. }
  117. @finally {
  118. [self gotoLoad];
  119. }
  120. }
  121. return YES;
  122. }
  123. #pragma mark 推送注册
  124. /** 注册 APNs */
  125. - (void)registerRemoteNotification {
  126. /*
  127. 警告:Xcode8 需要手动开启"TARGETS -> Capabilities -> Push Notifications"
  128. */
  129. if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
  130. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  131. center.delegate = self;
  132. [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) {
  133. if (!error) {
  134. NSLog(@"request authorization succeeded!(iOS >= 10.0)");
  135. }
  136. }];
  137. } else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
  138. UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
  139. UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
  140. [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
  141. }
  142. //远程需添加
  143. [[UIApplication sharedApplication] registerForRemoteNotifications];
  144. }
  145. /** 远程通知注册成功委托 */
  146. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  147. NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
  148. token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
  149. // NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token);
  150. // 向个推服务器注册deviceToken
  151. [GeTuiSdk registerDeviceToken:token];
  152. }
  153. /** 远程通知注册失败委托 */
  154. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  155. NSLog(@"FailToRegisterForRemoteNotificationsWithError:%@",error);
  156. }
  157. //iOS7.0 以后支持 APP 后台刷新数据,会回调 performFetchWithCompletionHandler 接口。为保证个推SDK的数据刷新,需在该回调接口中调用[GeTuiSdk resume]方法帮助个推 SDK 刷新数据。
  158. - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  159. /// Background Fetch 恢复SDK 运行
  160. [GeTuiSdk resume];
  161. completionHandler(UIBackgroundFetchResultNewData);
  162. }
  163. #pragma mark - iOS < 10
  164. /*
  165. 1.点击通知栏,
  166. 2.要求app未被杀死,如果app是terminate就去didfinishlaunch获取;这一点在ios10以后改善了
  167. */
  168. //-----本地推送
  169. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
  170. //ios<10 点击弹窗
  171. if (notification.userInfo[@"theoryTrainVC_classId"]) {
  172. [self gotoTheory];
  173. }
  174. }
  175. //------远程推送
  176. //处理APNs 通知点击事件,统计有效用户点击数
  177. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  178. // NSLog(@"didReceiveRemoteNotification and iOS<10");
  179. // 将收到的APNs信息传给个推统计
  180. [GeTuiSdk handleRemoteNotification:userInfo];
  181. completionHandler(UIBackgroundFetchResultNewData);
  182. [self remoteNotificationDealWithPayLoadDic:userInfo];
  183. }
  184. #pragma mark iOS > 10
  185. /*
  186. 1.对于iOS 10 及以后版本,为处理 APNs 通知点击,统计有效用户点击数,需先添加 UNUserNotificationCenterDelegate
  187. 2.app可以处于任何状态(terminate,前/后台)
  188. */
  189. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
  190. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  191. // NSLog(@"willPresentNotification and iOS>10前台:%@", notification.request.content.userInfo);
  192. // 根据APP需要,判断是否要提示用户Badge、Sound、Alert(ios>10 如果需要应用在前台也展示通知)
  193. completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
  194. }
  195. // iOS 10: 统一获取推送的接口 点击通知进入App时触发,在该方法内统计有效用户点击数 (点击弹窗,不管app处于任何状态(terminate,前/后台)都会调用)
  196. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  197. // NSLog(@"didReceiveNotificationResponse iOS>10:%@", response.notification.request.content.userInfo);
  198. //理论计时提醒本地推送
  199. if (response.notification.request.content.userInfo[@"theoryTrainVC_classId"]) {
  200. [self gotoTheory];
  201. return;
  202. }
  203. //远程推送
  204. //将收到的APNs信息传给个推统计
  205. [GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo];
  206. completionHandler();
  207. NSDictionary* userInfo = response.notification.request.content.userInfo;
  208. [self remoteNotificationDealWithPayLoadDic:userInfo];
  209. }
  210. #endif
  211. #pragma mark - 个推相关方法
  212. /** SDK启动成功返回cid */
  213. - (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
  214. //个推SDK已注册,返回clientId
  215. // NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);
  216. myDelegate.token = clientId;
  217. upLoadToken();
  218. }
  219. /** SDK遇到错误回调 */
  220. - (void)GeTuiSdkDidOccurError:(NSError *)error {
  221. //个推错误报告,集成步骤发生的任何错误都在这里通知,如果集成后,无法正常收到消息,查看这里的通知。
  222. NSLog(@"\n>>>[GexinSdk error]:%@\n\n", [error localizedDescription]);
  223. }
  224. /** SDK收到透传消息回调 */
  225. - (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
  226. //收到个推消息
  227. if (payloadData) {
  228. // NSString * payloadMsg = [[NSString alloc] initWithBytes:payloadData.bytes length:payloadData.length encoding:NSUTF8StringEncoding];
  229. // NSLog(@"GeTuiSdkDidReceivepayloadMsg:%@",payloadMsg);
  230. //判断是否被处理过[即发送离线消息的时候,会走apns+个推sdk(有效期内)]
  231. //ps:这里处理之后不做存档操作,保证点击通知栏的时候可以”再处理一次“
  232. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  233. NSString *oldmsgId = [userDefaults objectForKey:@"remoteNotifymsgId"];
  234. if ([oldmsgId isEqualToString:msgId]) {
  235. return;
  236. }
  237. NSDictionary * payloadDic = [NSJSONSerialization JSONObjectWithData:payloadData options:NSJSONReadingMutableContainers error:nil];
  238. if (payloadDic.count == 0) {
  239. return;
  240. }
  241. pushType = [NSString stringWithFormat:@"%@",payloadDic[@"code"]];
  242. pushID = [NSString stringWithFormat:@"%@",payloadDic[@"id"]];
  243. if ([pushType integerValue] != 0) {
  244. UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:payloadDic[@"title"] message:payloadDic[@"body"] preferredStyle:UIAlertControllerStyleAlert];
  245. [alertFind addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  246. }]];
  247. [alertFind addAction:[UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  248. [self gotoPush];
  249. }]];
  250. [self.window.rootViewController presentViewController:alertFind animated:true completion:nil];
  251. }else{
  252. //其它信息
  253. [self showMsgByArertWithTitle:payloadDic[@"title"] message:payloadDic[@"body"]];
  254. }
  255. }
  256. }
  257. #pragma mark 远程通知的数据处理
  258. -(void)remoteNotificationDealWithPayLoadDic:(NSDictionary *)userInfo{
  259. /*
  260. {
  261. "_ge_" = 1;
  262. "_gmid_" = "OSL-0818_bJ6zVRy8cC6JsflZOYPXO9:965f14a1051748c4a48f9db2437362e5:d48422d462d2bcf0b9d962f150b235b9";
  263. "_gurl_" = "sdk.open.extension.getui.com:8123";
  264. aps = {
  265. alert = {
  266. body = "\U60a8\U9a7e\U6821\U6709\U8f66\U8f86\U7ec8\U7aef\U62a5\U8b66\U9700\U89e3\U9664\Uff0c\U8bf7\U67e5\U770b\U5e76\U5904\U7406";
  267. title = "\U544a\U8b66\U6d88\U606f";
  268. };
  269. badge = 2;
  270. category = "button\U663e\U793a";
  271. "content-available" = 1;
  272. "mutable-content" = 1;
  273. sound = default;
  274. };
  275. payload = "{\"title\":\"\U544a\U8b66\U6d88\U606f\",\"code\":\"1\",\"body\":\"\U60a8\U9a7e\U6821\U6709\U8f66\U8f86\U7ec8\U7aef\U62a5\U8b66\U9700\U89e3\U9664\Uff0c\U8bf7\U67e5\U770b\U5e76\U5904\U7406\,\"id\":\"2\"}";
  276. }
  277. */
  278. // _gmid_对应-- taskId msgId clientID
  279. //这里使用msgid判断是否被处理【不过似乎应该用taskid?@lee,幻想着一个任务多条消息]
  280. NSString *msgId = [userInfo[@"_gmid_"] componentsSeparatedByString:@":"][1];
  281. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  282. NSString *oldMsgId = [userDefaults objectForKey:@"remoteNotifymsgId"];
  283. if ([msgId isEqualToString:oldMsgId]) {
  284. return;
  285. }
  286. //开始处理
  287. [userDefaults setObject:msgId forKey:@"remoteNotifymsgId"];
  288. [userDefaults synchronize];
  289. NSString *jsonStr = userInfo[@"payload"];
  290. if ([jsonStr length] == 0) {
  291. return;
  292. }
  293. NSData *data = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
  294. if (data) {
  295. NSDictionary * payloadDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  296. pushType = [NSString stringWithFormat:@"%@",payloadDic[@"code"]];
  297. pushID = [NSString stringWithFormat:@"%@",payloadDic[@"id"]];
  298. if (pushType != 0) {
  299. [self gotoPush];
  300. }else{
  301. //其它信息
  302. [self showMsgByArertWithTitle:payloadDic[@"title"] message:payloadDic[@"body"]];
  303. }
  304. }
  305. }
  306. -(void)showMsgByArertWithTitle:(NSString *)title message:(NSString *)message{
  307. if (message.length == 0) {
  308. return;
  309. }
  310. //其它信息
  311. UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  312. [alertFind addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  313. }]];
  314. [self.window.rootViewController presentViewController:alertFind animated:true completion:nil];
  315. }
  316. #pragma mark 跳转相关
  317. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  318. {
  319. if (alertView.tag == 10000) {
  320. if (buttonIndex == 1) {
  321. NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/liao-ning-jia-pei-jiao-lian-ban/id1225444971?l=en&mt=8"];
  322. [[UIApplication sharedApplication]openURL:url];
  323. }
  324. }else{
  325. if (buttonIndex != alertView.cancelButtonIndex) {
  326. [self gotoPush];
  327. }
  328. }
  329. }
  330. -(void)gotoTheory{
  331. TheoryTrainVC * vc = [[TheoryTrainVC alloc]init];
  332. vc.isNotification = YES;
  333. MyUINavigationController *nav = [[MyUINavigationController alloc]initWithRootViewController:vc];
  334. [myDelegate.window setRootViewController:nav];
  335. }
  336. -(void)gotoPush
  337. {
  338. [self goToMessage];
  339. }
  340. -(void)goToMessage
  341. {
  342. MyMsgVC* vc = [[MyMsgVC alloc] init];
  343. vc.isNotification = YES;
  344. MyUINavigationController *nav = [[MyUINavigationController alloc]initWithRootViewController:vc];
  345. [myDelegate.window setRootViewController:nav];
  346. }
  347. #pragma mark -
  348. //程序挂起
  349. - (void)applicationWillResignActive:(UIApplication *)application {
  350. }
  351. //程序进入后台
  352. - (void)applicationDidEnterBackground:(UIApplication *)application {
  353. myDelegate.isBackgroundTask = YES;
  354. }
  355. //程序到前台
  356. - (void)applicationWillEnterForeground:(UIApplication *)application {
  357. //[ADView showADView];
  358. }
  359. //程序复原
  360. - (void)applicationDidBecomeActive:(UIApplication *)application
  361. {
  362. [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];//本地
  363. myDelegate.isBackgroundTask = NO;
  364. [GeTuiSdk resetBadge];//同步给个推
  365. }
  366. - (void)applicationWillTerminate:(UIApplication *)application {
  367. }
  368. #pragma mark 检查新旧
  369. -(void)onCheckVersion
  370. {
  371. if (![Util connectedToNetWork])
  372. {
  373. return;
  374. }
  375. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  376. NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
  377. NSString *URL = @"http://itunes.apple.com/lookup?id=1225444971";
  378. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  379. [request setURL:[NSURL URLWithString:URL]];
  380. [request setHTTPMethod:@"POST"];
  381. NSOperationQueue *queue=[NSOperationQueue mainQueue];
  382. [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  383. if (!data) {
  384. return;
  385. }
  386. //如果data等于nil的时候 再去解析 会发生崩溃
  387. NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  388. if (!dic) {
  389. return;
  390. }
  391. NSArray *infoArray = [dic objectForKey:@"results"];
  392. if ([infoArray count]) {
  393. NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
  394. NSString *lastVersion = [releaseInfo objectForKey:@"version"];
  395. NSArray *last = [lastVersion componentsSeparatedByString:@"."];
  396. NSArray *current = [currentVersion componentsSeparatedByString:@"."];
  397. NSComparisonResult result = [last[0] compare:current[0]];
  398. if (result == NSOrderedDescending) {
  399. [self updateVersion];
  400. }else if(result == NSOrderedSame){
  401. result = [last[1] compare:current[1]];
  402. if (result == NSOrderedDescending) {
  403. [self updateVersion];
  404. }else if(result == NSOrderedSame){
  405. result = [last[2] compare:current[2]];
  406. if (result == NSOrderedDescending) {
  407. [self updateVersion];
  408. }
  409. }
  410. }
  411. }
  412. }];
  413. }
  414. -(void)updateVersion
  415. {
  416. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新提示" message:@"有新的版本发布,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
  417. alert.tag = 10000;
  418. [alert show];
  419. }
  420. #pragma mark - 欢迎页面
  421. -(void)makeLaunchView
  422. {
  423. NSArray* images = @[@"welcom_page_21.jpg",@"welcom_page_22.jpg",@"welcom_page_23.jpg"];
  424. //关于导航页要修改 添上新的导航页
  425. scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, kSize.height)];
  426. /**跳到第4页,也会欢迎结束。
  427. */
  428. [scroll setContentSize:CGSizeMake(kSize.width* images.count+10, 0)];
  429. [scroll setBackgroundColor:[UIColor orangeColor]];
  430. [scroll setShowsHorizontalScrollIndicator:NO];
  431. [scroll setDelegate:self];
  432. scroll.pagingEnabled = YES;
  433. scroll.bounces = NO;
  434. UIViewController* vc = [UIViewController new];
  435. [vc.view addSubview:scroll];
  436. [self.window setRootViewController:vc];
  437. UIImageView* iv;
  438. for (int i =0; i<images.count; i++) {
  439. iv = [[UIImageView alloc] initWithFrame:kFrame];
  440. iv.x = kSize.width*i;
  441. [scroll addSubview:iv];
  442. [iv setImage:[UIImage imageNamed:images[i]]];
  443. UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width-70+kSize.width*i, kStatusHeight, 50, 28)];
  444. btn.backgroundColor = [UIColor darkGrayColor];
  445. [btn setTitle:[NSString stringWithFormat:@"%d / %ld",i+1,images.count] forState:UIControlStateNormal];
  446. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  447. if (i==images.count-1) {
  448. btn.tag = 100;
  449. }
  450. [scroll addSubview:btn];
  451. [btn addTarget:self action:@selector(nextClick:) forControlEvents:UIControlEventTouchUpInside];
  452. }
  453. UIButton* btn = [[UIButton alloc] initWithFrame:iv.frame];
  454. [scroll addSubview:btn];
  455. [btn addTarget:self action:@selector(gotoLoad) forControlEvents:UIControlEventTouchUpInside];
  456. }
  457. -(void)nextClick:(UIButton *)btn{
  458. if (btn.tag == 100) {
  459. [self gotoLoad];
  460. return;
  461. }
  462. CGPoint off = scroll.contentOffset;
  463. [scroll setContentOffset:CGPointMake(off.x+kSize.width, off.y) animated:YES];
  464. }
  465. -(void)gotoLoad
  466. {
  467. ViewController *vc = [ViewController new];
  468. MyUINavigationController *nav = [[MyUINavigationController alloc] initWithRootViewController:vc];
  469. nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  470. self.window.rootViewController = nav;
  471. }
  472. -(void)scrollViewDidScroll:(UIScrollView *)scrollView
  473. {
  474. CGPoint off = scrollView.contentOffset;
  475. if (off.x / kSize.width > 2.0) {
  476. [self gotoLoad];
  477. }
  478. }
  479. @end