意在記錄一些開(kāi)發(fā)中遇到的問(wèn)題和處理方式方法 記錄一些小問(wèn)題的解決 以鑒需要的人 會(huì)長(zhǎng)期更新
1、APP icon 在iPhone上可以顯示 但在iPad上顯示不出來(lái)
解決方法:在info.plist里刪除CFBundleIcons~ipad 這個(gè)Key
iPhone項(xiàng)目在iPad中無(wú)法顯示圖標(biāo)解決方法
2、生成標(biāo)準(zhǔn)的整套iOS App icon 圖片(無(wú)alpha通道)
應(yīng)用的圖標(biāo)。遵循 Apple、 Google、 Microsoft 官方標(biāo)
圖片工廠— 移動(dòng)應(yīng)用圖標(biāo)生成工具,一鍵生成所有尺寸的應(yīng)用圖標(biāo)
3、Object/Model對(duì)象儲(chǔ)存到NSUserDefaults
//序列化對(duì)象并儲(chǔ)存
LHPersonModel *personModel = [[LHPersonModel alloc]init];
NSData *personData = [NSKeyedArchiver archivedDataWithRootObject: LHPersonModel];
[[NSUserDefaults standardUserDefaults] setObject:personData forKey:@"KPersonData"];
//取出數(shù)據(jù)并反序列化
NSData *personData = [[NSUserDefaults standardUserDefaults] objectForKey:@"KPersonData"];
LHPersonModel * personModel = [NSKeyedUnarchiver unarchiveObjectWithData:personData];
NSLog(@"Name:%@ Age:%@", personModel.name, @(personModel.age));
4、賬號(hào)/密碼/用戶名 去除左右兩側(cè)空格
產(chǎn)品反饋 有用戶前后多輸入空格導(dǎo)致無(wú)法登錄 空格肉眼又看不見(jiàn) so 需要過(guò)濾左右空格(中間空格不過(guò)濾)讓用戶可以登錄
NSString* str = @" 15910221559@163.com ";
NSString* res = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
5、MKMapvie iOS蘋(píng)果系統(tǒng)地圖 獲取比例尺&羅盤(pán) 更改其坐標(biāo)(支持iOS11+)
//比例尺
- (void)setMapScaleBarPosition:(CGPoint)mapScaleBarPosition
{
if (@available(iOS 11.0, *)) {
__block MKScaleView * mapScaleView = [self.mapView viewWithTag:11 * 11];
if ( mapScaleView == nil) {
mapScaleView = [MKScaleView scaleViewWithMapView:_mapView];
mapScaleView.scaleVisibility = MKFeatureVisibilityVisible;
mapScaleView.tag = 11 * 11;
mapScaleView.width = 100;
[self.mapView addSubview:mapScaleView];
}
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
mapScaleView.origin = mapScaleBarPosition;
} completion:nil];
} else {
// _mapView.showsScale = YES;
// Fallback on earlier versions
}
}
//羅盤(pán)
- (void)setMapCompassPosition:(CGPoint)mapCompassPosition
{
if (@available(iOS 11.0, *)) {
MKCompassButton *compass = [MKCompassButton compassButtonWithMapView:(MKMapView *)self.mapView];
compass.compassVisibility = MKFeatureVisibilityAdaptive;
compass.origin = mapCompassPosition;
compass.tag = 12 * 12;
[self.mapView addSubview:compass];
} else {
// _mapView.showsCompass = YES;
// Fallback on earlier versions
}
}
6、百度坐標(biāo)、國(guó)測(cè)局坐標(biāo)、地球坐標(biāo)之間的互轉(zhuǎn)糾偏
//百度坐標(biāo)轉(zhuǎn)國(guó)測(cè)局坐標(biāo)
+ (CLLocationCoordinate2D)baiduToGCJ02Tool:(CLLocationCoordinate2D)coodinate
{
double x = coodinate.longitude - 0.0065, y = coodinate.latitude - 0.006;
double z = sqrt(x * x + y * y) - 0.00002 * sin(y * earth_pi);
double theta = atan2(y, x) - 0.000003 * cos(x * earth_pi);
double lon = z * cos(theta);
double lat = z * sin(theta);
return CLLocationCoordinate2DMake(lat, lon);
}
//國(guó)測(cè)局坐標(biāo)轉(zhuǎn)百度坐標(biāo)
+ (CLLocationCoordinate2D)GCJ02ToBaidu:(CLLocationCoordinate2D)coodinate
{
double x = coodinate.longitude;
double y = coodinate.latitude;
double z = sqrt(x * x + y * y) + 0.00002 * sin(y * earth_pi);
double theta = atan2(y, x) + 0.000003 * cos(x * earth_pi);
double lon = z * cos(theta) + 0.0065;
double lat = z * sin(theta) + 0.006;
return CLLocationCoordinate2DMake(lat, lon);
}
//地球坐標(biāo)轉(zhuǎn)國(guó)測(cè)局坐標(biāo)
+ (CLLocationCoordinate2D)earthToGCJ02Tool:(CLLocationCoordinate2D)coodinate{
double dLat = [self transformLatWithLon:coodinate.longitude - 105.0 andLat:coodinate.latitude - 35.0];
double dLon = [self transformLonWithLon:coodinate.longitude - 105.0 andLat:coodinate.latitude - 35.0];
double radLat = coodinate.latitude / 180.0 * math_pi;
double magic = sin(radLat);
magic = 1 - de * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - de)) / (magic * sqrtMagic) * math_pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * math_pi);
return CLLocationCoordinate2DMake(coodinate.latitude + dLat, coodinate.longitude + dLon);
}
const double math_pi = 3.14159265358979324;
const double earth_pi = 3.14159265358979324 * 3000.0 / 180.0;
const double a = 6378245.0;
const double de = 0.00669342162296594323;
+ (double)transformLatWithLon:(double)x andLat:(double)y
{
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * sqrt(fabs(x));
ret += (20.0 * sin(6.0 * x * math_pi) + 20.0 * sin(2.0 * x * math_pi)) * 2.0 / 3.0;
ret += (20.0 * sin(y * math_pi) + 40.0 * sin(y / 3.0 * math_pi)) * 2.0 / 3.0;
ret += (160.0 * sin(y / 12.0 * math_pi) + 320 * sin(y * math_pi / 30.0)) * 2.0 / 3.0;
return ret;
}
//國(guó)測(cè)局坐標(biāo)轉(zhuǎn)地球坐標(biāo)
+ (CLLocationCoordinate2D)GCJ02ToEarthTool:(CLLocationCoordinate2D)coodinate
{
double dLat = [self transformLatWithLon:coodinate.longitude - 105.0 andLat:coodinate.latitude - 35.0];
double dLon = [self transformLonWithLon:coodinate.longitude - 105.0 andLat:coodinate.latitude - 35.0];
double radLat = coodinate.latitude / 180.0 * math_pi;
double magic = sin(radLat);
magic = 1 - de * magic * magic;
double sqrtMagic = sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - de)) / (magic * sqrtMagic) * math_pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * math_pi);
return CLLocationCoordinate2DMake(coodinate.latitude - dLat, coodinate.longitude - dLon);
}
.......
7、iOS 判斷當(dāng)前經(jīng)緯度位置coordinate 是否在中國(guó)大陸、中國(guó)臺(tái)灣、中國(guó)香港
8、推薦一個(gè)練習(xí)刷算法題的網(wǎng)站 現(xiàn)在也有了中國(guó)版 很nice的
9、run-ios React Native 運(yùn)行時(shí)報(bào)錯(cuò)
Found Xcode project AwesomeProject.xcodeproj
xcrun: error: unable to find utility "simctl", not a developer tool or in PATH
Command failed: xcrun simctl list devices
xcrun: error: unable to find utility "simctl", not a developer tool or in PATH
Xcode工具路徑不對(duì) 應(yīng)該是你裝過(guò)兩個(gè)Xcode 或者改過(guò)路徑 去Xcode>preferences>Locations command line tools:

10、React-native 報(bào)錯(cuò)集合
如果你正在學(xué)習(xí)入門(mén)RN 建議可以遇到error可以看一下這篇文章 新手遇到的問(wèn)題這里都有列舉 還有其他問(wèn)題可以留言 一起討論
React Native開(kāi)發(fā)錯(cuò)誤警告處理總結(jié)
11、APP啟動(dòng)頁(yè)時(shí)如何隱藏狀態(tài)欄
在info.plits文件中添加key Status bar is initially hidden = YES
就可以啦

11、 NSPredicate 謂詞的使用
OC中的謂詞操作是針對(duì)于數(shù)組類(lèi)型的,他就好比數(shù)據(jù)庫(kù)中的查詢操作,數(shù)據(jù)源就是數(shù)組,這樣的好處是我們不需要編寫(xiě)很多代碼就可以去操作數(shù)組,同時(shí)也起到過(guò)濾的作用,我們可以編寫(xiě)簡(jiǎn)單的謂詞語(yǔ)句,就可以從數(shù)組中過(guò)濾出我們想要的數(shù)據(jù)。非常方便。在Java中是沒(méi)有這種技術(shù)的,但是有開(kāi)源的框架已經(jīng)實(shí)現(xiàn)了此功能。
NSPredicate 謂詞的使用
12、切圓角的三種方式
切圓角是開(kāi)發(fā)app過(guò)程中經(jīng)常會(huì)用到的功能,但是使用不同的方式,性能損耗也會(huì)不同,下面會(huì)介紹3種切圓角的方法;其中,方法三的性能相對(duì)最好。
方法一:
使用cornerRadius進(jìn)行切圓角,在iOS9之前會(huì)產(chǎn)生離屏渲染,比較消耗性能,而之后系統(tǒng)做了優(yōu)化,則不會(huì)產(chǎn)生離屏渲染,但是操作最簡(jiǎn)單
iv.layer.cornerRadius = 30;
iv.layer.masksToBounds = YES;
方法二:
利用mask設(shè)置圓角,利用的是UIBezierPath和CAShapeLayer來(lái)完成
CAShapeLayer *mask1 = [[CAShapeLayer alloc] init];
mask1.opacity = 0.5;
mask1.path = [UIBezierPath bezierPathWithOvalInRect:iv.bounds].CGPath;
iv.layer.mask = mask1;
方法三:
利用CoreGraphics畫(huà)一個(gè)圓形上下文,然后把圖片繪制上去,得到一個(gè)圓形的圖片,達(dá)到切圓角的目的。
- (UIImage *)drawCircleImage:(UIImage*)image
{
CGFloat side = MIN(image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(side, side), false, [UIScreen mainScreen].scale);
CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, side, side)].CGPath);
CGContextClip(UIGraphicsGetCurrentContext());
CGFloat marginX = -(image.size.width - side) * 0.5;
CGFloat marginY = -(image.size.height - side) * 0.5;
[image drawInRect:CGRectMake(marginX, marginY, image.size.width, image.size.height)];
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
13、 藍(lán)牙項(xiàng)目中遇到打開(kāi)藍(lán)牙允許“xxx”連接到配件 這個(gè)系統(tǒng)提示怎么關(guān)閉 CBCentralManagerOptionShowPowerAlertKey
@{CBCentralManagerOptionShowPowerAlertKey:[NSNumber numberWithBool:NO]}
14、 iOS12 TableView section header/footer 間隔設(shè)置高度無(wú)效添加下面一句 配合使用
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10.f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return [UIView new];
}
15、 XCode10 組件化編譯不更新或運(yùn)行報(bào)錯(cuò)Multiple commands produce /路徑


Legacy Build System 更改后生效
16、 NSMutableAttributedString 使用富文本 圖片元素(NSTextAttachment)+ 文字 時(shí)不能居中對(duì)齊
富文本中使用NSMutableAttributedString 富文本字符串 實(shí)現(xiàn) 圖片 + 文字的Label 時(shí)遇到圖片在前時(shí) 整個(gè)文本不能居中對(duì)齊 正常情況下設(shè)置NSMutableParagraphStyle 文本格式就可以實(shí)現(xiàn)對(duì)齊效果 如下
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSMutableAttributedString *mAttStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ",insuranceText] attributes:@{NSFontAttributeName: [UIFont f12],
NSForegroundColorAttributeName:[UIColor colorWithHexString:@"BBBBBB"],
NSParagraphStyleAttributeName:paragraphStyle }];
但是遇到圖片在前面時(shí)不能對(duì)齊 我是這樣處理的 你可以嘗試一下 有更好的方式歡迎在評(píng)論中分享
//在字符串前加一個(gè)空格占位位
[@" " stringByAppendingString:insuranceText];
//圖片Attachment元素 insert插入1位置 就不會(huì)影響paragraphStyle.alignment的使用了
[mAttStr insertAttributedString:Attachment atIndex:1];
17、 Xcode 遇到報(bào)錯(cuò)Error: xib: Encountered an error communicating with IBAgent-iOS.
意思是與Xib通訊錯(cuò)誤 原因也很莫名其妙 不太清楚怎么造成的 解決方法 command + k / 重啟xocde / 重啟電腦 一步
18、 郵箱/電話號(hào)碼簡(jiǎn)單的脫密顯示(dah*******@gmail.com)
NSString *account = @"dahuangeya@gmail.com";
NSArray <NSString *>*accounts = [account componentsSeparatedByString:@"@"];
if (accounts.firstObject.length > 3) {
NSString *rangeStr = [accounts.firstObject substringWithRange:NSMakeRange(0, 3)];
NSString *str = [rangeStr stringByPaddingToLength:accounts.firstObject.length
withString:@"*"
startingAtIndex:0];
NSString *encryptionStr = [NSString stringWithFormat:@"%@@%@",str,accounts.lastObject];
NSLog(@"%@",encryptionStr);
//輸出:dah*******@gmail.com
}
19、根據(jù)圖片Url 獲取網(wǎng)絡(luò)圖片尺寸的處理方式 (準(zhǔn)確)
之前有個(gè)需求是根據(jù)用戶上傳的圖片鏈接按顯示比例鋪滿排版
常見(jiàn)的處理方式是解析圖片的header信息內(nèi)容格式(png、jpg、gif等)來(lái)獲取圖片尺寸(網(wǎng)上很多就不列舉了) 但有問(wèn)題是不夠準(zhǔn)確 經(jīng)常會(huì)計(jì)算錯(cuò)誤 導(dǎo)致排版異常 下面這種方式可以準(zhǔn)確的獲取圖片的寬高尺寸
/**
* 根據(jù)圖片url獲取圖片尺寸
*/
+ (CGSize)getImageSizeWithURL:(id)URL{
NSURL * url = nil;
if ([URL isKindOfClass:[NSURL class]]) {
url = URL;
}
if ([URL isKindOfClass:[NSString class]]) {
url = [NSURL URLWithString:URL];
}
if (!URL) {
return CGSizeZero;
}
CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGFloat width = 0, height = 0;
if (imageSourceRef) {
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
if (imageProperties != NULL) {
CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
#if defined(__LP64__) && __LP64__
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
}
#else
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat32Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat32Type, &height);
}
#endif
CFRelease(imageProperties);
}
CFRelease(imageSourceRef);
}
return CGSizeMake(width, height);
}
20、在iOS 10 裝載UICollectionView / UITableView 的VC頁(yè)面在push下級(jí)頁(yè)面再pop回來(lái)事content內(nèi)容被下壓20像素
在iOS10系統(tǒng)中 開(kāi)發(fā)時(shí)遇到push頁(yè)面返回后 UICollectionView / UITableView content被下壓20像素 試了下面各種屬性設(shè)置并沒(méi)有什么卵用
self.automaticallyAdjustsScrollViewInsets = NO;
self.edgesForExtendedLayout = UIRectEdgeNone;
后來(lái)網(wǎng)上有位兄dei貼出來(lái)這樣一段代碼
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
[self.view addSubview:view];
mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 75, SCREEN_WIDTH, 60 * 3)];
mainTableView.backgroundColor = [UIColor yellowColor];
mainTableView.dataSource = self;
mainTableView.delegate = self;
[self.view addSubview:mainTableView];
思路清奇~ 試了下問(wèn)題確實(shí)得以神奇解決 但沒(méi)有說(shuō)明具體原因 猜測(cè)是因?yàn)閟afeArea 安全顯示區(qū)域做的一些系統(tǒng)適配 會(huì)作用于第一個(gè)contentView
嘗試不同的解決方式時(shí) 發(fā)現(xiàn)我們的TabBar因?yàn)槭亲远x的 NavBar也是自定義的 所以說(shuō)都不是使用的系統(tǒng)的控件 當(dāng)我嘗試把VC外用UINavigtionController 包裹一層時(shí)也可以解決這個(gè)問(wèn)題 猜測(cè)系統(tǒng)在這方面其實(shí)已經(jīng)對(duì)自己的控件做了適配 所以我想說(shuō)的是 不要過(guò)度自定義 這些控件完全可以遵循系統(tǒng)的方式來(lái)擴(kuò)展
MAC VNC://使用別名連接屏幕共享失敗的問(wèn)題原因
首先你要確認(rèn)你在偏好設(shè)置->共享->打開(kāi)屏幕共享 是開(kāi)啟的 然后檢查一下選項(xiàng)設(shè)置是否正確 在看一下自己的防火墻有沒(méi)有開(kāi)啟 確認(rèn)是同一個(gè)局域網(wǎng)
如果你設(shè)置了別名(自定義名稱) 系統(tǒng)會(huì)在下方提示你其他用戶可以通過(guò) vnc://LH/ 或通過(guò)在“訪達(dá)”邊欄中查找“LH”來(lái)訪問(wèn)您的電腦屏幕。
但這樣有可能還是鏈接不上 是因?yàn)槟愕脑O(shè)置了別名 別人找不到你 這時(shí)你可以通過(guò)你的網(wǎng)絡(luò)IP來(lái)替換別名建立鏈接 打開(kāi)網(wǎng)絡(luò)偏好設(shè)置 用vnc訪問(wèn)你的IPvnc://192.168.1.18/就可以嘗試屏幕共享了
21 iOS 如果讓子視圖View一直保持在最上層顯示
self.tableView.mj_header.layer.zPosition = MAXFLOAT;