给大家分享一下最近开发遇到的一些坑
2017-03-06 15:56
621 查看
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等操作了
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;
![](https://oscdn.geek-share.com/Uploads/Images/Content/202009/29/dc0eb1b63bdf5e92f2395ec032f6591c)
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;
}
}
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;
}
}
相关文章推荐
- 最近一阵子写C#的项目,遇到的一些问题以及解决的办法,跟大家分享一下```
- 最近在学JAVA,遇到一些Eclipse的问题,分享出来给大家
- APICloud框架——总结一下最近开发APP遇到的一些问题 (二)
- APICloud框架——总结一下最近开发APP遇到的一些问题 (三)
- 蛙蛙推荐:最近开发一个小型的OA,把常用代码和大家分享一下先
- 开发中自己遇到的问题和大家分享一下java.io.IOException: tmpFile.renameTo(classFile) failed and(和) failed (2: No suc
- APICloud框架——总结一下最近开发APP遇到的一些问题
- 最近项目上没有很忙的事情,想利用这段时间来写写这几年来在项目开发上的一些心得体会,乐于跟大家分享
- 把遇到过的对.Net线程的一些问题和误解集中起来和大家分享,也希望大家能一起补充,热烈欢迎讨论(转)
- 最近加入了控件开发团队,发现一些基础的东西,转发上来方便大家学习(转)
- 最近oracle开发中遇到的一些问题及解决办法(五)待续
- 最近招了几个新员工,大家有什么好的管理经验,分享一下
- 把遇到过的对.Net线程的一些问题和误解集中起来和大家分享转载
- 最近oracle开发中遇到的一些问题及解决办法(四):调试存储过程--待续
- 最近在学Java,想想后面会遇到很多问题,写个博客分享一下
- 最近发现一个很好的G站,99wg站,和大家分享一下的了
- (最近学习过程中发现的,觉得不错,和大家分享一下)MFC中OnDraw与OnPaint的区别
- 关于网站的推广方面,自己的一些愚见和大家分享一下。
- 最近oracle开发中遇到的一些问题及解决办法(三)待续
- 最近写了一些关于.net中js操作的通用函数,希望和大家分享 2