(iOS)項(xiàng)目總結(jié)-項(xiàng)目中遇到的各種的問(wèn)題和解決方法

? 前言:? ? ?

?一到公司報(bào)道那時(shí),便著手獨(dú)立的去完成了一個(gè)項(xiàng)目,其中的辛酸淚也是不足為外人道也。這次算是一個(gè)新型的app,仍然是獨(dú)立開(kāi)發(fā),但心境和想法卻是完全的不同。下面說(shuō)一次以前做開(kāi)發(fā)時(shí)常常忽略的知識(shí),也算是一種復(fù)習(xí)了吧。下面言歸正傳:


1、自帶鍵盤(pán)的next用法

這算是比較常用的了吧,只要我們想到登錄和注冊(cè)界面,就應(yīng)該自然而然的想到 - 鍵盤(pán)的next用法(當(dāng)然不排除相當(dāng)多的軟件并沒(méi)有實(shí)現(xiàn)這個(gè)功能)。

想法也就是:用戶在輸入用戶名后,點(diǎn)擊next跳轉(zhuǎn)到下一個(gè)輸入框。其實(shí)也不用想的如此復(fù)雜,在登錄界面,我們完全可以用一個(gè) if 語(yǔ)句做判斷,去實(shí)現(xiàn)鍵盤(pán)的響應(yīng)和注銷(xiāo)響應(yīng)。

代碼如下:?

?? - (BOOL)textFieldShouldReturn:(UITextField *)textField? ? {?

?? ? UITextField *passTextFiled = (UITextField *)[self.view viewWithTag:201];? ?

?? if (textField.tag == 200) {? ??

? ? ? [passTextFiled becomeFirstResponder];? ?

?? ? }else{? ? ?

? ? ? [passTextFiled resignFirstResponder];??

? ? }? ?

?? return YES;?

?? }

這樣就簡(jiǎn)單的在變成響應(yīng)和注銷(xiāo)響應(yīng)之間實(shí)現(xiàn)了輸入框的切換。

但還是存在一個(gè)問(wèn)題,如果我實(shí)現(xiàn)注冊(cè)界面的跳轉(zhuǎn)輸入框,并且在輸入框很多的情況下,顯然如果我仍是這樣判斷,會(huì)顯得啰嗦和冗余。

我曾想過(guò)使用計(jì)數(shù)的方式,記錄每一次的點(diǎn)擊next后的tag,但實(shí)踐中失敗了,因?yàn)楫?dāng)我不通過(guò)next而是直接用手去觸摸選擇輸入框時(shí),則無(wú)法計(jì)數(shù)。

?這里先留個(gè)空白,等把手里的項(xiàng)目完成時(shí)再研究這個(gè)問(wèn)題


2、UITextField的糾錯(cuò)和首字母大小寫(xiě)問(wèn)題

這個(gè)問(wèn)題只是簡(jiǎn)單的屬性問(wèn)題,在開(kāi)發(fā)時(shí)如果忘記,建議多點(diǎn)進(jìn)去看看,(如:command + UITextField)? ?

?//輸入框中是否有個(gè)叉號(hào),在什么時(shí)候顯示,用于一次性刪除輸入框中的內(nèi)容? ? self.clearButtonMode = UITextFieldViewModeWhileEditing;? ??

//是否糾錯(cuò),本人感覺(jué)糾錯(cuò)是一個(gè)很反感的東西,但UITextField默認(rèn)是 YES,這里我改成了NO? ? ? self.autocorrectionType = UITextAutocorrectionTypeNo;? ? ? ?

?//再次編輯就清空? ? ? self.clearsOnBeginEditing = YES;? ? ? ?

?//設(shè)置自動(dòng)縮小顯示的最小字體大小? ? ? self.minimumFontSize = 15;? ? ? ??

//設(shè)置鍵盤(pán)的樣式,本人感覺(jué)這個(gè)設(shè)置對(duì)用戶體驗(yàn)有影響。比如說(shuō)我想打電話,Type為UIKeyboardTypeNumberPad,而不是其他 ? ??

?self.keyboardType = UIKeyboardTypeNamePhonePad;


3、iOS中圖片的幾種拉伸方法的使用

以下是apple給出的3種方法: ? ??

?? UIImage *image = [UIImage imageNamed:@"picture"];? ? ? ??

?//iOS5之前? ? ?

?// 左端蓋寬度? ? ? ??

NSInteger leftCapWidth = image.size.width * 0.5f;? ? ? ?

?NSInteger topCapHeight = image.size.height * 0.8f;? ? ? ??

image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];? ? ? ??

//iOS5之后? ? ? ?

?UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height * 0.8f,image.size.width * 0.5f, 40, 40);? ? ? ?

?image = [image resizableImageWithCapInsets:insets];? ? ??

?//iOS6之后? ? ? ? UIEdgeInsets insets = UIEdgeInsetsMake(image.size.height * 0.8f,image.size.width * 0.5f, 40, 40);? ? ? ?

?image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];? ??

下面是使用的解釋: ??

/**? ?

?*? - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;? ??

?*??

? *? leftCapWidth代表左端蓋寬度,topCapHeight代表頂端蓋高度。系統(tǒng)會(huì)自動(dòng)計(jì)算出右端蓋寬度(rightCapWidth)和底端蓋高度(bottomCapHeight)? ??

?* 算法如下:? ??

?* width為圖片寬度? rightCapWidth = width - leftCapWidth - 1;? ? ? ?

?* height為圖片高度 bottomCapHeight = height - topCapHeight - 1? ??

?*? ??

?*經(jīng)過(guò)計(jì)算,你會(huì)發(fā)現(xiàn)中間的可拉伸區(qū)域只有1x1? ??

?* ??

* stretchWidth為中間可拉伸區(qū)域的寬度? ? ? ? stretchWidth = width - leftCapWidth - rightCapWidth = 1;? ? ? ?

?* stretchHeight為中間可拉伸區(qū)域的高度? ? ? ? stretchHeight = height - topCapHeight - bottomCapHeight = 1;? ??

?*? ??

?*因此,使用這個(gè)方法只會(huì)拉伸圖片中間1x1的區(qū)域,并不會(huì)影響到邊緣和角落。? ??

?*/

/**? ?

?* - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets? ??

*? 這個(gè)方法只接收一個(gè)UIEdgeInsets類(lèi)型的參數(shù),可以通過(guò)設(shè)置UIEdgeInsets的left、right、top、bottom來(lái)分別指定左端蓋寬度、右端蓋寬度、頂端蓋高度、底端蓋高度? ??

?*?*/ ? ? ??

?/**? ??

?*? - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode? ??

對(duì)比iOS5.0中的方法,只多了一個(gè)UIImageResizingMode參數(shù),用來(lái)指定拉伸的模式: ? ? ? UIImageResizingModeStretch:拉伸模式,通過(guò)拉伸UIEdgeInsets指定的矩形區(qū)域來(lái)填充圖片? ? UIImageResizingModeTile:平鋪模式,通過(guò)重復(fù)顯示UIEdgeInsets指定的矩形區(qū)域來(lái)填充圖片? ??

?**/

圖片拉伸的以上內(nèi)容參考文章 《iOS圖片拉伸技巧》講的很好,建議大家多看看。


4、iOS中喚起自帶瀏覽器(safari)的方法

也是一個(gè)簡(jiǎn)單的一句代碼 ,這種也就是知道了就知道了,不知道就是抓耳撓腮吧?

?? NSURL *url = [NSURL URLWithString:urlStr];? ? [[UIApplication sharedApplication] openURL:url];


5、iOS中喚起電話界面

app內(nèi)部喚起電話界面的實(shí)現(xiàn),是找了很多資料才有的一個(gè)結(jié)論(安全,并且撥打完之后可以返回app)

實(shí)現(xiàn)的方法是使用UIWebView 加載電話,目前這種方法是合法的,App Store也允許通過(guò)的。??

?NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",newPhoneString]]; ? ??

?if (_phoneCallWebView) {?

?? ? ? ? ? ? ? [_phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];??

?}

但如果使用下面的這種方法,則可能不允許通過(guò)審核? ? [

[UIApplication sharedApplication] openURL:phoneURL];


6、iOS 調(diào)用地圖的方法

app內(nèi)部調(diào)用第三方app,這里有一個(gè)詳細(xì)的文章《 IOS實(shí)現(xiàn)應(yīng)用內(nèi)打開(kāi)第三方地圖app進(jìn)行導(dǎo)航》

- app內(nèi)部調(diào)用地圖,需要先檢測(cè)用戶手機(jī)上是否已經(jīng)安裝地圖app。

我們常用的地圖app有:高德、百度;國(guó)外有:谷歌(Google Map )。當(dāng)然還有蘋(píng)果自帶的地圖,隨著iOS10的發(fā)布,蘋(píng)果地圖這塊也有很大的完善和進(jìn)步。

我使用Xcode模擬器實(shí)現(xiàn)時(shí),會(huì)提示:

?1、-canOpenURL: failed for URL: "iosamap://" - error: "(null)" 。原因是:模擬器上沒(méi)有高德地圖。?

2、-canOpenURL: failed for URL: "comgooglemaps://" - error: "This app is not allowed to query for scheme comgooglemaps"。 原因是:LSApplicationQueriesSchemes 我自己馬虎設(shè)成了字典類(lèi)型。

首先說(shuō)好的是iOS9以后,我們?cè)赼pp內(nèi)部要跳轉(zhuǎn)到其他軟件時(shí),需要在 Info.plist 中添加白名單。

方法為:在Info.plist中添加 key :LSApplicationQueriesSchemes? 類(lèi)型為:Array。

將高德、百度、谷歌的值填進(jìn)去分別是:**iosamap**|**baidumap**|**comgooglemaps**

圖片:


?至于下面代碼中的URLScheme 和appName??

是在Info.plist 的URL types中添加? URL Scheme( URL Schemes 是一個(gè)數(shù)組,允許應(yīng)用定義多個(gè) URL schemes。 )? 和? URL identifier(建議采用反轉(zhuǎn)域名的方法保證該名字的唯一性,比如 com.yourApp.www)

圖片如下:


?下面是具體的代碼:

?__block NSString *urlScheme = urlScheme;? ?

?__block NSString *appName = appName;? ??

__block CLLocationCoordinate2D coordinate = self.coordinate;? ? ? ??

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選擇地圖" message:nil preferredStyle:UIAlertControllerStyleActionSheet];? ? ? ? ??

//apple自帶的地圖不需要判斷? ? ? ??

UIAlertAction *action = [UIAlertAction actionWithTitle:@"蘋(píng)果地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ??

? ? ? ? ? ?MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];?

?? ? ? ? ? MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];? ? ? ? ? ? ? ? ? ? ??

?[MKMapItem openMapsWithItems:@[currentLocation, toLocation]? ? ? ? ? ? ? ? ? ? ? ? ? launchOptions:@{

MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];? ? ? ??

}];? ? ? ??

?[alert addAction:action];? ? ? ??

?//判斷百度地圖? ?

?if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]])? ? ??

{? ? ? ? UIAlertAction *action = [UIAlertAction actionWithTitle:@"百度地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ? ? ? ? ? ? ? ? ? ?

?NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];? ? ? ? ?

?}];? ? ? ??

[alert addAction:action];? ? ??

}? ? ?

?//判斷高德地圖? ? ?

?if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]])? ? ??

?{? ? ? ??

UIAlertAction *action = [UIAlertAction actionWithTitle:@"高德地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ? ? ? ? ? ? ? ? ? ??

NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];??

? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];? ? ? ? ? ? ? ? ? ? }];? ? ? ?

?[alert addAction:action];? ??

}? ? ? ??

?//判斷谷歌地圖? ?

?if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]])? ? {? ? ? ?

?UIAlertAction *action = [UIAlertAction actionWithTitle:@"谷歌地圖" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {? ? ? ? ? ? ? ? ? ? ? ?

?NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",appName,urlScheme,coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];? ? ? ? ? ? ? ? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];? ? ? ? ??

}];? ? ? ? ? ? ? ??

[alert addAction:action];? ??

}? ? ? ??

UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];? ? [alert addAction:action];? ? ? ??

[self presentViewController:alert animated:YES completion:^{? ? ? ? ??

}];? ??

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容