IntroViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // IntroViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 9/16/15.
  6. // Copyright (c) 2015 Jason Lee. All rights reserved.
  7. //
  8. #import "RequestHandler.h"
  9. #import "IntroViewController.h"
  10. @interface IntroViewController () <NSNetServiceBrowserDelegate, NSNetServiceDelegate> {
  11. NSNetServiceBrowser *_netServiceBrowser;
  12. BOOL _isNotFirstLoading;
  13. }
  14. @property (strong, nonatomic) NSNetService *netService;
  15. @end
  16. #pragma mark - Class Definition
  17. @implementation IntroViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. // Do any additional setup after loading the view.
  21. [self initUI];
  22. NSLog(@"%s\n applicationState = %zd", __PRETTY_FUNCTION__, [UIApplication sharedApplication].applicationState);
  23. if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive) {
  24. [self prepareViewDidLoad];
  25. }
  26. }
  27. - (void)initUI {
  28. NSString *test = @"test";
  29. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, test);
  30. DDLogError(@"test colors");
  31. }
  32. - (void)prepareViewDidLoad {
  33. }
  34. - (void)viewDidAppear:(BOOL)animated {
  35. [super viewDidAppear: animated];
  36. [self publishNetService];
  37. }
  38. #pragma mark - Main Logic
  39. - (void)publishNetService {
  40. _netService = [[NSNetService alloc] initWithDomain:@"" type:@"_ntels._tcp" name:[JDFacade facade].deviceHostName port:7979];
  41. if (_netService) {
  42. _netService.delegate = self;
  43. [_netService publish];
  44. } else {
  45. NSLog(@"An error occurred initializing the NSNetService object.");
  46. }
  47. }
  48. - (void)requestBonjourList {
  49. if (_netServiceBrowser) {
  50. [_netServiceBrowser stop];
  51. _netServiceBrowser = nil;
  52. }
  53. _netServiceBrowser = [[NSNetServiceBrowser alloc] init];
  54. _netServiceBrowser.delegate = self;
  55. NSString *browseType;
  56. if (_netService == nil) {
  57. browseType = @"_services._dns-sd._udp.";
  58. self.title = @"mDNS";
  59. } else {
  60. NSString *fullDomainName = [NSString stringWithFormat:@"%@.%@", _netService.name, _netService.type];
  61. NSArray *domainNameParts = [fullDomainName componentsSeparatedByString:@"."];
  62. browseType = [NSString stringWithFormat:@"%@.%@.", [domainNameParts objectAtIndex:0], [domainNameParts objectAtIndex:1]];
  63. self.title = _netService.name;
  64. }
  65. [_netServiceBrowser searchForServicesOfType:browseType inDomain:@""];
  66. }
  67. - (void)requestVersionInfo {
  68. //parameters
  69. NSDictionary *parameter = @{@"os_type": MOBILE_DEVICE_TYPE,
  70. @"store_code": APPSTORE_TYPE,
  71. @"app_version": [CommonUtil applicationShortVersion],
  72. @"service_id": MOBILE_SERVICE_ID};
  73. NSString *path = [NSString stringWithFormat:API_GET_APP_VERSION];
  74. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  75. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  76. return;
  77. }
  78. JDJSONModel *result = (JDJSONModel *) responseObject;
  79. if (result) {//API 성공 ,
  80. if ([result.resultCode isEqualToString:@"99"]) {//사용불가
  81. //앱 업그레이드 유도
  82. [[JDFacade facade] alert:MSG_ALERT_APP_UPDATE completionHander:^{
  83. NSString *appURL = URL_APP_UPDATE;
  84. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
  85. }];
  86. } else {
  87. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  88. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"LoginViewController" storyboardName:@"Main"];
  89. [JDFacade facade].appDelegate.window.rootViewController = vc;
  90. });
  91. }
  92. }
  93. } failure:^(id errorObject) {
  94. JDErrorModel *error = (JDErrorModel *)errorObject;
  95. [[JDFacade facade] alert:error.errorMessage];
  96. }];
  97. }
  98. #pragma mark - NSNetServiceDelegate methods
  99. - (void)netServiceDidPublish:(NSNetService *)sender {
  100. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, sender);
  101. if (!_isNotFirstLoading) {
  102. [self performSelector:@selector(requestVersionInfo) withObject:nil afterDelay:0.0f];
  103. _isNotFirstLoading = YES;
  104. }
  105. }
  106. - (void)netServiceWillPublish:(NSNetService *)sender {
  107. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, sender);
  108. }
  109. - (void)netServiceDidStop:(NSNetService *)sender {
  110. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, sender);
  111. }
  112. - (void)netService:(NSNetService *)sender didAcceptConnectionWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream {
  113. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, sender);
  114. }
  115. - (void)netService:(NSNetService *)sender didNotPublish:(NSDictionary<NSString *,NSNumber *> *)errorDict {
  116. NSLog(@"%s\n %@", __PRETTY_FUNCTION__, errorDict);
  117. }
  118. - (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict {
  119. NSNumber *errorCode = [errorDict valueForKey:NSNetServicesErrorCode];
  120. NSString *errorMessage;
  121. switch ([errorCode intValue]) {
  122. case NSNetServicesActivityInProgress:
  123. errorMessage = @"서비스에 연결중입니다. 잠시 기다려 주세요.";
  124. // errorMessage = @"Service Resolution Currently in Progress. Please Wait.";
  125. break;
  126. case NSNetServicesTimeoutError:
  127. errorMessage = @"서비스에 연결할 수 없습니다.";
  128. // errorMessage = @"Service Resolution Timeout";
  129. [sender stop];
  130. break;
  131. }
  132. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"mDNS Browser" message:errorMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  133. [alert show];
  134. }
  135. - (void)netServiceDidResolveAddress:(NSNetService *)service {
  136. // mDNSDetailViewController *sdvc = [self.storyboard instantiateViewControllerWithIdentifier:@"mDNSDetailViewController"];
  137. // sdvc.selectedService = service;
  138. //
  139. // [self.navigationController pushViewController:sdvc animated:YES];
  140. }
  141. #pragma mark - MemoryWarning
  142. - (void)viewDidDisappear:(BOOL)animated {
  143. [_netServiceBrowser stop];
  144. }
  145. #pragma mark - UI Events
  146. #pragma mark - MemoryWarning
  147. - (void)didReceiveMemoryWarning
  148. {
  149. [super didReceiveMemoryWarning];
  150. // Dispose of any resources that can be recreated.
  151. }
  152. @end