CommonUtil.m 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. //
  2. // CommonUtil.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 2/14/14.
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. @import CoreTelephony.CTCarrier;
  9. @import CoreTelephony.CTCallCenter;
  10. @import CoreTelephony.CTCall;
  11. @import CoreTelephony.CTTelephonyNetworkInfo;
  12. #import "Common.h"
  13. #import "CommonUtil.h"
  14. #import "SortDictionary.h"
  15. #import "UIDeviceUtil.h"
  16. #define ksEmptyString @""
  17. @implementation CommonUtil
  18. #pragma mark - System Util
  19. + (NSString *)applicationVersion {
  20. NSString *strVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
  21. // CGFloat version = [[[UIDevice currentDevice] ] floatValue];
  22. // CGFloat version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey] floatValue];
  23. return strVersion;
  24. }
  25. + (NSString *)applicationShortVersion {
  26. NSString *strVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
  27. // CGFloat version = [[[UIDevice currentDevice] ] floatValue];
  28. // CGFloat version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey] floatValue];
  29. return strVersion;
  30. }
  31. + (NSString *)bundleIdentifier {
  32. return [[NSBundle mainBundle] bundleIdentifier];
  33. }
  34. + (CGFloat)deviceSystemVersion {
  35. static NSUInteger _deviceSystemVersion = -1.0f;
  36. static dispatch_once_t onceToken;
  37. dispatch_once(&onceToken, ^{
  38. _deviceSystemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
  39. });
  40. return _deviceSystemVersion;
  41. }
  42. + (NSString *)uniqueIdentifier {
  43. //uuid 로 변경
  44. NSString *identifier = ksEmptyString;
  45. // NSString *identifier = [[OSUserDefaults defaults] objectForKey:@"MAC_ADD"];
  46. //
  47. // if ([identifier length] == 0 || [identifier isEqual:ksEmptyString]) {
  48. // CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
  49. // NSString *string = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuid));
  50. //
  51. // [[OSUserDefaults defaults] setObject:string forKey:@"MAC_ADD"];
  52. // [[OSUserDefaults defaults] synchronize];
  53. // identifier = string;
  54. // }
  55. NSLog(@"===identifier===%@", identifier);
  56. return identifier;
  57. }
  58. + (NSString *)deviceName {//////////////////
  59. return [UIDevice currentDevice].name;
  60. }
  61. + (CGFloat)hardwareVersion {
  62. return [UIDeviceUtil hardwareNumber:[UIDeviceUtil hardware]];
  63. }
  64. + (NSUInteger)hardware {
  65. return [UIDeviceUtil hardware];
  66. }
  67. + (void)showCurrentDevice {
  68. UIDevice *device = [UIDevice currentDevice];
  69. NSMutableString *print = [NSMutableString new];
  70. [print appendFormat:@"\n%@\n", device.name];
  71. [print appendFormat:@"%@\n", device.systemName];
  72. [print appendFormat:@"%@\n", device.model];
  73. [print appendFormat:@"%@\n", device.localizedModel];
  74. [print appendFormat:@"%zd\n", device.userInterfaceIdiom];
  75. [print appendFormat:@"%@\n", [UIDeviceUtil hardwareSimpleDescription]];
  76. }
  77. #pragma makr - Locale
  78. + (NSString *)countryCode {
  79. return [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
  80. }
  81. #pragma mark - Network Util
  82. + (BOOL)roamingCheck {
  83. CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
  84. CTCarrier *carrier = [networkInfo subscriberCellularProvider];
  85. NSString *carrierName = [carrier carrierName]; //통신사를 받아올 수 있다.
  86. NSLog(@"Carrier: %@", carrierName);
  87. NSString *mnc = [carrier mobileNetworkCode]; //통신사를 받아올 수 있다.
  88. NSLog(@"mnc: %@", mnc);
  89. NSString *mcc = [carrier mobileCountryCode]; // 국가코드를 받아올 수 있다.
  90. NSLog(@"Mobile Country Code (MCC): %@", mcc);
  91. BOOL network = NO;
  92. if ([mnc isEqualToString:@"05"] || [mnc isEqualToString:@"06"] || [mnc isEqualToString:@"08"] ) { //mnc code
  93. if ([mcc isEqualToString:@"450"]) { //국가 코드
  94. network = YES;
  95. }
  96. }
  97. return network;
  98. }
  99. + (BOOL)isOnPhoneCall {
  100. CTCallCenter *callCenter = [[CTCallCenter alloc] init];
  101. for (CTCall *call in callCenter.currentCalls) {
  102. if (call.callState == CTCallStateConnected) {
  103. return YES;
  104. }
  105. }
  106. return NO;
  107. }
  108. #pragma mark - File Util
  109. +(NSString*)getPath:(NSString *)lastPath {
  110. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  111. NSString *documentsDirectory= [paths objectAtIndex:0];
  112. return [documentsDirectory stringByAppendingPathComponent:lastPath];
  113. }
  114. +(BOOL)isExistFile:(NSString*)filePath {
  115. BOOL isExist = NO;
  116. NSFileManager *fileManager = [NSFileManager defaultManager];
  117. if ([fileManager fileExistsAtPath:filePath])
  118. isExist = YES;
  119. else
  120. isExist = NO;
  121. return isExist;
  122. }
  123. + (BOOL)createFile:(NSString*)filePath {
  124. BOOL bCreate = NO;
  125. NSLog(@"document file path : %@", filePath);
  126. //파일의 존재 유무 검사 및 없으면 생성
  127. NSFileManager *fileManager = [NSFileManager defaultManager];
  128. BOOL isFile = [fileManager fileExistsAtPath:filePath];
  129. if (isFile | [fileManager createFileAtPath:filePath contents:nil attributes:nil]){
  130. bCreate = YES;
  131. }else{
  132. bCreate = NO;
  133. }
  134. return bCreate;
  135. }
  136. + (BOOL)deleteFile:(NSString *)fileName {
  137. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  138. NSString *filepath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0], fileName];
  139. NSFileManager *fileManager = [NSFileManager defaultManager];
  140. [fileManager removeItemAtPath:filepath error:nil];
  141. return YES;
  142. }
  143. #pragma mark - UI Util
  144. + (id)instantiateViewControllerWithIdentifier:(NSString *)identifier storyboardName:(NSString *)storyboardName
  145. {
  146. UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
  147. return [sb instantiateViewControllerWithIdentifier:identifier];
  148. }
  149. + (UIViewController *)currentViewController {
  150. id appDelegate = [UIApplication sharedApplication].delegate;
  151. UIViewController *vc = (UINavigationController *)[[appDelegate window] rootViewController];
  152. if (vc.presentedViewController) {
  153. while (vc.presentedViewController) {
  154. vc = vc.presentedViewController;
  155. }
  156. if ([vc isKindOfClass:[UINavigationController class]]) {
  157. NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
  158. vc = [vcArray objectAtIndex:vcArray.count - 1];
  159. }
  160. return vc;
  161. }
  162. if (vc.presentingViewController && (vc.modalPresentationStyle == UIModalPresentationFullScreen)) {
  163. vc = vc.presentingViewController;
  164. }
  165. if ([NSClassFromString(@"ECSlidingViewController") class] && [vc isKindOfClass:[NSClassFromString(@"ECSlidingViewController") class]]) {
  166. id slideController = vc;
  167. vc = (UINavigationController *)[slideController topViewController];
  168. }
  169. if ([vc isKindOfClass:[UINavigationController class]]) {
  170. NSArray *vcArray = ((UINavigationController *)vc).viewControllers;
  171. vc = [vcArray objectAtIndex:vcArray.count - 1];
  172. }
  173. return vc;
  174. }
  175. + (UIView *)topView
  176. {
  177. UIView *topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
  178. while ([topView isKindOfClass:NSClassFromString(@"UITransitionView")]) {
  179. topView = [[topView subviews] lastObject];
  180. }
  181. if ([topView isKindOfClass:NSClassFromString(@"CustomLoadingView")]) {
  182. topView = [[[[UIApplication sharedApplication] keyWindow] subviews] lastObject];
  183. topView = [topView subviews][topView.subviews.count - 2];
  184. }
  185. topView = topView == nil ? [[UIApplication sharedApplication] keyWindow] : topView;
  186. return topView;
  187. }
  188. + (CGRect)mainScreenBounds {
  189. CGRect screenRect = [UIScreen mainScreen].bounds;
  190. return screenRect;
  191. }
  192. //해당 컨테이너로부터 해당 객체의 중앙 지점을 연산하여 반환함.
  193. + (CGRect)getCenterPosition:(id)object fromContainer:(id)container hasNavigationBar:(BOOL)hasNavigationBar hasTabBar:(BOOL)hasTabBar {
  194. CGRect frame = CGRectMake(0.0, 0.0, 0.0, 0.0);
  195. if (object && container) {
  196. CGRect cframe = [container frame];
  197. CGRect oframe = [object frame];
  198. float nHeight = hasNavigationBar ? 44.0 : 0;
  199. float tHeight = hasTabBar ? 59.0 : 0;
  200. float x = ((cframe.size.width - oframe.size.width) / 2);
  201. float y = (cframe.size.height - (oframe.size.height - nHeight - tHeight)) / 2;
  202. frame = CGRectMake(x, y, oframe.size.width, oframe.size.height);
  203. }
  204. return frame;
  205. }
  206. + (CGRect)centerRect:(id)object fromContainer:(id)container {
  207. CGRect frame = CGRectMake(0.0, 0.0, 0.0, 0.0);
  208. if (object && container) {
  209. CGRect cframe = [container frame];
  210. CGRect oframe = [object frame];
  211. float x = ((cframe.size.width - oframe.size.width) / 2);
  212. float y = (cframe.size.height - (oframe.size.height)) / 2;
  213. frame = CGRectMake(x, y, oframe.size.width, oframe.size.height);
  214. }
  215. return frame;
  216. }
  217. //해당 객체를 해당 컨테이너의 중앙(가로*세로)에 위치시킴.
  218. + (void)moveToCenter:(id)object withContainer:(id)container hasNavigationBar:(BOOL)hasNavigationBar hasTabBar:(BOOL)hasTabBar {
  219. CGRect rect = [CommonUtil getCenterPosition:object fromContainer:container hasNavigationBar:hasNavigationBar hasTabBar:hasTabBar];
  220. UIView *oView = (UIView *)object;
  221. [oView setFrame:rect];
  222. }
  223. //해당 객체를 해당 컨테이너의 중앙(가로)에 위치시킴.
  224. + (void)moveToCenterHorizon:(id)object withContainer:(id)container {
  225. CGRect rect = [CommonUtil getCenterPosition:object fromContainer:container hasNavigationBar:NO hasTabBar:NO];
  226. CGRect oframe = [object frame];
  227. oframe.origin.x = rect.origin.x;
  228. UIView *oView = (UIView *)object;
  229. [oView setFrame:oframe];
  230. }
  231. //해당 객체를 해당 컨테이너의 중앙(세로)에 위치시킴.
  232. + (void)moveToCenterVertical:(id)object withContainer:(id)container {
  233. CGRect rect = [CommonUtil getCenterPosition:object fromContainer:container hasNavigationBar:NO hasTabBar:NO];
  234. CGRect oframe = [object frame];
  235. oframe.origin.y = rect.origin.y;
  236. UIView *oView = (UIView *)object;
  237. [oView setFrame:oframe];
  238. }
  239. + (CGSize)sizeWithString:(NSString *)text font:(UIFont *)font constrainedToSize:(CGSize)constrainedToSize lineBreakMode:(NSLineBreakMode)lineBreakMode {
  240. CGSize size = CGSizeZero;
  241. if (IOS_VERSION >= 7.0f) {
  242. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:font}];
  243. CGRect rect = [attributedText boundingRectWithSize:(CGSize){constrainedToSize.width, CGFLOAT_MAX}
  244. options:NSStringDrawingUsesLineFragmentOrigin
  245. context:nil];
  246. size = rect.size;
  247. }
  248. return size;
  249. }
  250. + (float)sizeWithStringHeight:(NSString *)text font:(UIFont *)font constrainedToSize:(float)width lineBreakMode:(NSLineBreakMode)lineBreakMode {
  251. float size = 0;
  252. if (IOS_VERSION >= 7.0f) {
  253. NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:font}];
  254. CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
  255. options:NSStringDrawingUsesLineFragmentOrigin
  256. context:nil];
  257. size = rect.size.height;
  258. }
  259. return size;
  260. }
  261. + (CGFloat)getLabelRealWidth:(UILabel *)label fixedWidth:(CGFloat)fixedWidth {
  262. CGFloat width = 0.0f;
  263. CGRect oriRect = label.frame;
  264. CGRect chRect = label.frame;
  265. chRect.size.width = fixedWidth;
  266. label.frame = chRect;
  267. [label sizeToFit];
  268. width = label.frame.size.width;
  269. label.frame = oriRect;
  270. return width;
  271. }
  272. + (CGFloat)getLabelRealHeight:(UILabel *)label {
  273. CGFloat height = 0.0f;
  274. CGRect oriRect = label.frame;
  275. [label sizeToFit];
  276. height = label.frame.size.height;
  277. label.frame = oriRect;
  278. return height;
  279. }
  280. + (CGSize)getLabelRealSize:(UILabel *)label fixedWdith:(CGFloat)fixedWidth {
  281. CGFloat width = [CommonUtil getLabelRealWidth:label fixedWidth:fixedWidth];
  282. CGFloat height = [CommonUtil getLabelRealHeight:label];
  283. return CGSizeMake(width, height);
  284. }
  285. + (CGSize)getSizeFromString:(NSString *)string font:(UIFont *)font width:(CGFloat)fixedWidth {
  286. if ([string isEqualToString:ksEmptyString]) {
  287. return CGSizeZero;
  288. }
  289. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  290. CGSize labelSize = (CGSize){fixedWidth, FLT_MAX};
  291. CGRect r = [string boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:context];
  292. r = CGRectIntegral(r);
  293. return r.size;
  294. }
  295. + (CGSize)getSizeFromAttributeString:(NSAttributedString *)string width:(CGFloat)fixedWidth {
  296. if ([string.string isEqualToString:ksEmptyString]) {
  297. return CGSizeZero;
  298. }
  299. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  300. CGSize labelSize = (CGSize){fixedWidth, FLT_MAX};
  301. CGRect r = [string boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin context:context];
  302. r = CGRectIntegral(r);
  303. return r.size;
  304. }
  305. /**
  306. NIB에서 해당 뷰를 로드함.
  307. @return NSArray * 뷰어레이
  308. */
  309. + (NSArray *)nibViews:(NSString *)nibName {
  310. UINib *nib = [UINib nibWithNibName:nibName bundle:[NSBundle mainBundle]];
  311. return [nib instantiateWithOwner:nil options:nil];
  312. }
  313. #pragma mark - Date Util
  314. + (NSCalendar *)calendar {
  315. NSCalendar *cal = [NSCalendar currentCalendar];
  316. [cal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
  317. return cal;
  318. }
  319. + (NSDateFormatter *)dateFormatter {
  320. static NSDateFormatter *__shareDateFormatter = nil;
  321. static dispatch_once_t onceToken;
  322. dispatch_once(&onceToken, ^{
  323. __shareDateFormatter = [[NSDateFormatter alloc] init];
  324. NSLocale *locale = [NSLocale currentLocale];
  325. [__shareDateFormatter setLocale:locale];
  326. });
  327. return __shareDateFormatter;
  328. }
  329. + (NSString *)currentMonth {
  330. NSDateFormatter *df = [CommonUtil dateFormatter];
  331. df.dateFormat = @"YYYY.MM";
  332. NSString *title = [NSString stringWithFormat:@"%@", [df stringFromDate:[NSDate date]]];
  333. return title;
  334. }
  335. + (NSDateComponents *)dateComponents:(NSDate *)date {
  336. NSCalendar *calendar = [CommonUtil calendar];
  337. NSDateComponents *components = [calendar components:(NSCalendarUnitYear |
  338. NSCalendarUnitMonth |
  339. NSCalendarUnitDay |
  340. NSCalendarUnitHour |
  341. NSCalendarUnitMinute|
  342. NSCalendarUnitSecond|
  343. NSCalendarUnitTimeZone) fromDate:date];
  344. return components;
  345. }
  346. + (NSDateComponents *)dateComponents:(NSDate *)date timezone:(NSTimeZone *)timezone {
  347. NSCalendar *calendar = [CommonUtil calendar];
  348. [calendar setTimeZone:timezone];
  349. NSDateComponents *components = [calendar components:(NSCalendarUnitYear |
  350. NSCalendarUnitMonth |
  351. NSCalendarUnitDay |
  352. NSCalendarUnitHour |
  353. NSCalendarUnitMinute|
  354. NSCalendarUnitSecond|
  355. NSCalendarUnitTimeZone) fromDate:date];
  356. return components;
  357. }
  358. //Date객체를 String객체로 전환
  359. + (NSString *)stringFromDate:(NSDate *)date {
  360. NSDateFormatter *df = [CommonUtil dateFormatter];
  361. df.dateFormat = @"YYYY/MM/dd";
  362. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  363. }
  364. + (NSString *)stringFromDateTime:(NSDate *)date {
  365. NSDateFormatter *df = [CommonUtil dateFormatter];
  366. df.dateFormat = @"yyyyMMddHHmmss";
  367. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  368. }
  369. + (NSString *)birthStringFromDate:(NSDate *)date {
  370. NSDateFormatter *df = [CommonUtil dateFormatter];
  371. df.dateFormat = @"YYYYMMdd";
  372. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  373. }
  374. + (NSString *)stringFromDate2:(NSDate *)date {
  375. NSDateFormatter *df = [CommonUtil dateFormatter];
  376. df.dateFormat = @"YYYY-MM-dd";
  377. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  378. }
  379. + (NSString *)stringFromString:(NSString *)dateString {
  380. NSDateComponents *components = [CommonUtil dateComponentsFromString:dateString];
  381. return [CommonUtil stringFromDateComponents:components];
  382. }
  383. + (NSDateComponents *)dateComponentsFromString:(NSString *)dateString {
  384. NSDateComponents *dateComp = nil;
  385. if (dateString && dateString.length == 6) {//19990101 타입
  386. dateComp = [[NSDateComponents alloc] init];
  387. dateComp.year = [[dateString substringToIndex:4] intValue];
  388. dateComp.month = [[dateString substringWithRange:NSMakeRange(4, 2)] intValue];
  389. } else if (dateString && dateString.length == 8) {//19990101 타입
  390. dateComp = [[NSDateComponents alloc] init];
  391. dateComp.year = [[dateString substringToIndex:4] intValue];
  392. dateComp.month = [[dateString substringWithRange:NSMakeRange(4, 2)] intValue];
  393. dateComp.day = [[dateString substringWithRange:NSMakeRange(6, 2)] intValue];
  394. } else if (dateString && dateString.length == 14) {//19990101010101 타입
  395. dateComp = [[NSDateComponents alloc] init];
  396. dateComp.year = [[dateString substringToIndex:4] intValue];
  397. dateComp.month = [[dateString substringWithRange:NSMakeRange(4, 2)] intValue];
  398. dateComp.day = [[dateString substringWithRange:NSMakeRange(6, 2)] intValue];
  399. dateComp.hour = [[dateString substringWithRange:NSMakeRange(8, 2)] intValue];
  400. dateComp.minute = [[dateString substringWithRange:NSMakeRange(10, 2)] intValue];
  401. dateComp.second = [[dateString substringWithRange:NSMakeRange(12, 2)] intValue];
  402. } else if ([dateString rangeOfString:@"-"].location != NSNotFound) {
  403. NSArray *dateArray = [dateString componentsSeparatedByString:@"-"];
  404. if (dateArray && dateArray.count > 0) {
  405. dateComp = [[NSDateComponents alloc] init];
  406. dateComp.year = dateArray.count >= 1 ? [dateArray[0] intValue] : 0;
  407. dateComp.month = dateArray.count >= 2 ? [dateArray[1] intValue] : 0;
  408. dateComp.day = dateArray.count >= 3 ? [dateArray[2] intValue] : 0;
  409. }
  410. } else if ([dateString rangeOfString:@"/"].location != NSNotFound) {
  411. NSArray *dateArray = [dateString componentsSeparatedByString:@"/"];
  412. if (dateArray && dateArray.count > 0) {
  413. dateComp = [[NSDateComponents alloc] init];
  414. dateComp.year = dateArray.count >= 1 ? [dateArray[0] intValue] : 0;
  415. dateComp.month = dateArray.count >= 2 ? [dateArray[1] intValue] : 0;
  416. dateComp.day = dateArray.count >= 3 ? [dateArray[2] intValue] : 0;
  417. }
  418. } else if ([dateString rangeOfString:@"."].location != NSNotFound) {
  419. NSArray *dateArray = [dateString componentsSeparatedByString:@"."];
  420. if (dateArray && dateArray.count > 0) {
  421. dateComp = [[NSDateComponents alloc] init];
  422. dateComp.year = dateArray.count >= 1 ? [dateArray[0] intValue] : 0;
  423. dateComp.month = dateArray.count >= 2 ? [dateArray[1] intValue] : 0;
  424. dateComp.day = dateArray.count >= 3 ? [dateArray[2] intValue] : 0;
  425. }
  426. }
  427. return dateComp;
  428. }
  429. + (NSDateComponents *)dateComponentsFromUTCString:(NSString *)utcString {
  430. NSDateFormatter* df_local = [[CommonUtil dateFormatter] copy];
  431. [df_local setTimeZone:[NSTimeZone systemTimeZone]];
  432. [df_local setDateFormat:@"yyyyMMddHHmmssSSS"];
  433. NSDate *locTime = [df_local dateFromString:utcString];
  434. return [CommonUtil dateComponents:locTime];
  435. }
  436. + (NSDate *)dateFromDateString:(NSString *)dateString {
  437. NSDateComponents *ds = [CommonUtil dateComponentsFromString:dateString];
  438. NSDate *newDate = [[CommonUtil calendar] dateFromComponents:ds];
  439. return newDate;
  440. }
  441. + (NSString *)stringFromDateComponents:(NSDateComponents *)components {
  442. NSMutableString *rString = [NSMutableString new];
  443. if (components.year > 0) {
  444. [rString appendString:[NSString stringWithFormat:@"%zd년", (NSInteger)components.year]];
  445. }
  446. if (components.month > 0) {
  447. if (rString.length > 0) {
  448. [rString appendString:@" "];
  449. }
  450. [rString appendString:[NSString stringWithFormat:@"%zd월", (NSInteger)components.month]];
  451. }
  452. if (components.day > 0) {
  453. if (rString.length > 0) {
  454. [rString appendString:@" "];
  455. }
  456. [rString appendString:[NSString stringWithFormat:@"%zd일", (NSInteger)components.day]];
  457. }
  458. return rString;
  459. }
  460. + (NSString *)stringFromDateComponents2:(NSDateComponents *)components {
  461. NSString *month = components.month < 10 ? [NSString stringWithFormat:@"0%zd", components.month] : [NSString stringWithFormat:@"%zd", components.month];
  462. NSString *day = components.day < 10 ? [NSString stringWithFormat:@"0%zd", components.day] : [NSString stringWithFormat:@"%zd", components.day];
  463. NSString *rString = [NSString stringWithFormat:@"%zd%@%@", components.year, month, day];
  464. return rString;
  465. }
  466. + (NSString *)stringYearMonthFromDateComponents:(NSDateComponents *)components {
  467. NSMutableString *rString = [NSMutableString new];
  468. if (components.year > 0) {
  469. [rString appendString:[NSString stringWithFormat:@"%zd.", (NSInteger)components.year]];
  470. }
  471. if (components.month > 0) {
  472. if (components.month < 10) {
  473. [rString appendString:[NSString stringWithFormat:@"0%zd", (NSInteger)components.month]];
  474. } else {
  475. [rString appendString:[NSString stringWithFormat:@"%zd", (NSInteger)components.month]];
  476. }
  477. }
  478. return rString;
  479. }
  480. + (NSString *)paramMonthFromDateString:(NSString *)dateString {
  481. NSDateComponents *ds = [CommonUtil dateComponentsFromString:dateString];
  482. ds.day = 1;
  483. NSDateFormatter *df = [CommonUtil dateFormatter];
  484. df.dateFormat = @"YYYYMM";
  485. NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
  486. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  487. }
  488. + (NSDate *)dateFrom:(NSDate *)date adjustDay:(NSInteger)day {
  489. NSDateComponents *ds = [CommonUtil dateComponents:date];
  490. ds.day += day;
  491. NSDate *adjustDate = [[CommonUtil calendar] dateFromComponents:ds];
  492. return adjustDate;
  493. }
  494. //현재 로컬시간을 UTC로 변환합니다.
  495. + (NSString *)UTCDateStringFromLocal:(NSString *)locDateString {
  496. NSDateFormatter *df = [CommonUtil dateFormatter];
  497. [df setDateFormat:@"yyyyMMddHHmmss"];
  498. NSDate *locDate = [df dateFromString:locDateString];
  499. NSDateFormatter *dfu = [[CommonUtil dateFormatter] copy];
  500. dfu.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS";
  501. NSTimeZone *utc = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
  502. [dfu setTimeZone:utc];
  503. NSString *timeStamp = [dfu stringFromDate:locDate];
  504. timeStamp = [NSString stringWithFormat:@"%@Z",timeStamp];
  505. return timeStamp;
  506. }
  507. //UTC 시간을 현재 로컬 시간을 변환합니다.
  508. + (NSString *)localDateFromUTC:(NSString *)utcDateString {
  509. NSDateFormatter *df = [CommonUtil dateFormatter];
  510. [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
  511. NSDate *utcDate = [df dateFromString:utcDateString];
  512. NSDateFormatter* dfl = [[CommonUtil dateFormatter] copy];
  513. [dfl setTimeZone:[NSTimeZone systemTimeZone]];
  514. [dfl setDateFormat:@"yyyyMMddHHmmss"];
  515. NSString* localString = [dfl stringFromDate:utcDate];
  516. return localString;
  517. }
  518. + (NSString *)localDateFromUTC2:(NSString *)utcDateString {
  519. NSDateFormatter *df = [[CommonUtil dateFormatter] copy];
  520. NSTimeZone *utc = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
  521. [df setTimeZone:utc];
  522. [df setDateFormat:@"yyyyMMddHHmmss"];
  523. NSDate *utcDate = [df dateFromString:utcDateString];
  524. NSDateFormatter* dfl = [[CommonUtil dateFormatter] copy];
  525. [dfl setTimeZone:[NSTimeZone systemTimeZone]];
  526. [dfl setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  527. NSString* localString = [dfl stringFromDate:utcDate];
  528. return localString;
  529. }
  530. + (NSString *)formattedDate:(NSString *)dateString {
  531. NSDateComponents *ds = [CommonUtil dateComponentsFromString:dateString];
  532. NSDateFormatter *df = [CommonUtil dateFormatter];
  533. df.dateFormat = @"YYYY년 M월 d일";
  534. NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
  535. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  536. }
  537. + (NSString *)formattedDate2:(NSString *)dateString {
  538. NSDateComponents *ds = [CommonUtil dateComponentsFromString:dateString];
  539. NSDateFormatter *df = [CommonUtil dateFormatter];
  540. df.dateFormat = @"YYYY.MM.dd";
  541. NSDate *date = [[CommonUtil calendar]dateFromComponents:ds];
  542. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  543. }
  544. //기본 날짜포맷을 반환
  545. + (NSString *)formattedDate3:(NSString *)datetime {
  546. NSDateComponents *ds = [CommonUtil dateComponentsFromString:datetime];
  547. NSDateFormatter *df = [CommonUtil dateFormatter];
  548. df.dateFormat = @"YYYY년 MM월 dd일";
  549. NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
  550. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  551. }
  552. + (NSString *)formattedMonthDate:(NSString *)dateString {
  553. NSDateComponents *ds = [CommonUtil dateComponentsFromString:dateString];
  554. NSDateFormatter *df = [CommonUtil dateFormatter];
  555. df.dateFormat = @"YYYY.MM";
  556. NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
  557. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  558. }
  559. #pragma mark - TimeUtil
  560. + (NSString *)formattedTime:(NSString *)timeString {
  561. NSDateComponents *ds = [CommonUtil timeComponentsFromString:timeString];
  562. NSDateFormatter *df = [CommonUtil dateFormatter];
  563. df.dateFormat = @"HH시 mm분";
  564. NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
  565. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  566. }
  567. + (NSString *)formattedTime2:(NSString *)timeString {
  568. NSDateComponents *ds = [CommonUtil timeComponentsFromString:timeString];
  569. NSDateFormatter *df = [CommonUtil dateFormatter];
  570. df.dateFormat = @"HH시 mm분";
  571. NSString *period = ds.hour < 12 ? @"오전" : @"오후";
  572. if ([period isEqualToString:@"오후"]) {
  573. ds.hour -= 12;
  574. }
  575. NSDate *date = [[CommonUtil calendar] dateFromComponents:ds];
  576. return [NSString stringWithFormat:@"%@ %@", period, [df stringFromDate:date]];
  577. }
  578. + (NSDateComponents *)timeComponentsFromString:(NSString *)timeString {
  579. NSDateComponents *dateComp = [CommonUtil dateComponents:[NSDate date]];
  580. if (timeString && [timeString rangeOfString:@"시 "].location != NSNotFound) {//00시 00분 타입
  581. NSArray *timeArray = [timeString componentsSeparatedByString:@" "]; //시와 분을 나눔
  582. NSString *hour = [timeArray firstObject];
  583. hour = [hour stringByReplacingOccurrencesOfString:@"시" withString:@""];
  584. NSString *minute = [timeArray lastObject];
  585. minute = [minute stringByReplacingOccurrencesOfString:@"분" withString:@""];
  586. dateComp.hour = [hour intValue];
  587. dateComp.minute = [minute intValue];
  588. } else if ([timeString rangeOfString:@":"].location != NSNotFound) {
  589. NSArray *timeArray = [timeString componentsSeparatedByString:@":"];
  590. if (timeArray && timeArray.count > 0) {
  591. dateComp.hour = timeArray.count >= 1 ? [timeArray[0] intValue] : 0;
  592. dateComp.minute = timeArray.count >= 2 ? [timeArray[1] intValue] : 0;
  593. }
  594. }
  595. return dateComp;
  596. }
  597. + (NSString *)stringFromTime:(NSDate *)date {
  598. NSDateFormatter *df = [CommonUtil dateFormatter];
  599. df.dateFormat = @"HH:mm";
  600. return [NSString stringWithFormat:@"%@", [df stringFromDate:date]];
  601. }
  602. #pragma mark - StringUtil
  603. + (NSNumberFormatter *)numberFormatter {
  604. static NSNumberFormatter *__shareNumberFormatter = nil;
  605. static dispatch_once_t onceToken;
  606. dispatch_once(&onceToken, ^{
  607. __shareNumberFormatter = [[NSNumberFormatter alloc] init];
  608. NSLocale *locale = [NSLocale currentLocale];
  609. [__shareNumberFormatter setLocale:locale];
  610. });
  611. return __shareNumberFormatter;
  612. }
  613. + (NSString *)numberString:(NSInteger)number {
  614. NSNumberFormatter *formatter = [CommonUtil numberFormatter];
  615. formatter.numberStyle = NSNumberFormatterDecimalStyle;
  616. formatter.usesGroupingSeparator = YES;
  617. [formatter setGroupingSeparator:@","];
  618. NSString *formattedString = [NSString stringWithFormat:@"%@", [formatter stringFromNumber:[NSNumber numberWithInteger:number]]];
  619. return formattedString;
  620. }
  621. + (NSString *)currencyString:(NSInteger)number {
  622. NSString *currency = [NSString stringWithFormat:@"%@ 원", @"0"];
  623. if (number <= 0) {
  624. return currency;
  625. }
  626. NSNumberFormatter *formatter = [CommonUtil numberFormatter];
  627. formatter.numberStyle = NSNumberFormatterDecimalStyle;
  628. formatter.usesGroupingSeparator = YES;
  629. [formatter setGroupingSeparator:@","];
  630. NSString *formattedString = [NSString stringWithFormat:@"%@", [formatter stringFromNumber:[NSNumber numberWithInteger:number]]];
  631. currency = [NSString stringWithFormat:@"%@ 원", formattedString];
  632. return currency;
  633. }
  634. + (NSString *)currencyString2:(NSInteger)number {
  635. NSString *currency = [NSString stringWithFormat:@"%@원", @"0"];
  636. if (number <= 0) {
  637. return currency;
  638. }
  639. NSNumberFormatter *formatter = [CommonUtil numberFormatter];
  640. formatter.numberStyle = NSNumberFormatterDecimalStyle;
  641. formatter.usesGroupingSeparator = YES;
  642. [formatter setGroupingSeparator:@","];
  643. NSString *formattedString = [NSString stringWithFormat:@"%@", [formatter stringFromNumber:[NSNumber numberWithInteger:number]]];
  644. currency = [NSString stringWithFormat:@"%@원", formattedString];
  645. return currency;
  646. }
  647. + (NSString *)svcNoString:(NSString *)svcNo {
  648. //0103333333
  649. NSMutableString *tmpSvcNo = [NSMutableString stringWithString:svcNo];
  650. if (svcNo.length == 10) {
  651. [tmpSvcNo insertString:@"-" atIndex:3];
  652. [tmpSvcNo insertString:@"-" atIndex:7];
  653. } else if (svcNo.length == 11) {
  654. [tmpSvcNo insertString:@"-" atIndex:3];
  655. [tmpSvcNo insertString:@"-" atIndex:8];
  656. }
  657. return tmpSvcNo;
  658. }
  659. + (NSString *)cutSaName:(NSString *)saName {
  660. NSRange range = [saName rangeOfString:@"]"];
  661. NSString *rSaName = nil;
  662. if (range.location != NSNotFound) {
  663. rSaName = [saName substringWithRange:NSMakeRange(1, range.location-1)];
  664. }
  665. return rSaName;
  666. }
  667. + (NSInteger)getIntegerFromCurrency:(NSString *)currency {
  668. NSInteger rvalue = 0;
  669. NSString *tmp = [NSString stringWithString:currency];
  670. NSRange fRange = [currency rangeOfString:NSLocalizedString(@"Won", @"원")];
  671. if (fRange.location == currency.length-1) {
  672. tmp = [currency stringByReplacingOccurrencesOfString:NSLocalizedString(@"Won", @"원") withString:ksEmptyString];
  673. }
  674. tmp = [tmp stringByReplacingOccurrencesOfString:@"," withString:ksEmptyString];
  675. rvalue = [tmp intValue];
  676. return rvalue;
  677. }
  678. + (NSString *)formattedPhoneNumber:(NSString *)phoneNumber {
  679. if ([phoneNumber rangeOfString:@"-"].location != NSNotFound) {//이미 포맷에 맞춰진 경우는 리턴
  680. return phoneNumber;
  681. }
  682. NSMutableString *rString = [NSMutableString stringWithString:phoneNumber];
  683. NSInteger lastIndex = phoneNumber.length - 3;
  684. [rString insertString:@"-" atIndex:3];
  685. [rString insertString:@"-" atIndex:lastIndex];
  686. return rString;
  687. }
  688. + (NSString *)formattedIAId:(NSString *)iaId {
  689. NSMutableString *formatIAId = [NSMutableString stringWithString:iaId];
  690. [formatIAId replaceCharactersInRange:NSMakeRange(formatIAId.length-3, 3) withString:@"***"];
  691. return formatIAId;
  692. }
  693. + (NSInteger)countForOccurrencesOfString:(NSString *)occurString {
  694. NSInteger count = 0, length = [occurString length];
  695. NSRange range = NSMakeRange(0, length);
  696. while(range.location != NSNotFound) {
  697. range = [occurString rangeOfString:occurString options:0 range:range];
  698. if(range.location != NSNotFound) {
  699. range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
  700. count++;
  701. }
  702. }
  703. return count;
  704. }
  705. +(NSString *)URLEncoding:(NSString *)sender{
  706. NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
  707. NULL,
  708. (CFStringRef)sender,
  709. NULL,
  710. (CFStringRef)@"!*'();:@&=+$,/?%#[]",
  711. kCFStringEncodingUTF8 ));
  712. return encodedString;
  713. }
  714. +(NSString *)URLDecoding:(NSString *)sender {
  715. // Decode a percent escape encoded string.
  716. NSString *decodeString = (NSString *)CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL,
  717. (CFStringRef) sender,
  718. CFSTR(""),
  719. kCFStringEncodingUTF8));
  720. return decodeString;
  721. }
  722. #pragma getUrlPostUrl
  723. + (NSString *)switchPostUrl:(NSString *)getUrl{
  724. NSArray *urlLst = [getUrl componentsSeparatedByString:@"?"];
  725. NSString *splturl = [urlLst objectAtIndex:1];
  726. return splturl;
  727. }
  728. + (NSString *)getUrl:(NSString *)getUrl{
  729. NSString *url;
  730. NSArray *urlLst = [getUrl componentsSeparatedByString:@"?"];
  731. url = [urlLst objectAtIndex:0];
  732. return url;
  733. }
  734. + (BOOL)stringContainsEmoji:(NSString *)string {
  735. __block BOOL returnValue = NO;
  736. [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
  737. ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
  738. const unichar hs = [substring characterAtIndex:0];
  739. // surrogate pair
  740. if (0xd800 <= hs && hs <= 0xdbff) {
  741. if (substring.length > 1) {
  742. const unichar ls = [substring characterAtIndex:1];
  743. const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
  744. if (0x1d000 <= uc && uc <= 0x1f77f) {
  745. returnValue = YES;
  746. }
  747. }
  748. } else if (substring.length > 1) {
  749. const unichar ls = [substring characterAtIndex:1];
  750. if (ls == 0x20e3) {
  751. returnValue = YES;
  752. }
  753. } else {
  754. // non surrogate
  755. if (0x2100 <= hs && hs <= 0x27ff) {
  756. returnValue = YES;
  757. } else if (0x2B05 <= hs && hs <= 0x2b07) {
  758. returnValue = YES;
  759. } else if (0x2934 <= hs && hs <= 0x2935) {
  760. returnValue = YES;
  761. } else if (0x3297 <= hs && hs <= 0x3299) {
  762. returnValue = YES;
  763. } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
  764. returnValue = YES;
  765. }
  766. }
  767. }];
  768. return returnValue;
  769. }
  770. #pragma mark - TimeZone Dictionary
  771. + (SortDictionary *)timeZoneDict {
  772. NSString *plist = [[NSBundle mainBundle] pathForResource:@"TimeZoneCode" ofType:@"plist"];
  773. NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithContentsOfFile:plist];
  774. return [[SortDictionary alloc] initWithDictionary:dic];
  775. }
  776. #pragma mark - Color
  777. +(UIColor *)convertFromRGBToUIColor:(NSString *)hexRGB {
  778. NSString *cleanString = [hexRGB stringByReplacingOccurrencesOfString:@"#" withString:@""];
  779. if ([cleanString length] == 3) {
  780. cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@", [cleanString substringWithRange:NSMakeRange(0, 1)], [cleanString substringWithRange:NSMakeRange(0, 1)],
  781. [cleanString substringWithRange:NSMakeRange(1, 1)], [cleanString substringWithRange:NSMakeRange(1, 1)],
  782. [cleanString substringWithRange:NSMakeRange(2, 1)], [cleanString substringWithRange:NSMakeRange(2, 1)]];
  783. }
  784. if ([cleanString length] == 6) {
  785. cleanString = [cleanString stringByAppendingString:@"ff"];
  786. }
  787. unsigned int baseValue;
  788. [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
  789. float red = ((baseValue >> 24) & 0xFF) / 255.0f;
  790. float green = ((baseValue >> 16) & 0xFF) / 255.0f;
  791. float blue = ((baseValue >> 8) & 0xFF) / 255.0f;
  792. float alpha = ((baseValue >> 0) & 0xFF) / 255.0f;
  793. return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
  794. }
  795. @end