IBTCommon.m 8.15 KB
Newer Older
mei's avatar
mei committed
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
//
//  IBTCommon.m
//  AceMTer
//
//  Created by Xummer on 2/27/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "IBTCommon.h"
#import "ICRUserUtil.h"

@implementation IBTFileData

@end

@implementation IBTCommon

+ (NSString *)localizableString:(NSString *)text
{
    /*
     NSString *settingLanguge = [[TWDocument unarchive] language];
     
     NSString *directoryName = [NSString stringWithString:settingLanguge];
     directoryName = [directoryName stringByAppendingString:@".lproj"];
     NSString *tableName = [directoryName stringByAppendingPathComponent:@"Localizable"];
     text = NSLocalizedStringFromTable(text, tableName, text);
     */
    
    return NSLocalizedString(text, nil);
}

+ (UIImage *)localizableImage:(NSString *)name
{
    UIImage *image = nil;
    /*
     NSString *settingLanguge = [[TWDocument unarchive] language];
     
     NSString *directoryName = [NSString stringWithString:settingLanguge];
     directoryName = [directoryName stringByAppendingString:@".lproj"];
     NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:nil inDirectory:directoryName];
     image = [UIImage imageWithContentsOfFile:filePath];
     */
    return image;
}

+ (UIImage *)appIcon {
    /*
     CFBundleIcons =     {
     CFBundlePrimaryIcon =         {
     CFBundleIconFiles =             (
     AppIcon29x29,
     AppIcon40x40,
     AppIcon57x57,
     AppIcon60x60,
     AppIcon120x120
     );
     };
     };
     */
    
    NSArray *arrIcons = [[NSBundle mainBundle] infoDictionary][ @"CFBundleIcons" ][ @"CFBundlePrimaryIcon" ][ @"CFBundleIconFiles" ];
    UIImage *appIcon = nil;
    NSUInteger uiIconCount = [arrIcons count];
    for (NSInteger i = uiIconCount - 1; i >= 0; i -- ) {
        appIcon = [UIImage imageNamed:arrIcons[ i ]];
        if (appIcon) {
            break;
        }
    }
    
    return appIcon;
}

+ (BOOL)isLegalString:(NSString *)string WithRegex:(NSString *)regexStr
{
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:0 error:&error];
    if (string != nil && regex != nil) {
        NSTextCheckingResult *firstMatch = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
        if (firstMatch) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - Thread
+ (void)runOnMainThreadWithoutDeadlocking:(void (^)(void))block {
    if ([NSThread isMainThread]) {
        block();
    }
    else{
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

+ (NSString *)archivePathForCurrentUser {
    ICRUserUtil *userUtil = [ICRUserUtil sharedInstance];
    NSString *userName = [userUtil userName];
    if (!userName.length) return nil;
    
    NSString *orgCode = [userUtil orgCode];
    if (!orgCode.length) {
        return nil;
    }
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
    
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    if (appName == nil) {
        appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleName"];
        
        if (appName == nil) appName = @"XFFruit";
    }
    
    NSString *result = [basePath stringByAppendingPathComponent:appName];
    if (result != nil) {
        result = [result stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@", orgCode, userName]];
    }
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:result]) {
        [fileManager createDirectoryAtPath:result
               withIntermediateDirectories:YES
                                attributes:nil
                                     error:nil];
    }
    
    return result;
}

+ (NSString *)archivePathForTmpImages {
    NSString *archivePath = [[self class] archivePathForCurrentUser];
    if (!archivePath.length) {
        return nil;
    }
    
    NSString *dirName = @"images";
    NSString *result = [archivePath stringByAppendingPathComponent:dirName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:result]) {
        [fileManager createDirectoryAtPath:result
               withIntermediateDirectories:YES
                                attributes:nil
                                     error:nil];
    }
    
    return result;
}

+ (IBTFileData *)saveImageToLocal:(UIImage *)imageToSave {
    NSString *imgName = [NSString stringWithFormat:@"ICRIMG-%@.JPG", [[NSDate date] YMDHMSFormatterString]];
    NSString *imgPath = [[IBTCommon archivePathForTmpImages] stringByAppendingPathComponent:imgName];
    NSData *imgData = UIImageJPEGRepresentation(imageToSave, .6f);
    
    if ([imgData writeToFile:imgPath atomically:YES])
    {
        IBTFileData *fileData = [[IBTFileData alloc] init];
        fileData.fileName = imgName;
        fileData.filePath = imgPath;
        fileData.fileData = imgData;
        return fileData;
    }
    else {
        return nil;
    }
}

陈俊俊's avatar
陈俊俊 committed
173 174 175 176 177 178 179
+ (NSDate *)convertToDateFrom:(NSString *)dateString{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy.MM.dd"];
    return [formatter dateFromString:dateString];
}
+ (NSString *)stringFromDate:(NSDate*)aDate{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
陈俊俊's avatar
陈俊俊 committed
180
    [formatter setDateFormat:@"yyyy-MM-dd"];
陈俊俊's avatar
陈俊俊 committed
181 182 183
    NSString *dateString = [formatter stringFromDate:aDate];
    return dateString;
}
陈俊俊's avatar
陈俊俊 committed
184 185 186 187 188 189
+ (NSString *)stringFromDateWithFormat:(NSDate*)aDate format:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSString *dateString = [formatter stringFromDate:aDate];
    return dateString;
}
陈俊俊's avatar
陈俊俊 committed
190

陈俊俊's avatar
陈俊俊 committed
191 192 193 194 195 196 197 198 199 200 201
+ (NSString*)dictionaryToJson:(id)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}
n22's avatar
n22 committed
202 203 204 205 206
+ (NSString *)checkString:(NSString *)str
{
    
    return (str)?str:@"";
}
207 208 209 210 211
+ (NSString *)checkNull:(id)data {

    return data == [NSNull null] ? @"" : data;
  
}
freecui's avatar
freecui committed
212 213 214 215 216 217 218
+ (BOOL)checkStringIsNilOrSpance:(NSString *)str {
    if (!str || [str isEqualToString:@"" ] || [str isEqual:[NSNull null]]) {
        return YES;
    } else {
        return NO;
    }
}
n22's avatar
n22 committed
219 220 221 222 223 224 225
+ (NSMutableAttributedString *)setTextViewFontOfString:(NSString *)string paragraphStyle:(NSInteger)lineHeight fontSize:(float)size color:(UIColor *)color {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing = lineHeight;
    NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:size],NSForegroundColorAttributeName :color, NSParagraphStyleAttributeName:paragraphStyle};
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:string attributes:attributes];
    return attributeStr;
}
陈俊俊's avatar
陈俊俊 committed
226

陈俊俊's avatar
陈俊俊 committed
227 228 229 230
+ (NSString *)trimmingCharacters:(NSString *)str{
    NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return newStr;
}
陈俊俊's avatar
陈俊俊 committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
+ (int)compareDate:(NSString*)oneDate withDate:(NSString*)twoDate{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt2 = [[NSDate alloc] init];
    dt1 = [df dateFromString:oneDate];
    dt2 = [df dateFromString:twoDate];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending: ci=1; break;
            //date02比date01小
        case NSOrderedDescending: ci=-1; break;
            //date02=date01
        case NSOrderedSame: ci=0; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
    return ci;
}
陈俊俊's avatar
陈俊俊 committed
252 253 254 255 256 257 258 259
+ (BOOL)checkIsPermission:(NSString *)permission{
    for (NSString *per in [ICRUserUtil sharedInstance].permissions) {
        if ([per isEqualToString:permission]) {
            return YES;
        }
    }
    return NO;
}
陈俊俊's avatar
陈俊俊 committed
260

mei's avatar
mei committed
261
@end