iOS12适配及问题记录
Posted Jsen_Wang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS12适配及问题记录相关的知识,希望对你有一定的参考价值。
ios12适配及问题记录
版本信息
Xcode: Version 10.0 beta (10L176w)
macOS: 10.14 Beta (18A293u)
iOS: 12.0(16A5288q)
问题及解决过程
1,StatusBar内部结构改变
现象:crash
crash log:
-[_UIStatusBarIdentifier isEqualToString:]: unrecognized selector sent to instance 0x283452820
*** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[_UIStatusBarIdentifier isEqualToString:]: unrecognized selector sent to instance 0x283452820’
—————————————————————————————————————————————————————
问题代码和解决方法
+ (NSString *)getIphoneXNetWorkStates
UIApplication *app = [UIApplication sharedApplication];
id statusBar = [[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"statusBar"];
id one = [statusBar valueForKeyPath:@"regions"];
id two = [one valueForKeyPath:@"trailing"];
NSArray *three = [two valueForKeyPath:@"displayItems"];
NSString *state = @"无网络";
for (UIView *view in three)
//alert: iOS12.0 情况下identifier的变成了类"_UIStatusBarIdentifier"而不是NSString,所以会在调用“isEqualToString”方法时发生crash,
//修改前
// NSString *identifier = [view valueForKeyPath:@"identifier"];
//修改后
NSString *identifier = [[view valueForKeyPath:@"identifier"] description];
if ([identifier isEqualToString:@"_UIStatusBarWifiItem.signalStrengthDisplayIdentifier"])
id item = [view valueForKeyPath:@"_item"];
//alert: 这个问题和上边一样itemId是_UIStatusBarIdentifier 类型,不是string
NSString *itemId = [[item valueForKeyPath:@"identifier"] description];
if ([itemId isEqualToString:@"_UIStatusBarWifiItem"])
state = @"WIFI";
state = @"不确定";
else if ([identifier isEqualToString:@"_UIStatusBarCellularItem.typeDisplayIdentifier"])
UIView *statusBarStringView = [view valueForKeyPath:@"_view"];
// 4G/3G/E
state = [statusBarStringView valueForKeyPath:@"text"];
return state;
2,[UIImage imageNamed:]不能正常加载Assets中的图片
解决:
将图片放到bundle中
使用一下方式加载即可
NSString *path = [[NSBundle mainBundle] pathForResource:@"bg_login" ofType:@"png"];
_backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:path]];
这个不能正常加载的情况只出现在个别的地方,目前找到的共性是加载的图片偏大,其他并没有头绪,感觉像是测试版本的Bug,google也没有人解答此类问题,后续会继续关注。
iOS12、iOS11 iPhone X等刘海屏幕适配WKWebView 问题。
在项目中发现一个很恶心的问题,在WKWebView中输入内容,键盘弹起后收回,view的底部会向上偏移,并且可视区域减小,如图:
键盘弹起
键盘收回
可以看到底部会有很大的留白。
解决方法:
方法1:
H5中:
meta
标签中不要使用viewport-fit=cover"
如下:
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no>
在input组件失去焦点时将窗口滑动到(0,0)
如下:
$('input').on("blur",function (e)
window.scrollTo(0,0);
);
方法2:
meta
标签中不要使用viewport-fit=cover"
如下:
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no>
在native
的WKWebview
所在controller
监听键盘将要收起动作,调用WKWebview
中的滚动视图,修改其ContentOffset
属性. 如下:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardWillHide
// 解决iOS11在WKWebview中键盘收回时将可视区域上移问题
if (@available(iOS 11.0, *))
WKWebView *webview = (WKWebView*)self.webView;
for(UIView* v in webview.subviews)
if([v isKindOfClass:NSClassFromString(@"WKScrollView")])
UIScrollView *scrollView = (UIScrollView*)v;
[scrollView setContentOffset:CGPointMake(0, 0)];
iOS12.0.x serviceSubscriberCellularProviders Crash
获取Sim 卡信息crash
@property(readonly, retain, nullable) NSDictionary<NSString *, CTCarrier *> *serviceSubscriberCellularProviders API_AVAILABLE(ios(12.0), watchos(5.0)) API_UNAVAILABLE(macos, tvos);
定义说名在iOS12.0开始就可以用了,但是在12.0.x的系统中会在运行时找不到此方法而crash
解决方法
经调查发现,这是Apple的官方bug,在iOS12.1.x修复了此问题,所以代码如下解决
/**
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
sim卡信息
*/
+ (NSDictionary *)getSIMCardInfo
CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = nil;
NSString *radioType = nil;
if (@available(iOS 12.1, *))
if (info && [info respondsToSelector:@selector(serviceSubscriberCellularProviders)])
NSDictionary *dic = [info serviceSubscriberCellularProviders];
if (dic.allKeys.count)
carrier = [dic objectForKey:dic.allKeys[0]];
if (info && [info respondsToSelector:@selector(serviceCurrentRadioAccessTechnology)])
NSDictionary *radioDic = [info serviceCurrentRadioAccessTechnology];
if (radioDic.allKeys.count)
radioType = [radioDic objectForKey:radioDic.allKeys[0]];
else
carrier = [info subscriberCellularProvider];
radioType = [info currentRadioAccessTechnology];
//运营商可用
BOOL use = carrier.allowsVOIP;
//运营商名字
NSString *name = carrier.carrierName;
//ISO国家代码
NSString *code = carrier.isoCountryCode;
//移动国家代码
NSString *mcc = [carrier mobileCountryCode];
//移动网络代码
NSString *mnc = [carrier mobileNetworkCode];
return @
UCAnalysis_SIMCardInfoKey_AllowsVOIP:@(use),
UCAnalysis_SIMCardInfoKey_CarrierName:name?:@"NONE",
UCAnalysis_SIMCardInfoKey_ISOCountryCode:code?:@"NONE",
UCAnalysis_SIMCardInfoKey_MobileCountryCode:mcc?:@"NONE",
UCAnalysis_SIMCardInfoKey_MobileNetworkCode:mnc?:@"NONE",
UCAnalysis_SIMCardInfoKey_RadioAccessTechnology:radioType?:@"NONE"
;
以上是关于iOS12适配及问题记录的主要内容,如果未能解决你的问题,请参考以下文章