天天看點

給大家分享一下最近開發遇到的一些坑

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

繼續閱讀