一、結(jié)構(gòu)體的使用struct
結(jié)構(gòu)體能更加方便的定義好不同的屬性,我們使用起來更方便,減少了我們使用中進(jìn)一步去手動(dòng)獲取數(shù)據(jù)。
1、定義結(jié)構(gòu)體:
struct TYTCommonResponse {
NSUInteger code;
NSString *_Nullable msg;
id _Nullable data; //上面三個(gè)手動(dòng)拆分的數(shù)據(jù)
id _Nullable rowData; // 接口返回原數(shù)據(jù)
};
聲明結(jié)構(gòu)體:
typedef struct TYTCommonResponse TYTCommonResponse;
12、使用結(jié)構(gòu)體回傳數(shù)據(jù)
比如我們封裝好網(wǎng)絡(luò)請求,response的數(shù)據(jù)需要進(jìn)一步解析。
- (void)responseStructWithURLString:(NSString *)path onSuccess:(nullable void (^)(struct TYTCommonResponse))success responseObject:(id _Nullable)responseObject
{
TYTCommonResponse resultObj;
resultObj.code = [responseObject[@"code"] integerValue];
resultObj.msg = responseObject[@"msg"];
resultObj.data = responseObject[@"data"];
resultObj.rowData = responseObject;
TYTLog(@"返回?cái)?shù)據(jù)(%@):%@", path, [NSString stringWithFormat:@"\ncode: %zd\nmsg: %@\nrowData: %@", resultObj.code, resultObj.msg, resultObj.rowData]);
[TYTResultCodeHandler handleMessageWithCode:resultObj.code data:responseObject];
if (success) {
success(resultObj);
}
}
二、WKWebView中的Cookie
Cookie是我們?nèi)粘g覽的網(wǎng)頁頁面為了識別具體的用戶身份,而在request Header加帶的一種數(shù)據(jù)。有了這個(gè)數(shù)據(jù),你要瀏覽的的網(wǎng)頁就知道你是哪個(gè)用戶。
舉個(gè)例子:如果瀏覽網(wǎng)站的時(shí)候沒有Cookie或者Cookie失效了,那么就會(huì)出現(xiàn)這種情況。你隔了好多天訪問微博網(wǎng)頁,點(diǎn)擊的標(biāo)簽明明之前保存的是你登錄之后的微博首頁,可以直接看到你的好友動(dòng)態(tài)。但是現(xiàn)在它卻給你跳轉(zhuǎn)了登錄頁面先要你登錄。就是因?yàn)槟鉉ookie失效導(dǎo)致的。微博沒辦法判斷你是否是有效用戶了。
另外,我們需要特別注意,在頁面跳轉(zhuǎn)的時(shí)候,網(wǎng)站有時(shí)候會(huì)自己在request Header中加入一些Cookie,也作為身份校驗(yàn)的一部分。
Cookie可以由native端設(shè)置也可以由服務(wù)端來設(shè)置。
客戶端想要打印出webView中的Cookie,就有四個(gè)地方可選了:
1、native端設(shè)置Cookie的時(shí)候
2、獲取request Header
3、獲取document.cookie的內(nèi)容
4、獲取response header中的set-cookie
WKWebView中設(shè)置Cookie的方案:

適配iOS11.0~iOS11.2
蘋果在iOS11.0推出了WKHTTPCookieStore,這個(gè)類對WKWebView的效果和NSHttpCookieStorage對UIWebView的效果一樣,并且也是零延遲。在請求發(fā)起的時(shí)候,我們只需要將Cookie設(shè)置到WKHTTPCookieStore就可以了。
// 1. 設(shè)置Cookie
WKWebsiteDataStore *store = [WKWebsiteDataStore defaultDataStore];
[store.httpCookieStore setCookie:cookie completionHandler:^{
NSLog(@“cookie添加成功”);
}];
// 2. 加載請求
[self.wkwebView loadRequest: request];
適配iOS11.3
WKHTTPCookieStore第一次設(shè)置Cookie之后,發(fā)起請求,Cookie不能同步到request Header中。此時(shí),webView重新請求頁面,WKHTTPCookieStore的作用恢復(fù)正常,之后請求其他的webView也不會(huì)出現(xiàn)Cookie丟失的問題。 總結(jié)就是在iOS11.3中,WKHTTPCookieStore設(shè)置Cookie之后,第一次發(fā)起請求Cookie會(huì)丟失,第二次及以后的請求的Cookie沒有問題。
解決方式是,我們在執(zhí)行l(wèi)oadRequest之前主動(dòng)設(shè)置一次Cookie。我這里是在didFinishLaunchOption:中設(shè)置的。設(shè)置完之后,就沒有問題了,注意,他是單利奧。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
WKWebsiteDataStore *store = [WKWebsiteDataStore defaultDataStore];
[store.httpCookieStore setCookie:cookie completionHandler:^{
NSLog(@“cookie添加成功”);
}];
}
三、Bundle 和 xcassets 的主要區(qū)別
簡述:
1、工程中所有使用 Asset Catalog 管理的圖片(在 .xcassets 文件夾下),最終輸出的時(shí)候,都會(huì)被壓縮到 Assets.car 內(nèi)。
2、反之,不在 Assets.car 內(nèi)的,我們將它統(tǒng)一歸類為 Bundle 管理的。
Bundle 和 xcassets 的主要區(qū)別有:
1、xcassets 里面的圖片,只能通過 imageNamed 加載。Bundle 還可以通過 imageWithContentsOfFile 等方式。
2、xcassets 里的 2x 和 3x,會(huì)根據(jù)具體設(shè)備分發(fā),不會(huì)同時(shí)包含。而 Bundle 會(huì)都包含。(App Slicing)
3、xcassets 內(nèi),可以對圖片進(jìn)行 Slicing,即裁剪和拉伸。Bundle 不支持。所以,Bundle會(huì)使包的體積變大。
4、Bundle 內(nèi)支持多語言,xcassets 不支持。
xcassets使用 imageNamed 創(chuàng)建的 UIImage,會(huì)立即被加入到 NSCache 中(解碼后的 Image Buffer),直到收到內(nèi)存警告的時(shí)候,才會(huì)釋放不在使用的 UIImage。
而 Bundle的imageWithContentsOfFile方式,它每次都會(huì)重新申請內(nèi)存,相同圖片不會(huì)緩存。
所以,xcassets 內(nèi)的圖片,加載后會(huì)產(chǎn)生緩存,Bundle則不會(huì),所以在開發(fā)中,常用的,較小的圖,應(yīng)該放在 xcassets 內(nèi)管理。而大圖應(yīng)該放在 Bundle 內(nèi)管理。
三、宏定義調(diào)用方法
以block的方式調(diào)用,每次換行都要添加\,否則報(bào)錯(cuò)。
#pragma mark - 適配ios11 和iPhone X
#define isiPhoneX \
({BOOL isPhoneX = NO;\
if (@available(iOS 11.0, *)) {\
isPhoneX = [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom > 0.0;\
}\
(isPhoneX);})
// 判斷是否是iPhone 5
#define isiPhone5 Screen_Width == 320
// iPhoneX的狀態(tài)欄的高度
//#define TYT_StatusBarHeight (isiPhoneX ? 44.f : 20.f)
#define TYT_StatusBarHeight \
^(){\
if (@available(iOS 13.0, *)) {\
UIStatusBarManager *statusBarManager = [[[UIApplication sharedApplication] windows] objectAtIndex:0].windowScene.statusBarManager;\
return statusBarManager.statusBarFrame.size.height;\
}else{\
return [[UIApplication sharedApplication] statusBarFrame].size.height;\
}\
}()
// iPhoneX的導(dǎo)航欄的高度
#define TYT_NavBarHeight \
^(){\
if (@available(iOS 13.0, *)) {\
UIStatusBarManager *statusBarManager = [[[UIApplication sharedApplication] windows] objectAtIndex:0].windowScene.statusBarManager;\
return statusBarManager.statusBarFrame.size.height+44;\
}else{\
return [[UIApplication sharedApplication] statusBarFrame].size.height+44;\
}\
}()
四、Block作為返回值使用
__nullable:可以為空
這里遇到一個(gè)崩潰,設(shè)置為nil,打斷點(diǎn)發(fā)現(xiàn)completion是null,就是但是completion沒有判空,導(dǎo)致崩潰,加了判空就可以了。
[self startAreaSelViewAnimation:nil];
#pragma mark -- 開始動(dòng)畫
- (void)startAreaSelViewAnimation:(void (^ __nullable)(BOOL finished))completion{
[UIView animateWithDuration:0.25 animations:^{
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
_backGroundView.frame = CGRectMake(0, _backGroundView.frame.origin.y, Screen_Width, _newBgHeight);//locationView.y+locationView.height
} completion:^(BOOL finished) {
if (completion != nil) {
completion(finished);
}
}];
}
五、Xcode 14.3升級后運(yùn)行報(bào)錯(cuò)
模擬器:
ld: file not found: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphonesimulator.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
真機(jī):
ld: file not found: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphoneos.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
由于Xcode 14.3版本移除了ARC相關(guān)的庫,從而導(dǎo)致一些默認(rèn)部署目標(biāo)是iOS 8版本的第三方庫出現(xiàn)報(bào)錯(cuò)。只要最低部署目標(biāo)不低于iOS 9版本,運(yùn)行項(xiàng)目時(shí)就不會(huì)去鏈接ARC相關(guān)的庫,也就不會(huì)出現(xiàn)找不到庫的報(bào)錯(cuò)。
解決方法
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
end
end
end
11.0是你支持的最低版本,并不是固定的。
解決方案鏈接
六、自定義AlertView的文字顏色
Title使用富文本。
alterView使用KVC設(shè)置顏色。
- (void)createWaitActivateAlertView{
NSString *message = @"您的企業(yè)賬戶未完成激活,不能發(fā)”專票“貨源,請盡快激活。激活之前,您只能發(fā)布不開票或普票貨源。";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:message];
[attrStr addAttribute:NSForegroundColorAttributeName
value:kUIColorFromRGB(0x666666)
range:NSMakeRange(0, 28)];
[attrStr addAttribute:NSForegroundColorAttributeName
value:kUIColorFromRGB(0xFE9D09)
range:NSMakeRange(attrStr.length-19, 19)];
[attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, attrStr.length)];
UIAlertController *alterView = [UIAlertController alertControllerWithTitle:@"您的企業(yè)賬戶待激活" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"暫不激活" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alterView setValue:attrStr forKey:@"attributedMessage"];
[cancle setValue:kUIColorFromRGB(0x999999) forKey:@"titleTextColor"];
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"去激活" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
[sure setValue:kUIColorFromRGB(0x3194EF) forKey:@"titleTextColor"];
[alterView addAction:cancle];
[alterView addAction:sure];
[self presentViewController:alterView animated:YES completion:nil];
}
七、限定鍵盤輸入的內(nèi)容
TextField只能輸入字母和數(shù)字+字?jǐn)?shù)限制。
傳送門
1、設(shè)置鍵盤樣式
self.textField.keyboardType = UIKeyboardTypeASCIICapable;
2、設(shè)置宏條件
#define NUM @"0123456789"
#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
3、textFieldDelegate代理方法
// 只能輸入字母和數(shù)字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ALPHANUM] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
// 小寫字母轉(zhuǎn)大寫字母
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.text = [textField.text uppercaseString];
}
八、組件化block傳參
需求一個(gè)組件調(diào)用另一個(gè)組件,需要獲取返回的參數(shù),有兩種方式,一個(gè)是delegate,另一個(gè)就是block。
1、delegate
通過設(shè)置代理,使用responseSelect的方式直接調(diào)用方法,方法返回?cái)?shù)據(jù)之后,使用代理回傳參數(shù)。調(diào)用中CTM的時(shí)候不需要在調(diào)用VC中聲明代理。
1.1 中間件方法
- (UIViewController *)Main_Car_complaintShowBigImagesViewControllerWithDelegate:(id)delegate dataArray:(NSArray *)dataArray index:(NSInteger)index isHidden:(BOOL)isHidden {
NSMutableDictionary *params = @{}.mutableCopy;
if (delegate) {
params[@"delegate"] = delegate;
}
params[@"index"] = @(index);
return [self performTarget:MainApp_Car_TargetName action:@"complaintShowBigImagesViewController" params:params shouldCacheTarget:NO];
}
1.2 組件中被調(diào)用的事件
- (UIViewController *)Action_complaintShowBigImagesViewController:(NSDictionary *)params {
id data = params[@"data_array"];
if (![data isKindOfClass:[NSArray class]]) {
return nil;
}
NSArray *images = (NSArray *)data;
TYTComplaintShowBigImagesViewController *viewImage = [[TYTComplaintShowBigImagesViewController alloc] init];
viewImage.delegate = params[@"delegate"];
viewImage.dataArray = [NSMutableArray arrayWithArray:images];
viewImage.isHidden = [params[@"is_hidden"] boolValue];
viewImage.currentIndex = [params[@"index"] intValue];
return viewImage;
}
2、block
2.1 中間件方法:
- (void)Shipment_shipViewControllerExcellentCar:(void(^)(NSDictionary *response,BOOL isExcellentCar))callBack{
NSMutableDictionary *params = [NSMutableDictionary new];
if(callBack){
params = @{
@"block": callBack
}.mutableCopy;
}
[CT() performTarget:Shipment_TargetName action:@"shipmmentManagerExcellentCar" params:params shouldCacheTarget:NO];
}
2.2 組件內(nèi)被CTM調(diào)用的方法:
- (void)Action_shipmmentManagerExcellentCar:(NSDictionary *)params
{
TYTShipmmentManager *manager = [[TYTShipmmentManager alloc] init];
void (^callBack)(NSDictionary *response, BOOL isExcellentCar) = params[@"block"];
if (callBack) {
manager.callBack = callBack;
}
[manager queryExcellentCarResult:^(NSDictionary *response, BOOL isExcellentCar) {}];
}
2.3 TYTShipmmentManager網(wǎng)絡(luò)請求賦值
注意,這里的self.callBack(GlobalBlock)一定要有值,否則會(huì)崩潰,所以如果CTM沒有調(diào)用傳參復(fù)制的話,我們就需要手動(dòng)復(fù)制。
- (void)queryExcellentCarResult:(TYTGShipmmentManagerBlock)callBack{
if (self.callBack == nil) {
self.callBack = callBack;
}
callBack = self.callBack;
[[TYTCommonRequestManager sharedManager] fetchResponseMethod:GET forPlatURLString:@"deposit/queryEquity.action" parameters:@{} success:^(struct TYTCommonResponse response) {
if(response.code == 200){
NSDictionary *dataDic = (NSDictionary *)response.data;
if( [dataDic[@"isJoin"] intValue] == 1 && [dataDic[@"blackStatus"] intValue] == 0 && [dataDic[@"authStatus"] intValue] == 1){
self.callBack(dataDic, YES);
}else{
self.callBack(dataDic, NO);
}
}else{
self.callBack(@{},NO);
}
} failure:^(NSError * _Nonnull error) {
self.callBack(@{},NO);
}];
}