美文网首页DevSupport
iOS开发小技巧

iOS开发小技巧

作者: 夏虫不可语冰__ | 来源:发表于2015-10-14 20:18 被阅读355次

    不定期更新:有部分摘自其他博客,不一一列出出处,在此感谢作者


    • 使用MD5对NSData或者NSString加密
    //需要引入此头文件来使用CC_MD5
    #import <CommonCrypto/CommonDigest.h>
    

    针对NSString的加密①

    - (NSString *) md5:(NSString *) input{ 
      const char *cStr = [input UTF8String]; 
      unsigned char digest[CC_MD5_DIGEST_LENGTH]; 
      CC_MD5( cStr, strlen(cStr), digest );
      NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; 
      for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) [output appendFormat:@"%02x", digest[i]];
      return output;
    }
    

    针对NSString的加密②

    - (NSString *)md5:(NSString *) input{ 
      const char *cStr = [input UTF8String]; 
      unsigned char result[CC_MD5_DIGEST_LENGTH]; 
      CC_MD5( cStr, (int)strlen(cStr), result ); 
      return [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                result[0], result[1], result[2], result[3],
                result[4], result[5], result[6], result[7],
                result[8], result[9], result[10], result[11],
                result[12], result[13], result[14], result[15]
                ];
    
    }
    

    针对NSData的加密

    - (NSString*)md5:(NSData *)data
    {
        unsigned char result[CC_MD5_DIGEST_LENGTH];
        CC_MD5( data.bytes, (int)data.length, result ); 
        return [NSString stringWithFormat:
                @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                result[0], result[1], result[2], result[3],
                result[4], result[5], result[6], result[7],
                result[8], result[9], result[10], result[11],
                result[12], result[13], result[14], result[15]
                ];  
    }
    

    • 获取显示在屏幕上的所有cell
     self.tableView.visibleCells
    

    • 判断NSString中是否包含中文
    -(BOOL)isChinese:(NSString *)str{
        NSString *match=@"(^[\u4e00-\u9fa5]+$)";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
        return [predicate evaluateWithObject:str];
    }
    
    • 去掉TableView未占满屏幕的空白行
    self.tableView.tableFooterView = [UIView new];
    

    //方法1:重写下面方法
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
        {
            if (action == @selector(paste:))//禁止粘贴
                return NO;
            if (action == @selector(select:))// 禁止选择   
                return NO;   
            if (action == @selector(selectAll:))// 禁止全选   
                return NO;  
            return [super canPerformAction:action withSender:sender];
        }
    
    //方法2:
    -(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
        UIMenuController *menuController = [UIMenuController sharedMenuController];
        if (menuController) {
            [UIMenuController sharedMenuController].menuVisible = NO;
        }
        return NO;
    }
    
    • Xcode7下将https请求改回http请求
    //将下面代码加入info.plist文件
    //info.plist->open as->source code
     <key>NSAppTransportSecurity</key>
     <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
     </dict>
    
    • iOS9下将NSString转为UTF8
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    

    相关文章

      网友评论

        本文标题:iOS开发小技巧

        本文链接:https://www.haomeiwen.com/subject/gewocttx.html