给大家分享一下最近开发遇到的一些坑

1、获取音频文件的时长;

2、CALayer position contains NaN:[182.5 nan];

3、Can't add self as subview;

4、iOS中修改WebView默认的User Agent(用户代理);

5、The certificate used to sign "XXX" has either expired or has been revoked;

6、右滑pop界面禁用手势;

问题的解决办法:

1、获取音频文件的时长;
-(CGFloat)durationWithMusic:(NSURL *)urlPath
{
    AVURLAsset *audioAsset=[AVURLAsset assetWithURL:urlPath];
    
    CMTime durationTime = audioAsset.duration;
    
    CGFloat reultTime=0;
    
    reultTime = CMTimeGetSeconds(durationTime);
    
    return reultTime;
    
}

2、CALayer position contains NaN:[182.5 nan];

Terminating app due to uncaught exception ‘CALayerInvalidGeometry’, reason: ‘CALayer position contains NaN: [nan 38]’
这错误发生时,一般打开NSZombieEnable,用个全局断点,会定位到错误发生的位置,如果不出所料,错误是与CGRect相关的,也就是reason中提示的position的问题,

NaN,是Not a Number的缩写。 NaN 用于处理计算中出现的错误情况。
简单说,那个position可能包含了异常值,从内存方面来看一下会比较好。
1、一般和layer相关的也可能会有重复设值、刷新和释放的情况
2、在设备高度及宽度时,CGRectMake设置等中存在错误,看看是不是除0等操作了

self.pictureView.frame = CGRectMake(margin, 65 + textH, maxSize.width, news!.height * (maxSize.width / news!.width))

如代码,当我的news!.width == 0时,maxSize.width / 0 是不合法的,所以作为CGRect的参数时,就出现了上面的这个crash

3、Can't add self as subview;

从日志上来看崩溃是在main函数,定位不到具体的地方。

像这种crash,一般最简单地情况是:

[self.view addSubview:self.view];

这种确实会直接导致崩溃,但不是引起原因。

另一种错误原因是说一次push了两次,动画被打断后引起的crash。

对push的UIViewController来进行进行控制。

另一种方法:

创建一个分类,拦截控制器入栈\出栈的方法调用,通过安全的方式,确保当有控制器正在进行入栈\出栈操作时,没有其他入栈\出栈操作。

此分类用到运行时 (Runtime) 的方法交换Method Swizzling,因此只需要复制下面的代码到自己的项目中,此 bug 就不复存在了。

这里有我写好的关于UINaviationController的两个类别,希望可以帮助到大家: https://github.com/hbblzjy/UINavigationController

4、iOS中修改WebView默认的User Agent(用户代理);

在AppDelegate添加代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //修改app默认UA
    @autoreleasepool {
        UIWebView* tempWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
        NSString* userAgent = [tempWebView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
        //        NSLog(@"------%@",userAgent);
        NSString *executableFile = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleExecutableKey];
        NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];
        NSString *ua = [NSString stringWithFormat:@"%@ %@/%@",
                        userAgent,
                        executableFile,version];
        //        NSLog(@"------%@",ua);
        
        [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent" : ua, @"User-Agent" : ua}];
#if !__has_feature(objc_arc)
        [tempWebView release];
#endif  
    }
    return YES;
}

//在网页中
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"ua=======%@",[request valueForHTTPHeaderField:@"User-Agent" ]);
    //判断是否是单击
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSURL *url = [request URL];
        [[UIApplication sharedApplication]openURL:url];
        return NO;
    }
    return YES;
}

5、The certificate used to sign "XXX" has either expired or has been revoked;


6、右滑pop界面禁用手势;

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 开启
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hbblzjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值