FLEXUtility.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // FLEXUtility.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/18/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXUtility.h"
  9. #import "FLEXResources.h"
  10. #import <ImageIO/ImageIO.h>
  11. #import <zlib.h>
  12. #import <objc/runtime.h>
  13. @implementation FLEXUtility
  14. + (UIColor *)consistentRandomColorForObject:(id)object
  15. {
  16. CGFloat hue = (((NSUInteger)object >> 4) % 256) / 255.0;
  17. return [UIColor colorWithHue:hue saturation:1.0 brightness:1.0 alpha:1.0];
  18. }
  19. + (NSString *)descriptionForView:(UIView *)view includingFrame:(BOOL)includeFrame
  20. {
  21. NSString *description = [[view class] description];
  22. NSString *viewControllerDescription = [[[self viewControllerForView:view] class] description];
  23. if ([viewControllerDescription length] > 0) {
  24. description = [description stringByAppendingFormat:@" (%@)", viewControllerDescription];
  25. }
  26. if (includeFrame) {
  27. description = [description stringByAppendingFormat:@" %@", [self stringForCGRect:view.frame]];
  28. }
  29. if ([view.accessibilityLabel length] > 0) {
  30. description = [description stringByAppendingFormat:@" · %@", view.accessibilityLabel];
  31. }
  32. return description;
  33. }
  34. + (NSString *)stringForCGRect:(CGRect)rect
  35. {
  36. return [NSString stringWithFormat:@"{(%g, %g), (%g, %g)}", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height];
  37. }
  38. + (UIViewController *)viewControllerForView:(UIView *)view
  39. {
  40. UIViewController *viewController = nil;
  41. SEL viewDelSel = NSSelectorFromString([NSString stringWithFormat:@"%@ewDelegate", @"_vi"]);
  42. if ([view respondsToSelector:viewDelSel]) {
  43. #pragma clang diagnostic push
  44. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  45. viewController = [view performSelector:viewDelSel];
  46. #pragma clang diagnostic pop
  47. }
  48. return viewController;
  49. }
  50. + (NSString *)detailDescriptionForView:(UIView *)view
  51. {
  52. return [NSString stringWithFormat:@"frame %@", [self stringForCGRect:view.frame]];
  53. }
  54. + (UIImage *)circularImageWithColor:(UIColor *)color radius:(CGFloat)radius
  55. {
  56. CGFloat diameter = radius * 2.0;
  57. UIGraphicsBeginImageContextWithOptions(CGSizeMake(diameter, diameter), NO, 0.0);
  58. CGContextRef imageContext = UIGraphicsGetCurrentContext();
  59. CGContextSetFillColorWithColor(imageContext, [color CGColor]);
  60. CGContextFillEllipseInRect(imageContext, CGRectMake(0, 0, diameter, diameter));
  61. UIImage *circularImage = UIGraphicsGetImageFromCurrentImageContext();
  62. UIGraphicsEndImageContext();
  63. return circularImage;
  64. }
  65. + (UIColor *)scrollViewGrayColor
  66. {
  67. return [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:244.0/255.0 alpha:1.0];
  68. }
  69. + (UIColor *)hierarchyIndentPatternColor
  70. {
  71. static UIColor *patternColor = nil;
  72. static dispatch_once_t onceToken;
  73. dispatch_once(&onceToken, ^{
  74. UIImage *indentationPatternImage = [FLEXResources hierarchyIndentPattern];
  75. patternColor = [UIColor colorWithPatternImage:indentationPatternImage];
  76. });
  77. return patternColor;
  78. }
  79. + (NSString *)applicationImageName
  80. {
  81. return [[NSBundle mainBundle] executablePath];
  82. }
  83. + (NSString *)applicationName
  84. {
  85. return [[[FLEXUtility applicationImageName] componentsSeparatedByString:@"/"] lastObject];
  86. }
  87. + (NSString *)safeDescriptionForObject:(id)object
  88. {
  89. // Don't assume that we have an NSObject subclass.
  90. // Check to make sure the object responds to the description methods.
  91. NSString *description = nil;
  92. if ([object respondsToSelector:@selector(debugDescription)]) {
  93. description = [object debugDescription];
  94. } else if ([object respondsToSelector:@selector(description)]) {
  95. description = [object description];
  96. }
  97. return description;
  98. }
  99. + (UIFont *)defaultFontOfSize:(CGFloat)size
  100. {
  101. return [UIFont fontWithName:@"HelveticaNeue" size:size];
  102. }
  103. + (UIFont *)defaultTableViewCellLabelFont
  104. {
  105. return [self defaultFontOfSize:12.0];
  106. }
  107. + (NSString *)stringByEscapingHTMLEntitiesInString:(NSString *)originalString
  108. {
  109. static NSDictionary *escapingDictionary = nil;
  110. static NSRegularExpression *regex = nil;
  111. static dispatch_once_t onceToken;
  112. dispatch_once(&onceToken, ^{
  113. escapingDictionary = @{ @" " : @"&nbsp;",
  114. @">" : @"&gt;",
  115. @"<" : @"&lt;",
  116. @"&" : @"&amp;",
  117. @"'" : @"&apos;",
  118. @"\"" : @"&quot;",
  119. @"«" : @"&laquo;",
  120. @"»" : @"&raquo;"
  121. };
  122. regex = [NSRegularExpression regularExpressionWithPattern:@"(&|>|<|'|\"|«|»)" options:0 error:NULL];
  123. });
  124. NSMutableString *mutableString = [originalString mutableCopy];
  125. NSArray *matches = [regex matchesInString:mutableString options:0 range:NSMakeRange(0, [mutableString length])];
  126. for (NSTextCheckingResult *result in [matches reverseObjectEnumerator]) {
  127. NSString *foundString = [mutableString substringWithRange:result.range];
  128. NSString *replacementString = escapingDictionary[foundString];
  129. if (replacementString) {
  130. [mutableString replaceCharactersInRange:result.range withString:replacementString];
  131. }
  132. }
  133. return [mutableString copy];
  134. }
  135. + (UIInterfaceOrientationMask)infoPlistSupportedInterfaceOrientationsMask
  136. {
  137. NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
  138. UIInterfaceOrientationMask supportedOrientationsMask = 0;
  139. if ([supportedOrientations containsObject:@"UIInterfaceOrientationPortrait"]) {
  140. supportedOrientationsMask |= UIInterfaceOrientationMaskPortrait;
  141. }
  142. if ([supportedOrientations containsObject:@"UIInterfaceOrientationMaskLandscapeRight"]) {
  143. supportedOrientationsMask |= UIInterfaceOrientationMaskLandscapeRight;
  144. }
  145. if ([supportedOrientations containsObject:@"UIInterfaceOrientationMaskPortraitUpsideDown"]) {
  146. supportedOrientationsMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  147. }
  148. if ([supportedOrientations containsObject:@"UIInterfaceOrientationLandscapeLeft"]) {
  149. supportedOrientationsMask |= UIInterfaceOrientationMaskLandscapeLeft;
  150. }
  151. return supportedOrientationsMask;
  152. }
  153. + (NSString *)searchBarPlaceholderText
  154. {
  155. return @"Filter";
  156. }
  157. + (BOOL)isImagePathExtension:(NSString *)extension
  158. {
  159. // https://developer.apple.com/library/ios/documentation/uikit/reference/UIImage_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006890-CH3-SW3
  160. return [@[@"jpg", @"jpeg", @"png", @"gif", @"tiff", @"tif"] containsObject:extension];
  161. }
  162. + (UIImage *)thumbnailedImageWithMaxPixelDimension:(NSInteger)dimension fromImageData:(NSData *)data
  163. {
  164. UIImage *thumbnail = nil;
  165. CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, 0);
  166. if (imageSource) {
  167. NSDictionary *options = @{ (__bridge id)kCGImageSourceCreateThumbnailWithTransform : @YES,
  168. (__bridge id)kCGImageSourceCreateThumbnailFromImageAlways : @YES,
  169. (__bridge id)kCGImageSourceThumbnailMaxPixelSize : @(dimension) };
  170. CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options);
  171. if (scaledImageRef) {
  172. thumbnail = [UIImage imageWithCGImage:scaledImageRef];
  173. CFRelease(scaledImageRef);
  174. }
  175. CFRelease(imageSource);
  176. }
  177. return thumbnail;
  178. }
  179. + (NSString *)stringFromRequestDuration:(NSTimeInterval)duration
  180. {
  181. NSString *string = @"0s";
  182. if (duration > 0.0) {
  183. if (duration < 1.0) {
  184. string = [NSString stringWithFormat:@"%dms", (int)(duration * 1000)];
  185. } else if (duration < 10.0) {
  186. string = [NSString stringWithFormat:@"%.2fs", duration];
  187. } else {
  188. string = [NSString stringWithFormat:@"%.1fs", duration];
  189. }
  190. }
  191. return string;
  192. }
  193. + (NSString *)statusCodeStringFromURLResponse:(NSURLResponse *)response
  194. {
  195. NSString *httpResponseString = nil;
  196. if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
  197. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
  198. NSString *statusCodeDescription = nil;
  199. if (httpResponse.statusCode == 200) {
  200. // Prefer OK to the default "no error"
  201. statusCodeDescription = @"OK";
  202. } else {
  203. statusCodeDescription = [NSHTTPURLResponse localizedStringForStatusCode:httpResponse.statusCode];
  204. }
  205. httpResponseString = [NSString stringWithFormat:@"%ld %@", (long)httpResponse.statusCode, statusCodeDescription];
  206. }
  207. return httpResponseString;
  208. }
  209. + (NSDictionary *)dictionaryFromQuery:(NSString *)query
  210. {
  211. NSMutableDictionary *queryDictionary = [NSMutableDictionary dictionary];
  212. // [a=1, b=2, c=3]
  213. NSArray *queryComponents = [query componentsSeparatedByString:@"&"];
  214. for (NSString *keyValueString in queryComponents) {
  215. // [a, 1]
  216. NSArray *components = [keyValueString componentsSeparatedByString:@"="];
  217. if ([components count] == 2) {
  218. NSString *key = [[components firstObject] stringByRemovingPercentEncoding];
  219. id value = [[components lastObject] stringByRemovingPercentEncoding];
  220. // Handle multiple entries under the same key as an array
  221. id existingEntry = queryDictionary[key];
  222. if (existingEntry) {
  223. if ([existingEntry isKindOfClass:[NSArray class]]) {
  224. value = [existingEntry arrayByAddingObject:value];
  225. } else {
  226. value = @[existingEntry, value];
  227. }
  228. }
  229. [queryDictionary setObject:value forKey:key];
  230. }
  231. }
  232. return queryDictionary;
  233. }
  234. + (NSString *)prettyJSONStringFromData:(NSData *)data
  235. {
  236. NSString *prettyString = nil;
  237. id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
  238. if ([NSJSONSerialization isValidJSONObject:jsonObject]) {
  239. prettyString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:jsonObject options:NSJSONWritingPrettyPrinted error:NULL] encoding:NSUTF8StringEncoding];
  240. // NSJSONSerialization escapes forward slashes. We want pretty json, so run through and unescape the slashes.
  241. prettyString = [prettyString stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
  242. } else {
  243. prettyString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  244. }
  245. return prettyString;
  246. }
  247. + (BOOL)isValidJSONData:(NSData *)data
  248. {
  249. return [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL] ? YES : NO;
  250. }
  251. // Thanks to the following links for help with this method
  252. // http://www.cocoanetics.com/2012/02/decompressing-files-into-memory/
  253. // https://github.com/nicklockwood/GZIP
  254. + (NSData *)inflatedDataFromCompressedData:(NSData *)compressedData
  255. {
  256. NSData *inflatedData = nil;
  257. NSUInteger compressedDataLength = [compressedData length];
  258. if (compressedDataLength > 0) {
  259. z_stream stream;
  260. stream.zalloc = Z_NULL;
  261. stream.zfree = Z_NULL;
  262. stream.avail_in = (uInt)compressedDataLength;
  263. stream.next_in = (void *)[compressedData bytes];
  264. stream.total_out = 0;
  265. stream.avail_out = 0;
  266. NSMutableData *mutableData = [NSMutableData dataWithLength:compressedDataLength * 1.5];
  267. if (inflateInit2(&stream, 15 + 32) == Z_OK) {
  268. int status = Z_OK;
  269. while (status == Z_OK) {
  270. if (stream.total_out >= [mutableData length]) {
  271. mutableData.length += compressedDataLength / 2;
  272. }
  273. stream.next_out = (uint8_t *)[mutableData mutableBytes] + stream.total_out;
  274. stream.avail_out = (uInt)([mutableData length] - stream.total_out);
  275. status = inflate(&stream, Z_SYNC_FLUSH);
  276. }
  277. if (inflateEnd(&stream) == Z_OK) {
  278. if (status == Z_STREAM_END) {
  279. mutableData.length = stream.total_out;
  280. inflatedData = [mutableData copy];
  281. }
  282. }
  283. }
  284. }
  285. return inflatedData;
  286. }
  287. + (SEL)swizzledSelectorForSelector:(SEL)selector
  288. {
  289. return NSSelectorFromString([NSString stringWithFormat:@"_flex_swizzle_%x_%@", arc4random(), NSStringFromSelector(selector)]);
  290. }
  291. + (BOOL)instanceRespondsButDoesNotImplementSelector:(SEL)selector class:(Class)cls
  292. {
  293. if ([cls instancesRespondToSelector:selector]) {
  294. unsigned int numMethods = 0;
  295. Method *methods = class_copyMethodList(cls, &numMethods);
  296. BOOL implementsSelector = NO;
  297. for (int index = 0; index < numMethods; index++) {
  298. SEL methodSelector = method_getName(methods[index]);
  299. if (selector == methodSelector) {
  300. implementsSelector = YES;
  301. break;
  302. }
  303. }
  304. free(methods);
  305. if (!implementsSelector) {
  306. return YES;
  307. }
  308. }
  309. return NO;
  310. }
  311. + (void)replaceImplementationOfKnownSelector:(SEL)originalSelector onClass:(Class)class withBlock:(id)block swizzledSelector:(SEL)swizzledSelector
  312. {
  313. // This method is only intended for swizzling methods that are know to exist on the class.
  314. // Bail if that isn't the case.
  315. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  316. if (!originalMethod) {
  317. return;
  318. }
  319. IMP implementation = imp_implementationWithBlock(block);
  320. class_addMethod(class, swizzledSelector, implementation, method_getTypeEncoding(originalMethod));
  321. Method newMethod = class_getInstanceMethod(class, swizzledSelector);
  322. method_exchangeImplementations(originalMethod, newMethod);
  323. }
  324. + (void)replaceImplementationOfSelector:(SEL)selector withSelector:(SEL)swizzledSelector forClass:(Class)cls withMethodDescription:(struct objc_method_description)methodDescription implementationBlock:(id)implementationBlock undefinedBlock:(id)undefinedBlock
  325. {
  326. if ([self instanceRespondsButDoesNotImplementSelector:selector class:cls]) {
  327. return;
  328. }
  329. IMP implementation = imp_implementationWithBlock((id)([cls instancesRespondToSelector:selector] ? implementationBlock : undefinedBlock));
  330. Method oldMethod = class_getInstanceMethod(cls, selector);
  331. if (oldMethod) {
  332. class_addMethod(cls, swizzledSelector, implementation, methodDescription.types);
  333. Method newMethod = class_getInstanceMethod(cls, swizzledSelector);
  334. method_exchangeImplementations(oldMethod, newMethod);
  335. } else {
  336. class_addMethod(cls, selector, implementation, methodDescription.types);
  337. }
  338. }
  339. @end