iOS 메모

6763 단어 iosobjective-c
설 연휴 동안 회사의 프로젝트 시간이 매우 바쁜 데다가 자신이 iOS의 작은 프로젝트를 하나 하고 있기 때문에 퇴근과 주말 시간에 거의 쉬지 않아서 블로그도 태만해졌다.
여기에는 내가 이 작은 프로젝트를 쓸 때 사용했던 코드 세션들을 조금 요약해 놓았다.첫째는 자신에게 메모를 하는 것이고, 둘째는 나누어 주는 것이 여러분에게도 도움이 되었으면 합니다.
// Data   
- (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];
    switch (c) {
        case 0xFF:
            return @"jpeg";
        case 0x89:
            return @"png";     
        case 0x47:
            return @"gif";        
        case 0x49:   
        case 0x4D:
            return @"tiff";        
        case 0x52:  
            if ([data length] < 12) {
                return nil;
            }
            NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
            if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
                return @"webp";
            }
            return nil;
    }
    return nil;
}
/**  ( ) */
- (UIImage *)cutCircleImage {
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    //  
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    //  
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctr, rect);
    //  
    CGContextClip(ctr);
    //  
    [self drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
#define SWWeakSelf(type)  __weak typeof(type) weak##type = type;
#define SWToast(str) [NSString stringWithFormat:@"%@",str]
// AFNetworking 3.0  http 
AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
AFHTTPRequestSerializer *requestSerializer =  [AFJSONRequestSerializer serializer];

NSDictionary *headerFieldValueDictionary = @{@"version":@"1.0"};
if (headerFieldValueDictionary != nil) {
    for (NSString *httpHeaderField in headerFieldValueDictionary.allKeys) {
        NSString *value = headerFieldValueDictionary[httpHeaderField];
        [requestSerializer setValue:value forHTTPHeaderField:httpHeaderField];
    }
}
manger.requestSerializer = requestSerializer;
[manger GET:@"url" parameters:nil  progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

}];
//  
+ (SWString *)sharedManager{
    static SWString *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[self alloc]init];
    });
    return shared;
}
//  
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // block
        // 
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                SWLog(@" ");
                break;
            case AFNetworkReachabilityStatusNotReachable:
                SWLog(@" ");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWWAN:
                SWLog(@" ");
                break;
                
            case AFNetworkReachabilityStatusReachableViaWiFi:
                SWLog(@"WiFi ");
                break;
            default:
                break;
        }
    }] ;
// NSDate NSString
+ (NSString *)nowDataString{
    NSDate *date = [NSDate date];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:@"yyyyMMddHHmmss"];
    return [objDateformat stringFromDate: date];
}
// ios md5
#import 

- (NSString *)MD5:(NSString *)mdStr
{
    const char *original_str = [mdStr UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [hash appendFormat:@"%02X", result[i]];
    return [hash lowercaseString];
}
// ios des 
 #import 
+ (NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key
{
    NSString *ciphertext = nil;
    const char *textBytes = [plainText UTF8String];
    NSUInteger dataLength = [plainText length];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    Byte iv[] = {1,2,3,4,5,6,7,8};
    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES,
                                          kCCOptionPKCS7Padding,
                                          [key UTF8String], kCCKeySizeDES,
                                          iv,
                                          textBytes, dataLength,
                                          buffer, 1024,
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted];
        
        ciphertext = [[[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding] autorelease];
    }
    return ciphertext;
}
 
// 
+ (NSString *) decryptUseDES:(NSString*)cipherText key:(NSString*)key 
{
    NSData* cipherData = [GTMBase64 decodeString:cipherText];
    unsigned char buffer[1024];
    memset(buffer, 0, sizeof(char));
    size_t numBytesDecrypted = 0;
    Byte iv[] = {1,2,3,4,5,6,7,8};
    CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, 
                                          kCCAlgorithmDES, 
                                          kCCOptionPKCS7Padding, 
                                          [key UTF8String], 
                                          kCCKeySizeDES, 
                                          iv, 
                                          [cipherData bytes], 
                                          [cipherData length], 
                                          buffer, 
                                          1024, 
                                          &numBytesDecrypted);
    NSString* plainText = nil;
    if (cryptStatus == kCCSuccess) {
        NSData* data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesDecrypted];
        plainText = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    }
    return plainText;
}

좋은 웹페이지 즐겨찾기