1、textField 委托【textFieldShouldReturn】接受return事件,委托定义self.textField.delegate = self;
关闭键盘[textField resignFirstResponder];
2、如果是在storyBoard中拖动的控件,并且链接到代码后,使用的时候不需要alloc init新的对象,可以直接使用;
3、客户端Socket链接:
NSString * host =@"IP";
NSNumber * port = @port;
// 创建 socket
int socketFileDescriptor = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == socketFileDescriptor) {
NSLog(@"创建失败");
return;
}
// 获取 IP 地址
struct hostent * remoteHostEnt = gethostbyname([host UTF8String]);
if (NULL == remoteHostEnt) {
close(socketFileDescriptor);
NSLog(@"%@",@"无法解析服务器的主机名");
return;
}
struct in_addr * remoteInAddr = (struct in_addr *)remoteHostEnt->h_addr_list[0];
// 设置 socket 参数
struct sockaddr_in socketParameters;
socketParameters.sin_family = AF_INET;
socketParameters.sin_addr = *remoteInAddr;
socketParameters.sin_port = htons([port intValue]);
// 连接 socket
int ret = connect(socketFileDescriptor, (struct sockaddr *) &socketParameters, sizeof(socketParameters));
if (-1 == ret) {
close(socketFileDescriptor);
NSLog(@"连接失败");
return;
}
NSLog(@"连接成功");
4、定位:
info.plist加入两个参数:Location Always Usage Description 和 Location When In Use Usage Description,value随便。
引用:#import <CoreLocation/CoreLocation.h>
加入委托:CLLocationManagerDelegate
添加属性:@property (nonatomic, strong) CLLocationManager *locationManager;
添加代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"start location");
[self startLocation];
}
//开始定位
- (void)startLocation {
if ([CLLocationManager locationServicesEnabled]) {
// CLog(@"--------开始定位");
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
//控制定位精度,越高耗电量越
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// 总是授权
[self.locationManager requestAlwaysAuthorization];
self.locationManager.distanceFilter = 10.0f;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if ([error code] == kCLErrorDenied) {
NSLog(@"访问被拒绝");
}
if ([error code] == kCLErrorLocationUnknown) {
NSLog(@"无法获取位置信息");
}
}
//定位代理经纬度回调
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
CLLocation *newLocation = locations[0];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error){
if (array.count > 0){
CLPlacemark *placemark = [array objectAtIndex:0];
//获取城市
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSLog(@"city = %@", city);//石家庄市
NSLog(@"--%@",placemark.name);//黄河大道221号
NSLog(@"++++%@",placemark.subLocality); //裕华区
NSLog(@"country == %@",placemark.country);//中国
NSLog(@"administrativeArea == %@",placemark.administrativeArea); //河北省
}
else if (error == nil && [array count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error != nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}
最后手机设置选项允许app使用定位权限。
5、客户端socket连接和消息发送接收:
引入头文件
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
// 1.创建客户端Socket
/**
参数
参数1 : domain,协议域/协议簇,AF_INET(IPV4的网络开发)
参数2 : type,Socket类型,SOCK_STREAM(TCP)/SOCK_DGRAM(UDP,报文)
参数3 : protocol,IPPROTO_TCP,协议,如果输入0,可以根据第二个参数,自动选择协议
返回值
int类型,如果 > 0 就表示创建客户端Socket成功,返回socket
*/
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket > 0) {
NSLog(@"创建客户端Socket成功");
}
// 2.客户端Socket连接到服务器Socket
/**
参数
参数1 : 客户端socket
参数2 : 指向数据结构sockaddr的指针,其中包括目的端口和IP地址
服务器的"结构体"地址
提示:C 语言中没有对象
参数3 : 结构体数据长度
返回值
0 成功/其他 错误代号,(不是非0即真)
*/
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8888);
addr.sin_addr.s_addr = inet_addr("192.168.0.104");
int isConnected = connect(clientSocket, (const struct sockaddr *)&addr, sizeof(addr));
if (isConnected == 0) {
NSLog(@"连接到服务器成功");
}
// 3.客户端Socket向服务器Socket发送请求
/**
参数
参数1 : 客户端socket
参数2 : 发送内容地址 void * == id
参数3 : 发送内容长度
参数4 : 发送方式标志,一般为0
返回值
如果成功,则返回发送的字节数,失败则返回SOCKET_ERROR
*/
NSString *sendMsg = @"哇哈哈";
ssize_t sendCount = send(clientSocket, sendMsg.UTF8String, strlen(sendMsg.UTF8String), 0);
NSLog(@"发送字符数 %ld",sendCount);
// 4.客户端Socket接收服务器Socket发送的数据(响应)
/**
参数
参数1 : 客户端socket
参数2 : 接收内容缓冲区地址
参数3 : 接收内容缓存区长度
参数4 : 接收方式,0表示阻塞,必须等待服务器返回数据
返回值
如果成功,则返回读入的字节数,失败则返回SOCKET_ERROR
提示 : 服务器发送给客户端数据时,是一点一点发送的
提示 : 当服务器把数据都发送完了以后,再次发送时,只发送0字节,
*/
// 创建接收服务器发送的数据的容器 / 缓冲区 ,并且指定了容量
uint8_t buffer[1024];
// 需要创建一个容器
NSMutableData *dataM = [NSMutableData data];
// 循环的接收服务器发送的数据
ssize_t recvCount = -1;
while (recvCount != 0) {
// 值接收了一次
recvCount = recv(clientSocket, buffer, sizeof(buffer), 0);
NSLog(@"接收的内容数 %ld",recvCount);
[dataM appendBytes:buffer length:recvCount];
}
NSString *html = [[NSString alloc] initWithData:dataM encoding:NSUTF8StringEncoding];
NSLog(@"%@",html);
// 5.关闭Socket
close(clientSocket);
6、查看本机IP,同样是引入上面的头文件
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
NSLog(@"本机的IP为:%@", address);
7、字符串拼装:
self.textView.text = [self.textView.text stringByAppendingString:@"\r\n"];
self.textView.text = [self.textView.text stringByAppendingFormat:@"街道:%@\r\n区:%@\r\n国家:%@\r\n省份:%@",placemark.name,placemark.subLocality,placemark.country,placemark.administrativeArea];
8、转换:
1. 字符串转Data
NSString * str =@"str";
NSData *data =[str dataUsingEncoding:NSUTF8StringEncoding];
2.NSData 转NSString
NSString * str =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
3.data 转char
NSData *data;
char * haha=[data bytes];
4. char 转data
byte * byteData = malloc(sizeof(byte)*16);
NSData *content=[NSData dataWithBytes:byteData length:16];
9、指定圆角:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_btnUnfold.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(8.0, 8.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = _btnUnfold.bounds;
maskLayer.path = maskPath.CGPath;
_btnUnfold.layer.mask = maskLayer;
10、状态栏颜色:
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
11、UINavigationController导航栏的隐藏和显示
- (void)viewWillAppear:(BOOL)animated {
[super.navigationController setNavigationBarHidden:YES animated:TRUE];
}
- (void)viewWillDisappear:(BOOL)animated {
[super.navigationController setNavigationBarHidden:NO animated:TRUE];
}
12、隐藏tabbar
这样隐藏在手势pop的时候不会有遮挡问题
13、页面跳转(不会出现黑屏)
PublishViewEditController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"PublishEdit"];
[self showViewController:vc sender:nil];
14、由navController跳转过来的标题navbar修改:
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.title = @"发布";
15、圆角和阴影
_imgView.layer.cornerRadius = 75; //设置imageView的圆角
_imgView.layer.masksToBounds = YES;
_imgView.layer.shadowColor = [UIColor blackColor].CGColor;//设置阴影的颜色
_imgView.layer.shadowOpacity = 0.8;//设置阴影的透明度
_imgView.layer.shadowOffset = CGSizeMake(1, 1);//设置阴影的偏移量
_imgView.layer.shadowRadius = 3;//设置阴影的圆角