iOS开发技巧

1. 共享倒计时结束后自动消失。

iOS 12及以下。

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getSysBroadcastPickerStandaloneViewController:) name:@"ShowRPBroadcastPickerStandaloneViewControllerNofi" object:nil];

//实现监听方法
-(void)getSysBroadcastPickerStandaloneViewController:(NSNotification *)notification
{
    UIViewController *vc = notification.userInfo[@"viewController"];
    if (vc) {
        self.systemBroadcastPickerViewController = vc;
    }
}


    if (self.systemBroadcastPickerViewController) {
        [self.systemBroadcastPickerViewController dismissViewControllerAnimated:YES completion:nil];
    }

//
//  UIViewController+DismissBroadcast.m
//  TangClient
//
//  Created by chip on 2021/7/27.
//  Copyright © 2021 QS. All rights reserved.
//

#import "UIViewController+DismissBroadcast.h"
@implementation UIViewController (DismissBroadcast)
+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self swizzleSelector:@selector(presentViewController:animated:completion:) withAnotherSelector:@selector(mna_presentViewController:animated:completion:)];
    });
}

+ (void)swizzleSelector:(SEL)originalSelector withAnotherSelector:(SEL)swizzledSelector
{
    Class aClass = [self class];

    Method originalMethod = class_getInstanceMethod(aClass, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector);

    BOOL didAddMethod =
        class_addMethod(aClass,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));

    if (didAddMethod) {
        class_replaceMethod(aClass,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

#pragma mark - Method Swizzling

- (void)mna_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
    if ([NSStringFromClass(viewControllerToPresent.class) isEqualToString:@"RPBroadcastPickerStandaloneViewController"]) {
        NSDictionary *dict = @{@"viewController":viewControllerToPresent};
        [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"ShowRPBroadcastPickerStandaloneViewControllerNofi" object:nil userInfo:dict]];
        
        [self mna_presentViewController:viewControllerToPresent animated:flag completion:completion];
    } else {
        /**
         MEETINGQA-21063
         Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modal view controller on itself. Presenting controller is .
         */
        if (self != viewControllerToPresent) {
            [self mna_presentViewController:viewControllerToPresent animated:flag completion:completion];
        }
    }
    
}

@end

iOS13及以上。

//共享成功后调用。
[weakSelf.presenter backToHost];

- (void)backToHost {
    if(@available(iOS 10.0, *)) {
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"quanshiHost://com.quanshi"] options:@{
            
        } completionHandler:nil];
        
    }else{
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString:@"quanshiHost://com.quanshi"]];
    }
}

2.framework中有同名文件导致iskindofclass失效

p/x 可以打印内存地址

使用分类实现类系统方法,可以小范围实现类似交换方法。

- (BOOL)lc_isKindOfClass:(Class)aClass
{
    // 0.参数为NULL,直接给结果
    if (aClass == NULL) {
        return NO;
    }
    
    BOOL value = [self isKindOfClass:aClass];
    
    // 1.系统方法已经断定类型
    if (value) {
        return YES;
    }
    
    NSBundle *objBundle = [NSBundle bundleForClass:self.class];
    NSBundle *clsBundle = [NSBundle bundleForClass:aClass];
    
    // 2.对象的类和类属于同一个bundle
    if (objBundle == clsBundle || [objBundle.bundlePath isEqualToString:clsBundle.bundlePath]) {
        return value;
    }
    
    // 3.二者来自于不同的bundle,根据名称补刀
    Class objCls = self.class;
    NSString *objClsName = nil;
    NSString *clsName = NSStringFromClass(aClass);
    
    do {
        objClsName = NSStringFromClass(objCls);
        value = [objClsName isEqualToString:clsName];
        
        if (value) {
            return YES;
        }
        
        objCls = class_getSuperclass(objCls);
    } while (objCls != NULL);
    
    return NO;
}

3.调试

连接手机。
device and simulars
open console
筛选关键词 项目名

4.webview白屏

webview白屏
原来是uiwebview 进化到wkwebview 原因是uiwebview内存消耗比较高 使用wkwebview内存更低
原理是wkwebview把加载和渲染放在两个进程里 白屏大概率是因为渲染进程因为内存等原因被干掉

监测白屏
在webview加载完的时间点 快照 再监测webview中的像素点 如果全白 在一定时间后重新reload

5.XCFramework

xcodebuild -create-xcframework -framework XXX.framework -output XXX.xcframework

6.使用masonry适配横竖屏下的安全距离

@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuide API_AVAILABLE(ios(11.0),tvos(11.0));
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideTop API_AVAILABLE(ios(11.0),tvos(11.0));
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideBottom API_AVAILABLE(ios(11.0),tvos(11.0));
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideLeft API_AVAILABLE(ios(11.0),tvos(11.0));
@property (nonatomic, strong, readonly) MASViewAttribute *mas_safeAreaLayoutGuideRight API_AVAILABLE(ios(11.0),tvos(11.0));

7.url的safe base 64

#pragma - 将saveBase64编码中的"-","_"字符串转换成"+","/",字符串长度余4倍的位补"="
+(NSData*)safeUrlBase64Decode:(NSString*)safeUrlbase64Str
{
    // '-' -> '+'
    // '_' -> '/'
    // 不足4倍长度,补'='
    NSMutableString * base64Str = [[NSMutableString alloc]initWithString:safeUrlbase64Str];
    base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
    base64Str = (NSMutableString * )[base64Str stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
    NSInteger mod4 = base64Str.length % 4;
    if(mod4 > 0)
        [base64Str appendString:[@"====" substringToIndex:(4-mod4)]];
    NSLog(@"Base64原文:%@", base64Str);
    return [GTMBase64 decodeData:[base64Str dataUsingEncoding:NSUTF8StringEncoding]];
    
}

#pragma - 因为Base64编码中包含有+,/,=这些不安全的URL字符串,所以要进行换字符
+(NSString*)safeUrlBase64Encode:(NSData*)data
{
    // '+' -> '-'
    // '/' -> '_'
    // '=' -> ''
    NSString * base64Str = [GTMBase64 stringByEncodingData:data];
    NSMutableString * safeBase64Str = [[NSMutableString alloc]initWithString:base64Str];
    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"+" withString:@"-"];
    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    safeBase64Str = (NSMutableString * )[safeBase64Str stringByReplacingOccurrencesOfString:@"=" withString:@""];
    NSLog(@"safeBase64编码:%@", safeBase64Str);
    return safeBase64Str;
}

你可能感兴趣的:(iOS开发技巧)