iOS 开发中遇到的问题(客户端篇)

在项目开发中,自己遇到一些问题及解决方法,不断更新中。

(1)UINavigationBarUITabBar上有一条横线,是ShadowImage,默认是黑色的。在项目开发中,可以改变其图片和颜色。在下图个人热点图中可以看到导航栏下面的黑线。

[self.navigationController.navigationBar  setShadowImage:[UIImage imageWithColor:MyColor]];

(2) statusBar默认的高度是20.0f,在使用微信或者QQ通话,热点等功能进入后台时,statusBar的高度会变为40.0f,下方的布局也会发生变化,在此要根据statusBar的变化调整布局,设置监听监听其变化。

打开热点时`statusBar`高度变化

监听对象为UIApplicationDidChangeStatusBarFrameNotification

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(statusBarFrameWillChange:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];//添加监听

[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];//移除监听

当前statusBar的高度也是可以获取的:

NSValue *rectValue = [notification.userInfo objectForKey:UIApplicationStatusBarFrameUserInfoKey];
  CGRect statusRect = [rectValue CGRectValue];
    CGRect statusFrame = [self.view convertRect:statusRect fromView:[[UIApplication sharedApplication]keyWindow]];
    CGFloat statusHeight = statusFrame.size.height;

(3)在iPad开发时,使用iOS 9系统会出现tableviewCell的位置变化,在开发中默认与右侧是15个像素,可是现在明显大的多,这是因为在iOS 9后tableview的一个属性发生了变化。

tableviewCell位置变化

需要调整

tableView.cellLayoutMarginsFollowReadableWidth = NO;

补充,因为方法是iOS9之后出现的,因此在调用时需要判断系统是否大于9.0

if([UIDevice currentDevice].systemVersion.floatValue >= 9.0){
      tableView.cellLayoutMarginsFollowReadableWidth = NO;
    }

(4)在做直播功能模块,使用到 弹幕 的功能(弹幕使用第三方库BarrageRenderer),弹幕为横屏自动开启,竖屏时关闭。在测试用发现弹幕有时开有时关,最终发现在屏幕横放时无法显示。原因是设置方法出现错误。

通过[UIDevice currentDevice].orientation 的状态判断横竖屏会出现错误,因为其枚举类型是UIDeviceOrientation,在查看其枚举类型时会发现其除了Portrait LandscapeLeft LandscapeRight PortraitUpsideDown外还有FaceUp FaceDown两个状态,忽略对其的设置会产生影响。

UIDeviceOrientation 枚举

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
        UIDeviceOrientationUnknown,
        UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
        UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
        UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
        UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
        UIDeviceOrientationFaceUp,              // Device oriented flat, face up
        UIDeviceOrientationFaceDown             // Device oriented flat, face down
} __TVOS_PROHIBITED;

在对其使用时还可以将其转换成UIInterfaceOrientation,因为后者只有常见的几种类型。

UIDeviceOrientation orientation             = [UIDevice currentDevice].orientation;
  UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;

UIInterfaceOrientation枚举

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
      UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
      UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
      UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
      UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
      UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;

另外在横竖屏切换时我们会对屏幕的状态做监听,通常监听的是UIDeviceOrientationDidChangeNotification,监听得到的结果是UIDeviceOrientation,也可以监听UIApplicationDidChangeStatusBarFrameNotification,得到UIInterfaceOrientation。当然UIApplicationDidChangeStatusBarFrameNotification也可以监听热点等事件中出现的问题,如问题2.

(5)引入sdk报错(非pod)

_res_9_ninit”, referenced from: _setup_dns_server in QNResolver.o

  1. 项目中需导入 libresolv.dylib或libresolv.9.dylib。(Build Phases — Link Binary With Libraries);
  2. 或 (Build Settings — Linking — Other Linker Flags) 添加 -lresolv 选项

(6)在使用tableView时,使用footerView在最后一行默认不显示最后一条横线。简单粗暴的方法,在cell中重写layoutSubviews方法。

- (void)layoutSubviews {
    [super layoutSubviews];

      for (UIView *subview in self.contentView.superview.subviews) {
            if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"]) {
                  subview.hidden = NO;
            }
        }
}

显示tableView最后一行横线

(7)项目中有视频缓存,使用腾讯云下载完成下载功能,于是问题就来了,相信在做视频缓存、下载功能的同行可能也会遇到这种问题。

视频下载完成后,在 我的缓存中 查看,没有问题,但当软件更新或重新安装,视频列表还在,但视频打不开了。

查看腾讯云视频成功回调,发现在返回的字典中,缓存文件的路径是完整的。如图。

查看文件路径

查看文件路径并重新安装后会发现文件内容没变,但文件名变了。

重新安装前

重新安装后

这说明,app在重新安装或升级后,会重新建一个文件,并将原来的文件全部copy过去,然后将原文件删除。

知道原因之后就要解决了,当然解决的方法简单粗暴,既然是文件名,那就进行拼接和裁剪了。在plist文件中存储的是裁剪后的后半段路径,而读取文件路径后再拼接上Caches地址。

获取Caches目录路径的方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];

(8)iOS给好评时一般会直接打开app store应用详情界面,其实也可以直接跳转到评论页面,更有利于引导用户打分和评论。(将下面id改成自己的app id即可)

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=id1128294199&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"]];

直接跳转到详情

https://itunes.apple.com/cn/app/jia-zhang-mu-ke/id1128294199?mt=8

(9)

更多问题后续补充,欢迎探讨指正