0%

项目中的网络请求需要在Header中添加Authorization,并进行base64编码后请求数据。

  1. 设置SecurityPolicy

    有两种情况需要设置SecurityPollicy

    • 使用HTTP请求,则需要在plist文件中设置允许HTTP请求,并添加host地址到白名单,但有时请求仍会被Cancel(-999错误);
    • HTTPS请求,但是证书过期或无效了,请求可能被cancel

    设置SecurityPolicy:

    1
    2
    3
    4
    5
    6
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
    securityPolicy.validatesDomainName = NO;
    securityPolicy.allowInvalidCertificates = YES;

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = securityPolicy;
  2. 设置base64编码过的Authorization

    阅读全文 »

Tagged Pointer

  • bit64之后引入,针对小对象(NSString、NSNumber、NSDate等)的存储
  • 使用前:
    • NSNumber等对象需要动态分配内存,维护引用计数,NSNumber指针存储的是堆中的NSNumber对象的地址值
  • 使用后:
    • NSNumber指针里面存储的数据变成了Tag + Data,将数据直接存储在了指针中
    • 当指针不够存储数据时,才会使用动态分配内存的方式来存储数据(堆空间)
  • 判断是否是Tagged Pointer:
    • 对于Mac平台,指针的二进制最低有效位是1时,该指针为Tagged Pointer
    • 对于iOS平台,指针的二进制最高有效位是1时,该指针为Tagged Pointer
  • objc_msgSend能识别Tagged Pointer,直接从指针中提取数据,节省了调用开销(不必在类对象的方法列表中查找方法)
  • 优点:存储数据节省了内存开销,提取数据节省了调用开销
阅读全文 »

加速计物理特性

iOS设备内的加速计是个三轴元件,能够检测到三维空间中的运动或重力。也就是说,加速计不但可以指示用户握持设备的方式(同自动旋转功能),而且可以指示设备是否被放在桌子上,甚至指示正面朝上还是朝下。加速计可以测量重力。加速计返回值为1.0时,表示在特定方向上感知到的重力是1g。

  • 静止握持设备,没有任何运动,地球引力对其施加的力大约为1g
  • 纵向竖直地握持,那么设备会检测并报告在其y轴上的力大约为1g
  • 如果以一定角度握持,那么1g的力会分布到不同的轴上,这取决于握持的方式。在以45度角握持时,1g的力会均匀地分解到两个轴上。

如果检测到的加速计值远大于1g,那么可以判断这是突然运动。正常使用时,加速计在任一轴上都不会检测到大于1g的值。如果摇动、投掷或设备坠落,那么加速计便会在一个或多个轴上检测到很大的力。

阅读全文 »

1.PickerView设置选中行样式

比如需要设置背景色,在自定义pickerView的layoutSubviews方法中添加代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//修改选中行的背景色
for (UIView *subView in self.subviews) {
if(subView.subviews.count){
UIView *contentView = subView.subviews[0];
for (UIView *contentSubView in contentView.subviews) {
if(contentSubView.center.y == contentView.center.y){
if(_selectBackView != contentSubView){
_selectBackView.backgroundColor = [UIColor clearColor];
_selectBackView = contentSubView;
_selectBackView.backgroundColor = kColorFromHex(0xEBEBEB);
}
break;
}
}
break;
}

2.TableView的Section设置整体边框效果

在tableView代理类中,添加如下代码(可以使用CGPath,也可以用UIBezierPath):

OC版代码(使用了CGPath)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// 圆角弧度半径
CGFloat cornerRadius = 10.f;
// 设置cell的背景色为透明,如果不设置这个的话,则原来的背景色不会被覆盖
// cell.backgroundColor = UIColor.clearColor;

// 创建一个shapeLayer
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CAShapeLayer *backgroundLayer = [[CAShapeLayer alloc] init]; //显示选中
// 创建一个可变的图像Path句柄,该路径用于保存绘图信息
CGMutablePathRef pathRef = CGPathCreateMutable();
// 获取cell的size
// 第一个参数,是整个 cell 的 bounds, 第二个参数是距左右两端的距离,第三个参数是距上下两端的距离
CGRect bounds = CGRectInset(cell.bounds, 14, 0);

// CGRectGetMinY:返回对象顶点坐标
// CGRectGetMaxY:返回对象底点坐标
// CGRectGetMinX:返回对象左边缘坐标
// CGRectGetMaxX:返回对象右边缘坐标
// CGRectGetMidX: 返回对象中心点的X坐标
// CGRectGetMidY: 返回对象中心点的Y坐标

// 这里要判断分组列表中的第一行,每组section的第一行,每组section的中间行

// CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
if (indexPath.row == 0) {
// 初始起点为cell的左下角坐标
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
// 起始坐标为左下角,设为p,(CGRectGetMinX(bounds), CGRectGetMinY(bounds))为左上角的点,设为p1(x1,y1),(CGRectGetMidX(bounds), CGRectGetMinY(bounds))为顶部中点的点,设为p2(x2,y2)。然后连接p1和p2为一条直线l1,连接初始点p到p1成一条直线l,则在两条直线相交处绘制弧度为r的圆角。
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
// 终点坐标为右下角坐标点,把绘图信息都放到路径中去,根据这些路径就构成了一块区域了
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));

CGPathCloseSubpath(pathRef);

} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
// 初始起点为cell的左上角坐标
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
// 添加一条直线,终点坐标为右下角坐标点并放到路径中去
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
} else {
// 添加cell的rectangle信息到path中(不包括圆角)
CGPathAddRect(pathRef, nil, bounds);
}
// 把已经绘制好的可变图像路径赋值给图层,然后图层根据这图像path进行图像渲染render
layer.path = pathRef;
backgroundLayer.path = pathRef;
// 注意:但凡通过Quartz2D中带有creat/copy/retain方法创建出来的值都必须要释放
CFRelease(pathRef);
// 按照shape layer的path填充颜色,类似于渲染render
// layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
layer.fillColor = [UIColor whiteColor].CGColor;
layer.strokeColor = [UIColor clearColor].CGColor;

// view大小与cell一致
UIView *roundView = [[UIView alloc] initWithFrame:bounds];
// 添加自定义圆角后的图层到roundView中
[roundView.layer insertSublayer:layer atIndex:0];
roundView.backgroundColor = VIEW_COLOR;
// cell的背景view
cell.backgroundView = roundView;

// 以上方法存在缺陷当点击cell时还是出现cell方形效果,因此还需要添加以下方法
// 如果你 cell 已经取消选中状态的话,那以下方法是不需要的.
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:bounds];
backgroundLayer.fillColor = [UIColor whiteColor].CGColor;
[selectedBackgroundView.layer insertSublayer:backgroundLayer atIndex:0];
selectedBackgroundView.backgroundColor = UIColor.clearColor;
cell.selectedBackgroundView = selectedBackgroundView;
}

Swift版(使用了UIBezierPath)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let rowsCount:Int = tableView.numberOfRows(inSection: indexPath.section)

if indexPath.row == 0 {

let path:UIBezierPath = UIBezierPath.init(roundedRect: CGRect(x: 14, y: 0, width: cell.contentView.width - 14 * 2, height: cell.contentView.height), byRoundingCorners: [.topLeft,.topRight], cornerRadii: CGSize(width: 10, height: 0))

let shapLayer:CAShapeLayer = CAShapeLayer()
shapLayer.lineWidth = 1
shapLayer.strokeColor = UIColor.white.cgColor
shapLayer.fillColor = UIColor.clear.cgColor
shapLayer.path = path.cgPath
let maskLayer:CAShapeLayer = CAShapeLayer.init()
maskLayer.path = path.cgPath
cell.layer.mask = maskLayer
cell.layer.addSublayer(shapLayer)

}else if indexPath.row == rowsCount - 1 {

let path:UIBezierPath = UIBezierPath.init(roundedRect: CGRect(x: 14, y: 0, width: cell.contentView.width - 14 * 2, height: cell.contentView.height), byRoundingCorners: [.bottomLeft,.bottomRight], cornerRadii: CGSize(width: 10, height: 0))
let shapLayer:CAShapeLayer = CAShapeLayer()
shapLayer.lineWidth = 1
shapLayer.strokeColor = UIColor.white.cgColor
shapLayer.fillColor = UIColor.clear.cgColor
shapLayer.path = path.cgPath
let maskLayer:CAShapeLayer = CAShapeLayer.init()
maskLayer.path = path.cgPath
cell.layer.mask = maskLayer
cell.layer.addSublayer(shapLayer)

}else{
let path:UIBezierPath = UIBezierPath.init(roundedRect: CGRect(x: 14, y: 0, width: cell.contentView.width - 14 * 2, height: cell.contentView.height), byRoundingCorners: [.topLeft,.topRight], cornerRadii: CGSize(width: 0, height: 0))
let shapLayer:CAShapeLayer = CAShapeLayer()
shapLayer.lineWidth = 1
shapLayer.strokeColor = UIColor.white.cgColor
shapLayer.fillColor = UIColor.clear.cgColor
shapLayer.path = path.cgPath
let maskLayer:CAShapeLayer = CAShapeLayer.init()
maskLayer.path = path.cgPath
cell.layer.mask = maskLayer
cell.layer.addSublayer(shapLayer)
}
}

iOS中,KVO的基本原理是重写了被观察属性的setter方法。所以一般情况下只有通过调用setter方法对值进行改变才会触发KVO,直接访问实例变量修改值是不会触发KVO的。

对于NSMutableArray来说,当调用addObject、removeObject时,并不会触发它的setter方法。所以要KVO一个NSMutableArray,就需要用到(NSMutableArray *)mutableArrayValueForKey:(NSString *)key方法:

阅读全文 »

一、Substring

Swift 4 中有一个很大的变化就是String可以当做Collection来用,并不是因为String实现了Collection协议,而是String本身增加了很多Collection协议中的方法,使得String 在使用时看上去就是个Collection

1
2
3
4
5
let string = "IamBaylee"
print(string.prefix(3)) //"Iam"
print(string.suffix(6)) //"Baylee"
print(string.dropFirst()) //"amBaylee"
print(string.dropLast()) //"IamBayle"

比如上面的样例,我们使用一些Collection协议的方法对字符串进行截取,只不过它们的返回结果不是String类型,而是 Swift4 新增的Substring类型。

阅读全文 »

URL Scheme就是一种协议URL,同我们常见的http、ftp一样,是一种资源定位符。scheme表示一个URL的最初始位置,即://之前的字符,比如ftp、http。

在iOS中,因为苹果的沙盒机制,应用只能访问它声明可能访问的资源。但苹果允许App之间通过URL Scheme来实现App之间的互相调起。URL Scheme也是App之间互相调起的唯一标识。

要跳转到某个app,就要知道这个app的跳转协议是什么,需要传入什么参数。

阅读全文 »

UINavigationBar设置纯色背景

1
2
[nav.navigationBar setBarTintColor:MAIN_COLOR];
[nav.navigationBar setTintColor:MAIN_COLOR];

问题:导航条颜色透明度不是1,颜色略淡。

####UINavigationBar设置为图片

1
2
UIImage *bgImage = [[UIImage createImageWithColor:MAIN_COLOR] resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
[[UINavigationBar appearance] setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];

注意设置为图片后controller视图的位置变化:

阅读全文 »

标准等价是指两个Unicode标量序列在语言学层面是否相等。对于两个字符或字符串,如果他们具备相同的语言学含义和外观,那么无论是否使用相同的Unicode标量创建,都认为两者相等。

从内部实现上来说,Swift字符串是由Unicode标量实现的,Unicode标量是21位的数,表示Unicode标准中的一个特定字符。一个字符是有一个或多个Unicode标量构成的。多个Unicode标量组成的字符被称作组合标量。

比如,U+0301表示可组合的重音符号(‘),这个标量与它前面的字符组合,将重音符号放在它前面的标量所对应的字符上面。

1
let aAcute = "\u{0061}\u{0301}"	//aAcute表示à

但在Unicode标准中,已经为à提供了组合过的形式,有一个专属的标量,实际上不用分为字母和重音符号两部分。

阅读全文 »

  1. 点击按钮分享到好友或朋友圈没有反应,URLScheme正确,SendMessageToWXReq各个参数都不为空

    问题所在:分享图片太大,微信分享图片最大32k,图片压缩后进行分享

  2. iOS定位权限提示框一闪而过

    产生原因:CLLocationManager的实例被释放了

    解决方法:将其改为属性@property (nonatomic, strong)CLLocationManager *manager;

  3. HTTP请求报错:Error Code=-999 “cancelled”:

    产生原因:AFNetworking中- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust forDomain:(nullable NSString *)domain;方法判断证书无效(或没有证书)后返回NO,导致当前请求被cancel。

    阅读全文 »