1.刪除ToolBar下面的分割線
toolbar.showsBaselineSeparator = NO;
2.修改TextField的藍(lán)色或橙色邊框
self.textField.focusRingType = NSFocusRingTypeNone;
3.打開(kāi)已安裝的其他軟件
// 1
NSString *appPath = @"/Applications/Foxmail.app";
[[NSWorkspace sharedWorkspace] openFile:appPath];
// 2
[[NSWorkspace sharedWorkspace] launchApplication:@"Foxmail"];
4.打開(kāi)網(wǎng)頁(yè)
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://www.baidu.com"]];
5.Color生成純色圖片(iOS)
// 生成純色圖片
+ (UIImage *)pureColorImageWithColor:(UIColor *)color {
CGSize imageSize = CGSizeMake(1.0f, 1.0f);
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0f);
CGContextRef theContext = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(theContext, color.CGColor);
CGContextFillRect(theContext, CGRectMake(0.0f, 0.0f, imageSize.width, imageSize.height));
CGImageRef theCGImage = CGBitmapContextCreateImage(theContext);
UIImage *theImage;
if ([[UIImage class] respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
theImage = [UIImage imageWithCGImage:theCGImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
} else {
theImage = [UIImage imageWithCGImage:theCGImage];
}
CGImageRelease(theCGImage);
return theImage;
}
// 生成純色圓角圖片
+ (UIImage *)pureColorImageWithSize:(CGSize)size
color:(UIColor *)color
cornRadius:(CGFloat)radius {
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef cxt = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(cxt, color.CGColor);
CGContextSetStrokeColorWithColor(cxt, color.CGColor);
CGContextMoveToPoint(cxt, size.width, size.height-radius);
CGContextAddArcToPoint(cxt, size.width, size.height, size.width-radius, size.height, radius);//右下角
CGContextAddArcToPoint(cxt, 0, size.height, 0, size.height-radius, radius);//左下角
CGContextAddArcToPoint(cxt, 0, 0, radius, 0, radius);//左上角
CGContextAddArcToPoint(cxt, size.width, 0, size.width, radius, radius);//右上角
CGContextClosePath(cxt);
CGContextDrawPath(cxt, kCGPathFillStroke);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
6.時(shí)間格式轉(zhuǎn)換
/// 根據(jù)格式生成時(shí)間字符串
/// @param date 時(shí)間NSDate
/// @param formatStr 時(shí)間格式 eg:"YYYY-MM-dd HH:mm:ss"
+ (NSString *)stringFromDate:(NSDate *)date formatStr:(NSString *)formatStr {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formatStr];
return [formatter stringFromDate:date];
}
/// 根據(jù)格式生成NSDate
/// @param dateString 時(shí)間
/// @param formatStr 時(shí)間格式 eg:"YYYY-MM-dd HH:mm:ss"
+ (NSDate *)dateFromString:(NSString *)dateString formatStr:(NSString *)formatStr {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:formatStr];
return [formatter dateFromString:dateString];
}
/// 獲取當(dāng)前時(shí)間戳(秒)
+ (NSNumber *)getCurrentTimeStampSec {
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval timeInterval = [date timeIntervalSince1970];
NSNumber *u = [NSNumber numberWithInteger:timeInterval];
return u;
}
/// 獲取當(dāng)前時(shí)間戳(毫秒)
+ (NSNumber *)getCurrentTimeStampMilliSec {
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
long long timeInterval = [date timeIntervalSince1970] * 1000;
NSNumber *u = [NSNumber numberWithLongLong:timeInterval];
return u;
}
7.合法性
- 手機(jī)號(hào)是否合法
+ (BOOL)isValidMobile:(NSString *)mobile {
BOOL isValid = false;
if (!kStringIsEmpty(mobile) && mobile.length == 11) {
/**
* 移動(dòng)號(hào)段正則表達(dá)式
*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 聯(lián)通號(hào)段正則表達(dá)式
*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 電信號(hào)段正則表達(dá)式
*/
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
if (isMatch1 || isMatch2 || isMatch3) {
isValid = true;
}
}
return isValid;
}
- Url是否合法
+ (BOOL)isValidUrl:(NSString *)urlStr {
NSString *regex =@"[a-zA-z]+://[^\\s]*";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [urlTest evaluateWithObject:urlStr];
}
- 銀行卡是否合法
+ (BOOL)isValidCardNo:(NSString *)cardNumber {
int oddSum = 0; // 奇數(shù)和
int evenSum = 0; // 偶數(shù)和
int allSum =0; // 總和
// 循環(huán)加和
for(NSInteger i = 1; i <= cardNumber.length; i++) {
NSString *theNumber = [cardNumber substringWithRange:NSMakeRange(cardNumber.length-i,1)];
int lastNumber = [theNumber intValue];
if(i%2==0) {
// 偶數(shù)位
lastNumber *=2;
if(lastNumber >9) {
lastNumber -=9;
}
evenSum += lastNumber;
} else {
// 奇數(shù)位
oddSum += lastNumber;
}
}
allSum = oddSum + evenSum;
// 是否合法
if(allSum%10 == 0) {
return YES;
} else {
return NO;
}
}
- 判斷郵箱是否合法
+ (BOOL)isValidEmail:(NSString *)email {
if ([CCUtility checkEmptyString:email]) return NO;
NSString *emailRegex = @"^(([a-zA-Z0-9_-]+)|([a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)))@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
+ (BOOL)isValidUrl:(NSString *)urlStr {
NSString *regex =@"[a-zA-z]+://[^\\s]*";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [urlTest evaluateWithObject:urlStr];
}
- 判斷身份證(中國(guó)大陸)身份證是否合法
+ (BOOL)isValidIDCardNo:(NSString *)value {
value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSInteger length =0;
if (!value) {
return NO;
} else {
length = value.length;
//不滿足15位和18位张弛,即身份證錯(cuò)誤
if (length !=15 && length !=18) {
return NO;
}
}
// 省份代碼
NSArray *areasArray = @[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
// 檢測(cè)省份身份行政區(qū)代碼
NSString *valueStart2 = [value substringToIndex:2];
BOOL areaFlag =NO; //標(biāo)識(shí)省份代碼是否正確
for (NSString *areaCode in areasArray) {
if ([areaCode isEqualToString:valueStart2]) {
areaFlag =YES;
break;
}
}
if (!areaFlag) {
return NO;
}
NSRegularExpression *regularExpression;
NSUInteger numberofMatch;
int year =0;
//分為15位溺欧、18位身份證進(jìn)行校驗(yàn)
switch (length) {
case 15:
//獲取年份對(duì)應(yīng)的數(shù)字
year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
//創(chuàng)建正則表達(dá)式 NSRegularExpressionCaseInsensitive:不區(qū)分字母大小寫(xiě)的模式
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];//測(cè)試出生日期的合法性
} else {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$" options:NSRegularExpressionCaseInsensitive error:nil];//測(cè)試出生日期的合法性
}
//使用正則表達(dá)式匹配字符串 NSMatchingReportProgress:找到最長(zhǎng)的匹配字符串后調(diào)用block回調(diào)
numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
if(numberofMatch > 0) {
return YES;
} else {
return NO;
}
case 18:
year = [value substringWithRange:NSMakeRange(6,4)].intValue;
if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\\d{4}(((19|20)\\d{2}(0[13-9]|1[012])(0[1-9]|[12]\\d|30))|((19|20)\\d{2}(0[13578]|1[02])31)|((19|20)\\d{2}02(0[1-9]|1\\d|2[0-8]))|((19|20)([13579][26]|[2468][048]|0[048])0229))\\d{3}(\\d|X|x)?$" options:NSRegularExpressionCaseInsensitive error:nil];//測(cè)試出生日期的合法性
} else {
regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\\d{4}(((19|20)\\d{2}(0[13-9]|1[012])(0[1-9]|[12]\\d|30))|((19|20)\\d{2}(0[13578]|1[02])31)|((19|20)\\d{2}02(0[1-9]|1\\d|2[0-8]))|((19|20)([13579][26]|[2468][048]|0[048])0229))\\d{3}(\\d|X|x)?$" options:NSRegularExpressionCaseInsensitive error:nil];//測(cè)試出生日期的合法性
}
numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];
if(numberofMatch > 0) {
//1:校驗(yàn)碼的計(jì)算方法 身份證號(hào)碼17位數(shù)分別乘以不同的系數(shù)。從第一位到第十七位的系數(shù)分別為:7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2边臼。將這17位數(shù)字和系數(shù)相乘的結(jié)果相加。
int S = [value substringWithRange:NSMakeRange(0,1)].intValue*7 + [value substringWithRange:NSMakeRange(10,1)].intValue *7 + [value substringWithRange:NSMakeRange(1,1)].intValue*9 + [value substringWithRange:NSMakeRange(11,1)].intValue *9 + [value substringWithRange:NSMakeRange(2,1)].intValue*10 + [value substringWithRange:NSMakeRange(12,1)].intValue *10 + [value substringWithRange:NSMakeRange(3,1)].intValue*5 + [value substringWithRange:NSMakeRange(13,1)].intValue *5 + [value substringWithRange:NSMakeRange(4,1)].intValue*8 + [value substringWithRange:NSMakeRange(14,1)].intValue *8 + [value substringWithRange:NSMakeRange(5,1)].intValue*4 + [value substringWithRange:NSMakeRange(15,1)].intValue *4 + [value substringWithRange:NSMakeRange(6,1)].intValue*2 + [value substringWithRange:NSMakeRange(16,1)].intValue *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
//2:用加出來(lái)和除以11,看余數(shù)是多少?余數(shù)只可能有0-1-2-3-4-5-6-7-8-9-10這11個(gè)數(shù)字
int Y = S %11;
NSString *M =@"F";
NSString *JYM =@"10X98765432";
M = [JYM substringWithRange:NSMakeRange(Y,1)];// 3:獲取校驗(yàn)位
NSString *lastStr = [value substringWithRange:NSMakeRange(17,1)];
//4:檢測(cè)ID的校驗(yàn)位
if ([lastStr isEqualToString:@"x"]) {
if ([M isEqualToString:@"X"]) {
return YES;
} else {
return NO;
}
} else {
if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
return YES;
} else {
return NO;
}
}
} else {
return NO;
}
default:
return NO;
}
}
8.強(qiáng)弱引用
// 推薦使用(摘自YYKit)
/**
Synthsize a weak or strong reference.
Example:
@weakify(self)
[self doSomething^{
@strongify(self)
if (!self) return;
...
}];
*/
#ifndef weakify
#if DEBUG
#if __has_feature(objc_arc)
#define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
#endif
#else
#if __has_feature(objc_arc)
#define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
#else
#define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
#endif
#endif
#endif
#ifndef strongify
#if DEBUG
#if __has_feature(objc_arc)
#define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
#endif
#else
#if __has_feature(objc_arc)
#define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
#else
#define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
#endif
#endif
#endif
9.NSLog的宏定義
#ifdef DEBUG
#define NSLog(format,...) printf("\n[%s] %s [第%d行] %s\n",__TIME__,__FUNCTION__,__LINE__,[[NSString stringWithFormat:format,## __VA_ARGS__] UTF8String]);
#else
#define NSLog(format, ...)
#endif
10.判斷是否為空
#define kStringIsEmpty(string) (string == NULL || [string isKindOfClass:[NSNull class]] || string == nil || [string length] < 1)
#define kArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
#define kDictionaryIsEmpty(dictionary) (dictionary == nil || [dictionary isKindOfClass:[NSNull class]] || dictionary.allKeys.count == 0)
#define kObjectIsEmpty(object) (object == nil||[object isKindOfClass:[NSNull class]]||([object respondsToSelector:@selector(length)] && [(NSData *)object length] == 0)|| ([object respondsToSelector:@selector(count)] && [(NSArray *)object count] == 0))
11.使用xib
創(chuàng)建自定義View并在背景添加點(diǎn)擊事件
說(shuō)明:
xib
創(chuàng)建的自定義視圖壳影,添加的背景點(diǎn)擊動(dòng)作不會(huì)觸發(fā)點(diǎn)擊事件,因此需要單獨(dú)添加一個(gè)xibView
声怔,添加到自定義視圖上,點(diǎn)擊動(dòng)作添加到xibView
上就可以觸發(fā)點(diǎn)擊事件了舱呻,具體步驟如下:
- 創(chuàng)建自定義
FSCommonView
醋火、創(chuàng)建FSCommonView.xib
悠汽、關(guān)聯(lián)到自定義的FSCommonView
。
- 將xib添加到
FSCommonView
中
#import <UIKit/UIKit.h>
@interface FSCommonView : UIView
@end
#import "FSCommonView.h"
@interface FSCommonView ()
@property (strong, nonatomic) UIView *xibView;
@end
@implementation
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.xibView = [self loadViewFromNib];
[self settingUI];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.xibView = [self loadViewFromNib];
[self settingUI];
}
return self;
}
- (UIView *)loadViewFromNib {
UIView *xibView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil].firstObject;
xibView.frame = self.bounds;
[xibView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(xibBgViewClick)]];
[self addSubview:xibView];
return xibView;
}
- (void)settingUI {
// 設(shè)置背景色芥驳、圓角柿冲、邊框、自定義子視圖等兆旬。
}
- (void)xibBgViewClick {
// 背景點(diǎn)擊事件
}
@end
12.啄幕鳥(niǎo)
啄幕鳥(niǎo)假抄,阿里開(kāi)源項(xiàng)目,即手機(jī)屏幕上的啄木鳥(niǎo)丽猬,專(zhuān)抓App里的Bug宿饱。啄幕鳥(niǎo)集合了UI檢查、對(duì)象查看脚祟、方法監(jiān)聽(tīng)等多種開(kāi)發(fā)工具谬以,通過(guò)拾取UI控件、查看對(duì)象屬性由桌、監(jiān)聽(tīng)方法調(diào)用为黎、App內(nèi)抓包等,不依賴電腦聯(lián)調(diào)行您,直接獲取運(yùn)行時(shí)數(shù)據(jù)铭乾,快速定位Bug,提高開(kāi)發(fā)效率娃循。
功能簡(jiǎn)介
- UI檢查:快速查看頁(yè)面布局炕檩、UI控件間距、字體顏色淮野、UI控件類(lèi)名捧书、對(duì)象屬性/成員變量、圖片URL等骤星。
- JSON抓包:便捷JSON抓包工具经瓷,通過(guò)監(jiān)聽(tīng)系統(tǒng)json解析抓包。
- 方法監(jiān)聽(tīng):Bug聽(tīng)診器洞难,可監(jiān)聽(tīng)App中任意OC方法的調(diào)用舆吮,輸出調(diào)用參數(shù)、返回值等信息队贱,可以通過(guò)屏幕日志輸入監(jiān)聽(tīng)色冀、KVC取值等命令,支持后臺(tái)配置命令柱嫌。
- po命令:執(zhí)行類(lèi)似LLDB的po命令锋恬,在App運(yùn)行時(shí)執(zhí)行po命令,調(diào)用任意方法编丘。
- 系統(tǒng)信息:查看各種系統(tǒng)名稱(chēng)与学、版本彤悔、屏幕、UA等信息索守,支持外部添加信息晕窑。
- SandBox:查看沙盒文件,導(dǎo)出文件等卵佛。
- Bundle:查看杨赤、導(dǎo)出Bundle目錄中的內(nèi)容。
- Crash:查看Crash日志截汪,需先打開(kāi)一次Crash插件以開(kāi)啟Crash監(jiān)控疾牲。
- Defaults:查看、新增挫鸽、刪除User Defaults说敏。
- 清除數(shù)據(jù):清除所有沙盒數(shù)據(jù)、User Default丢郊。
- 觸點(diǎn)顯示:顯示手指觸控盔沫。
- UI對(duì)比:支持將設(shè)計(jì)圖導(dǎo)入到App中進(jìn)行對(duì)比,并可畫(huà)線枫匾、標(biāo)注需修改的地方架诞,方便UI走查。
- 查看圖片資源:查看干茉、導(dǎo)出App中的資源圖片谴忧。
- CPU:查看CPU占用。
- 內(nèi)存:查看內(nèi)存占用角虫。
- FPS:查看App幀率沾谓。
- 網(wǎng)絡(luò)流量:查看發(fā)送、接收網(wǎng)絡(luò)流量戳鹅。
pod 'YKWoodpecker'
官方github地址:YKWoodpecker
使用
// 顯示啄幕鳥(niǎo)均驶,啟動(dòng)默認(rèn)打開(kāi)UI檢查插件
[[YKWoodpeckerManager sharedInstance] show];
13.ViewController的生命周期
1. initWithCoder:通過(guò)nib文件初始化時(shí)觸發(fā)。
2. awakeFromNib:nib文件被加載的時(shí)候枫虏,會(huì)發(fā)生一個(gè)awakeFromNib的消息到nib文件中的每個(gè)對(duì)象妇穴。
3. loadView:開(kāi)始加載視圖控制器自帶的view。
4. viewDidLoad:視圖控制器的view被加載完成隶债。
5. viewWillAppear:視圖控制器的view將要顯示在window上腾它。
6. updateViewConstraints:視圖控制器的view開(kāi)始更新AutoLayout約束。
7. viewWillLayoutSubviews:視圖控制器的view將要更新內(nèi)容視圖的位置死讹。
8. viewDidLayoutSubviews:視圖控制器的view已經(jīng)更新視圖的位置瞒滴。
9. viewDidAppear:視圖控制器的view已經(jīng)展示到window上。
10. viewWillDisappear:視圖控制器的view將要從window上消失赞警。
11. viewDidDisappear:視圖控制器的view已經(jīng)從window上消失妓忍。
14. 去除Cocopods
第三方庫(kù)部分警告和MobileCoreServices.framework
過(guò)期的問(wèn)題
在Podfile
的末尾處添加如下代碼稀并,再 pod install
post_install do |pi|
# 去除Pods版本警告
pi.pods_project.targets.each do |t|
t.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'
end
end
# 替換Pods引用的不再使用的靜態(tài)庫(kù)MobileCoreServices.framework
framework = pi.pods_project.frameworks_group["iOS"]["MobileCoreServices.framework"]
framework.referrers.each do |ref|
if ref.isa == "PBXBuildFile"
ref.remove_from_project
end
end
framework.remove_from_project
end
個(gè)人博客: ?? ForgetSou