IOS開發基礎知識--片段11,ios基礎知識--片段
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->喜好設定-進階-在菜單中顯示“開發”);