天天看点

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

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界面禁用手势;