找了好久关于字典转模型的文章
http://www.jianshu.com/p/de718ef3bd20
//获取某个view所在的控制器
- (UIViewController *)viewController {
UIViewController *viewController = nil;
UIResponder *next = self.nextResponder;
while (next) {
if ([next isKindOfClass:[UIViewController class]]) {
viewController = (UIViewController *)next;
break;
}
next = next.nextResponder;
}
return viewController;
}
代码让UIImage具有渐变效果
- (UIImage *) imageWithBackgroundColor:(UIColor *)bgColor
shadeAlpha1:(CGFloat)alpha1
shadeAlpha2:(CGFloat)alpha2
shadeAlpha3:(CGFloat)alpha3
shadowColor:(UIColor *)shadowColor
shadowOffset:(CGSize)shadowOffset
shadowBlur:(CGFloat)shadowBlur {
UIImage *image = self;
CGColorRef cgColor = [bgColor CGColor];
CGColorRef cgShadowColor = [shadowColor CGColor];
CGFloat components[16] = {1,1,1,alpha1,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3};
CGFloat locations[4] = {0,0.5,0.6,1};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)4);
CGRect contextRect;
contextRect.origin.x = 0.0f;
contextRect.origin.y = 0.0f;
contextRect.size = [image size];
//contextRect.size = CGSizeMake([image size].width+5,[image size].height+5);
// Retrieve source image and begin image context
UIImage *itemImage = image;
CGSize itemImageSize = [itemImage size];
CGPoint itemImagePosition;
itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
UIGraphicsBeginImageContext(contextRect.size);
CGContextRef c = UIGraphicsGetCurrentContext();
// Setup shadow
CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
// Setup transparency layer and clip to mask
CGContextBeginTransparencyLayer(c, NULL);
CGContextScaleCTM(c, 1.0, -1.0);
CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
// Fill and end the transparency layer
CGContextSetFillColorWithColor(c, cgColor);
contextRect.size.height = -contextRect.size.height;
CGContextFillRect(c, contextRect);
CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(contextRect.size.width*1.0/4.0,contextRect.size.height),0);
CGContextEndTransparencyLayer(c);
//CGPointMake(contextRect.size.width*3.0/4.0, 0)
// Set selected image and end context
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGColorSpaceRelease(colorSpace);
CGGradientRelease(colorGradient);
return resultImage;
}
把几张图片合成一张图片
- (UIImage *)composeWithHeader:(UIImage *)header content:(UIImage *)content footer:(UIImage *)footer{
CGSize size = CGSizeMake(content.size.width, header.size.height +content.size.height +footer.size.height);
UIGraphicsBeginImageContext(size);
[header drawInRect:CGRectMake(0,0,header.size.width,header.size.height)];
[content drawInRect:CGRectMake(0,header.size.height,content.size.width,content.size.height)];
[footer drawInRect:CGRectMake(0,header.size.height+content.size.height,footer.size.width,footer.size.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
PCH文件使用
http://www.jianshu.com/p/67ce72c4ad6c
app后台持续运行 解决方法
http://www.jianshu.com/p/d1ecc467faff
http://www.jianshu.com/p/a7aab7f04639
http://www.jianshu.com/p/d466f2da0d33
http://www.jianshu.com/p/1cb03ec1cb0b
http://www.jianshu.com/p/b7173ccd4241
http://www.jianshu.com/p/1f2572c08816
http://www.jianshu.com/p/420a66beb0c1
http://www.jianshu.com/p/2c38e7dc738a
http://www.jianshu.com/p/2c48aef39142
http://blog.csdn.net/u010818357/article/details/51221002
http://www.2cto.com/kf/201401/273820.html
http://blog.csdn.net/ztp800201/article/details/14222755
http://blog.csdn.net/qqmcy/article/details/48680601
http://blog.csdn.net/dean19900504/article/details/8673800/
https://yq.aliyun.com/articles/41371
开启手机震动
#import
- (void)vibrate {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
1.真机调试签名证书无法注册
原因是两个人都用了这个开发者账号调试真机,只需要在配置签名证书里重新下载签名文件即可.
2.申请证书流程
iOS证书申请流程
http://newdocx.appcan.cn/dev-guide/ios-certi-process
iOS应用正式发布AppStore流程
http://newdocx.appcan.cn/dev-guide/ios-dev-appStore
3.collectionView的cell添加点击效果
//当cell高亮时返回是否高亮
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//设置(Highlight)高亮下的颜色
[cell setBackgroundColor:[UIColor colorWithString:@"f2f2f2"]];
}
- (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath];
//设置(Nomal)正常状态下的颜色
[cell setBackgroundColor:[UIColor whiteColor]];
}
4.在webView的代理方法里跳转崩溃
warning:This application is modifying the autolayout engine from a background thread
警告提示:This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
错误原因:在回调函数中或者后台线程中更新了UI界面,而UI界面必须是在主界面中才能进行修改
解决办法:
dispatch_async(dispatch_get_main_queue(), ^{
});
5.常见白名单
LSApplicationQueriesSchemes
wechat
weixin
sinaweibohd
sinaweibo
sinaweibosso
weibosdk
weibosdk2.5
mqqapi
mqq
mqqOpensdkSSoLogin
mqqconnect
mqqopensdkdataline
mqqopensdkgrouptribeshare
mqqopensdkfriend
mqqopensdkapi
mqqopensdkapiV2
mqqopensdkapiV3
mqqopensdkapiV4
mqzoneopensdk
wtloginmqq
wtloginmqq2
mqqwpa
mqzone
mqzonev2
mqzoneshare
wtloginqzone
mqzonewx
mqzoneopensdkapiV2
mqzoneopensdkapi19
mqzoneopensdkapi
mqqbrowser
mttbrowser
alipay
alipayshare
renrenios
renrenapi
renren
renreniphone
laiwangsso
yixin
yixinopenapi
instagram
whatsapp
line
fbapi
fb-messenger-api
fbauth2
fbshareextension
kakaofa63a0b2356e923f3edd6512d531f546
kakaokompassauth
storykompassauth
kakaolink
kakaotalk-4.5.0
kakaostory-2.9.0
pinterestsdk.v1
6.pod 更新库报错
[!] Unable to add a source with url `https://github.com/CocoaPods/Specs.git` named `master-1`.
You can try adding it manually in `~/.cocoapods/repos` or via `pod repo add`.
解决方法:把命令行改成当前使用的Xcode
7.常用数据接口
http://sharesdk.mob.com
https://www.juhe.cn
http://www.jisuapi.com
UITextfield 限制输入字数
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (range.length + range.location > textField.text.length) {
return NO;
}
NSUInteger length = textField.text.length + string.length - range.length;
return length <= 你要限制字数;
}
8.NSTimer使用发方法
1.创建定时器
1 NSTimer *timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
2 [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
2.开启定时器
3,关闭定时器
9.小知识点
// 设置导航栏颜色
self.navigationController.navigationBar.barTintColor = mainColor;
// 设置导航栏字体的颜色和大小
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:k6AutoSize(22)], NSForegroundColorAttributeName:[UIColor whiteColor]}];
// 设置左右两个按钮的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
// 左侧边缘滑动返回
UIScreenEdgePanGestureRecognizer *screenEdgePanGR = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(backEvent)];
screenEdgePanGR.edges = UIRectEdgeLeft;
[self.view addGestureRecognizer:screenEdgePanGR];
// 给view添加点击事件
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
[tapGesture addTarget:self action:@selector(action)];
[view addGestureRecognizer:tapGesture];
// 判断是否是汉字
for (int i = 0; i < self.identityInformationView.nameTextField.text.length; i++) {
NSRange range = NSMakeRange(i, 1);
NSString *subString = [self.identityInformationView.nameTextField.text substringWithRange:range];
const char *cString = [subString UTF8String];
if (strlen(cString) != 3) {
// 是汉字
}
}
// 复制文字到剪切板
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = @"100000";
修改bundle ID
我们一般是先创建工程,把代码写好;等打包上传App Store的时候,去Apple Developer Center创建App ID,搞那一堆证书什么的。
如果按部就班的走流程,没有什么问题,问题是如果当时工程的bundle ID默认是com.apple.test(只是举个例子)后来打包上传的时候发现不想用这个了,app ID于是就创建了一个不一样的,(比如)com.baidu.test。然后把所有的证书都弄好了之后,需要把项目的bundle ID改成新的com.baidu.test,因为不改的话,证书不匹配,编译失败没法打包上传。
那我们会直接把info.plist文件中的bundle ID改成com.baidu.test。
那么问题来了,这个时候还是会提示The provisioning profile specified in your build settings ("test") has an AppID of "com.baidu.test" which does not match your bundle identifier "com.apple.test"其实修改bundle identifier 只在info.plist中修改并没有完,还需要把Build Settings中Package分栏下的Product Bundle Identifier改成新的“com.baidu.test”
cell自适应
self.tableView.estimatedRowHeight = 100;
self.tableView.rowHeight = UITableViewAutomaticDimension;
100可以随便写,只要有个数就行。当然也不能忘了
cell.label.numberOfLines = 0;
//邮箱
- (BOOL) validateEmail:(NSString *)email
{
NSString *emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
//手机号码验证
- (BOOL) validateMobile:(NSString *)mobile
{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex =@"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//车牌号验证
- (BOOL) validateCarNo:(NSString *)carNo
{
NSString *carRegex =@"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
NSPredicate *carTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",carRegex];
NSLog(@"carTest is %@",carTest);
return [carTest evaluateWithObject:carNo];
}
//车型
- (BOOL) validateCarType:(NSString *)CarType
{
NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
NSPredicate *carTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
return [carTest evaluateWithObject:CarType];
}
//用户名
- (BOOL) validateUserName:(NSString *)name
{
NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
NSPredicate *userNamePredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",userNameRegex];
return [userNamePredicateevaluateWithObject:name];
}
//密码
- (BOOL) validatePassword:(NSString *)passWord
{
NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
NSPredicate *passWordPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",passWordRegex];
return [passWordPredicate evaluateWithObject:passWord];
}
//昵称
- (BOOL) validateNickname:(NSString *)nickname
{
NSString *nicknameRegex =@"^[\u4e00-\u9fa5]{4,8}$";
NSPredicate *passWordPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [passWordPredicate evaluateWithObject:nickname];
}
//身份证号
- (BOOL) validateIdentityCard: (NSString *)identityCard
{
if (identityCard.length == 15|| identityCard.length == 18) {
NSString *regex2 =@"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:identityCard];
}
return NO;
}
// 此方法是用来判断一个字符串是不是整型.如果传进的字符串是一个字符,可以用来判断它是不是数字
- (BOOL)isPureInt:(NSString *)string{
NSScanner *scan = [NSScanner scannerWithString:string];
int value;
return [scan scanInt:&value] && [scan isAtEnd];
}
// label根据文字自适应高度
self.tailLabel.numberOfLines = 0;
self.tailLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize size = [self.tailLabel sizeThatFits:CGSizeMake(self.tailLabel.frame.size.width, MAXFLOAT)];
CGRect frame = self.tailLabel.frame;
frame.size.height = size.height;
self.tailLabel.frame = frame;
// 设置label的行间距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:tail];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:k6AutoSize(8)];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [tail length])];
[self.tailLabel setAttributedText:attributedString];
[self.tailLabel sizeToFit];
单例
+ (ViewController *)sharedViewController
{
static ViewController *VC = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
VC = [[ViewController alloc] init];
});
return VC;
}
统计代码行数
打开终端
①cd进入工程文件夹下
②执行命令find . "(" -name "*.m" -or -name "*.mm" -or -name "*.cpp" -or -name "*.h" -or -name "*.rss" ")" -print | xargs wc -l
弹窗宏
// 抽成宏之后,只需要一行代码就能完成弹窗提醒
#define alert(string, detail) UIAlertController *alertController = [UIAlertController alertControllerWithTitle:string message:detail preferredStyle:UIAlertControllerStyleAlert]; \
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil]; \
[alertController addAction:okAction]; \
[self presentViewController:alertController animated:YES completion:nil];
// 调用它只需一行代码
alert(@"提交成功", @"终于成功了")
或者alert(@"提交失败", nil)
语法糖
当你这样写一段代码时:
self.imageView = [[UIImageView alloc] init];
self.imageView.backgroundColor = [UIColor redColor];
self.imageView.image = [UIImage imageNamed:@"12345"];
self.imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:self.imageView];
其实可以使用语法糖来表达:
self.imageView = ({
UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor redColor];
imageView.image = [UIImage imageNamed:@"12345"];
imageView.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:imageView];
imageView;
});