IOS开发基础知识--碎片11
1:AFNetwork判断网络状态
#import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //网络 [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; [[AFNetworkReachabilityManager sharedManager] startMonitoring]; //网络状态判断 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusNotReachable:{ [self showMBPHudTipStr:@"当前无网络连接"]; break; } case AFNetworkReachabilityStatusReachableViaWiFi:{ [LogUtil logUtilstring:@"WiFi网络"]; break; } case AFNetworkReachabilityStatusReachableViaWWAN:{ [self showMBPHudTipStr:@"无线网络"]; break; } default: break; } }]; return YES; }
2:UIButton倒计时
当在倒计时uibutton不能被响应事件; -(void)startTime{ __block int timeout=60; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue); dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行 dispatch_source_set_event_handler(_timer, ^{ if(timeout<=0){ //倒计时结束,关闭 dispatch_source_cancel(_timer); dispatch_async(dispatch_get_main_queue(), ^{ //设置界面的按钮显示 根据自己需求设置 [self.againBtn setTitle:@"重发验证码" forState:UIControlStateNormal]; self.againBtn.userInteractionEnabled = YES; self.labNoMessage.text=@"没有收到验证码吗?"; self.labNoMessage.textColor=[UIColor redColor]; }); }else{ int seconds = timeout % 60; NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds]; dispatch_async(dispatch_get_main_queue(), ^{ //设置界面的按钮显示 根据自己需求设置 [self.againBtn setTitle:[NSString stringWithFormat:@"重发激活邮件(%@)",strTime] forState:UIControlStateNormal]; self.againBtn.userInteractionEnabled = NO; self.labNoMessage.text=@"激活邮件发送成功"; self.labNoMessage.textColor=[UIColor colorWithHexString:@"84BF20"]; }); timeout--; } }); dispatch_resume(_timer); }
3:判断iphone设备
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0) #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT)) #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT)) #define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0) #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
4:在IOS8以下报一个自动布局的BUG,而在IOS8却能正常运行
会导致APP挂掉,BUG内容: Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Auto Layout still required after executing -layoutSubviews.
UITableView’s implementation of -layoutSubviews needs to call super.’ - (void)layoutSubviews { //自动布局的内容放在super layoutSubviews前面 [self _updateConstraints]; [super layoutSubviews]; }
5:与JS交互,并把JS代码兼容android跟IOS
JS代码: $(function () { var u = navigator.userAgent, app = navigator.appVersion; var isAndroid = u.indexOf(‘Android‘) > -1 || u.indexOf(‘Linux‘) > -1; //android终端或者uc浏览器 var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端 if (isAndroid) { $(‘#btn_Success‘).attr(‘href‘, ‘javascript:mailActive.backHome()‘); } else if (isiOS) { $(‘#btn_Success‘).attr(‘href‘, ‘protoclo://backHome‘); } }); 而IOS则是shouldStartLoadWithRequest: -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSString *newURL=[[request URL] absoluteString]; if([newURL hasPrefix:@"protoclo://"]) { NSArray *stringArray=[newURL componentsSeparatedByString:@"//"]; if (stringArray.count>1&&[[stringArray objectAtIndex:1] isEqualToString:@"backHome"]) { [self webJavascriptBackHome]; } } return YES; } 注:比较好IOS的JS交互第三方插件WebViewJavascriptBridge;在IOS开发调试内嵌webView时可以用Safari进行调试,它可以设置出开发模式菜单,开发->ios simulator 就可以实现查看网页的运行详情(Safari->偏好设置-高级-在菜单中显示“开发”);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。