[Xcode7 使用NSURLSession发送HTTP请求报错]
控制台打印:Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
解决办法:修改info.plist文件
NSAppTransportSecurity
NSAllowsArbitraryLoads
1.判断当前点击的点是不是属于userPassWord的范围内
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch*touch=[touchesanyObject];
CGPointp = [touchlocationInView:self.view];
// 判断当前点击的点是不是属于userPassWord的范围内
if(!CGRectContainsPoint(self.userPassWord.frame, p) ) {
[self.userPassWordresignFirstResponder];
}
}
2.改变textField的frema不会被键盘遮挡
//键盘弹出
- (void)keyboardShow:(NSNotification*)notifi
{
NSLog(@"%@",notifi.userInfo);
if([_textField1isFirstResponder]) {
}
elseif([_textField2isFirstResponder]){
}
elseif([_textField3isFirstResponder]){
CGRectrect = [notifi.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloatkby = rect.origin.y;
CGFloattfy =CGRectGetMaxY(_textField3.frame);
if(tfy - kby >0) {
[UIViewanimateWithDuration:0.25animations:^{
self.view.transform=CGAffineTransformMakeTranslation(0, -(tfy - kby));
}];
}
}
elseif([_textField4isFirstResponder]){
CGRectrect = [notifi.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];
CGFloatkby = rect.origin.y;
CGFloattfy =CGRectGetMaxY(_textField4.frame);
if(tfy - kby >0) {
[UIViewanimateWithDuration:0.25animations:^{
self.view.transform=CGAffineTransformMakeTranslation(0, -(tfy - kby));
}];
}
}
}
//键盘隐藏
- (void)keyboardHidden:(NSNotification*)notifi
{
[UIViewanimateWithDuration:0.25animations:^{
self.view.transform=CGAffineTransformIdentity;
}];
}
3.画进度条
- (void)drawRect:(CGRect)rect {
// Drawing code
CGFloattextH =20;
CGFloattextW =30;
CGFloatviewW = rect.size.width;
CGFloatviewH = rect.size.height;
CGFloattextX = (viewW - textW) *0.5;
CGFloattextY = (viewH - textH) *0.5;
// 1.画文字
NSString*text = [NSStringstringWithFormat:@"%.2f",self.pregress];
[textdrawInRect:CGRectMake(textX, textY, textW, textH)withAttributes:nil];
// 2.画弧
CGContextRefcontext =UIGraphicsGetCurrentContext();
//半径
CGFloatradius = (viewW -10) *0.5;
CGFloatendAngle =self.pregress*2*M_PI-M_PI_4;
CGContextAddArc(context, viewW *0.5, viewH *0.5, radius, -M_PI_4, endAngle,0);
CGContextStrokePath(context);
}
-(void)setPregress:(float)pregress{
_pregress= pregress;
//重绘
[selfsetNeedsDisplay];
}
3.ASI—创建post请求
// 1.创建请求对象
NSURL*url = [NSURLURLWithString:@"[http://192.168.1.200:8080/MJServer/login](http://192.168.1.200:8080/MJServer/login)"];
ASIFormDataRequest*request = [ASIFormDataRequestrequestWithURL:url];
// 2.添加请求参数(请求体中的参数)
[requestsetPostValue:@"123"forKey:@"username"];
[requestsetPostValue:@"999"forKey:@"pwd"];
[requestsetCompletionBlock:^{
NSLog(@"---请求完毕");
}];
// 3.发送请求
[requeststartAsynchronous];
}
或者
// 1.创建请求对象
NSURL*url = [NSURLURLWithString:@"[http://192.168.1.200:8080/MJServer/login](http://192.168.1.200:8080/MJServer/login)"];
self.request= [ASIHTTPRequestrequestWithURL:url];
self.request.timeOutSeconds=10;
//设置请求方法
self.request.requestMethod=@"POST";
//设置请求体
[self.requestappendPostData:[@"username=123&pwd=123"dataUsingEncoding:NSUTF8StringEncoding]];
// [self.request setPostLength:self.request.postBody.length];
// [self.request addRequestHeader:[NSString stringWithFormat:@"%d", self.request.postBody.length] value:@"Content-Length"];
// [self.request addRequestHeader:@"application/x-www-form-urlencoded" value:@"Content-Type"];
// 2.发送请求
[self.requeststartAsynchronous];
3.UILabel高度自适应(自动换行)
UILabel*label = [[UILabelalloc]init];
label.numberOfLines=0;
label.lineBreakMode=UILineBreakModeWordWrap;
label.text=@"并将label的frame变成实际大小并将label的frame变成实际大小并将label的frame变成实际大小并将label的frame变成实际大小";
label.font= [UIFontfontWithName:@"Microsoft"size:12];
CGSizesize =CGSizeMake(320,1000);
CGSizesizeLabel = [label.textsizeWithFont:label.fontconstrainedToSize:sizelineBreakMode:NSLineBreakByWordWrapping];
[labelsetFrame:CGRectMake(0,200, sizeLabel.width, sizeLabel.height)];
[self.viewaddSubview:label];
4.UILabel文本大小自适应
label.adjustsFontSizeToFitWidth=YES;
label.baselineAdjustment =UIBaselineAdjustmentAlignCenters;
4.1DUBUG和RELEASE模式切换
#ifdef DEBUG
#define debugLog(...) NSLog(__VA_ARGS__)
#define debugMethod() NSLog(@"%s", __func__)
#else
#define debugLog(...)
#define debugMethod()
#endif
#define RGB(A, B, C) [UIColor colorWithRed:A/255.0 green:B/255.0 blue:C/255.0 alpha:1.0]
5.根据颜色返回图片
- (UIImage*) imageWithColor:(UIColor*)color
{
CGRectrect =CGRectMake(0.0f,0.0f,1.0f,1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [colorCGColor]);
CGContextFillRect(context, rect);
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}