1.NSJSONSerialization解析錯誤的問題。
可以通過返回的error查找問題,可能是有一些轉義字符是NSJSONSerialization處理不了的,比如\t \r \n。可以把它們過濾掉。
strJsonPart = [strJsonPart stringByReplacingOccurrencesOfString:@"\t" withString:@""];
2.JSON在線視圖查看器(Online JSON Viewer):http://www.bejson.com/go.html?u=http://www.bejson.com/jsonview2/
- (void)xmppStream:(XMPPStream *)sender willSecureWithSettings:(NSMutableDictionary )settings
{
// NSLog(@"settins: %@", [settings description]);
// NSArray array = [sender supportedAuthenticationMechanisms];
// NSLog(@"array: %@", [array description]);
// BOOL bTest = [sender supportsAuthenticationMechanism:@"PLAIN"];
[settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString)kCFStreamSSLAllowsAnyRoot];
//[settings setObject:kCFStreamSocketSecurityLevelTLSv1 forKey:(NSString)kCFStreamSSLLevel];
//[settings setObject:kCFBooleanTrue forKey:kCFStreamSSLAllowsExpiredCertificates];
NSLog(@"willSecureWithSettings.");
}
3.獲取輸入法鍵盤高度調整控件位置的方法。
-
(void)viewDidLoad
{
//鍵盤通知事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//獲取輸入法高度
-
(void)handleKeyboardDidShow:(NSNotification*)notification
{
// 獲取鍵盤動態高度
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;NSInteger nDisHight = 89;
//判斷是高于ios7版本
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
nDisHight -= 64;
}
//調整位置
CGRect rectFrame = self.replyView.frame;
rectFrame.origin.y = [UIScreen mainScreen].applicationFrame.size.height-kbSize.height-nDisHight;
[self.replyView setFrame:rectFrame];
} (void)handleKeyboardWillHide:(NSNotification*)notification
{
NSInteger nDisHight = 89;
//判斷是高于ios7版本
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
nDisHight -= 64;
}
//調整位置
CGRect rectFrame = self.replyView.frame;
rectFrame.origin.y = [UIScreen mainScreen].applicationFrame.size.height-nDisHight;
[self.replyView setFrame:rectFrame];
}
4.在view controller中,重寫方法來處理點擊事件。
/************************************************
參數: 自動檢測 觸碰對象 和 觸碰事件
功能: 根據label的位置大小選擇觸發事件
************************************************/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([_titleLabel frame], [touch locationInView:self.view]))
{
NSString *textURL = [_myDetailDic objectForKey:@"PageUrl"];
textURL = [textURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if (!textURL) {
[self DAlertShow:@"無網頁地址數據,無法跳轉"];
}
else{
NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", textURL]];
[[UIApplication sharedApplication] openURL:cleanURL];
}
}
}
5.ios7對uiscrollview的高度做了自動適配狀態欄,多出了20的高度.可以在view controller中設置self.automaticallyAdjustsScrollViewInsets = NO;
6.獲取配置文件中的版本號的方法。
NSString *strVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];
或
NSString *strVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
7.在storyboard中啟用了”Use Auto Layout”后,在代碼中無法改變storyboard中tableViewCell中控件的位置。如果取消了auto layout還是不行,就卸載重裝試試。還發現如果要讓textview可以調整大小位置,還要設置scrolling enable為NO。
8.獲得應用的delegate。
AppDelegate delegate = (AppDelegate)[[UIApplication sharedApplication] delegate];
9.從storyboard中,獲取viewController的方法。
- (void)viewDidLoad
{
[super viewDidLoad];
//identifier是storyboard中controller的Storyboard ID
UIViewController *vc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"vc111"];
}
10.xcode 運行出現類似-[__NSCFString objectForKey:]: unrecognized selector sent to instance的調試方法
1.在程序中任意的.m文件(最好在特定的文件中,如為解決此類問題單獨建一個統一的.m文件)中添加類似以下代碼
@implementation NSString (NSStringDebug)
-(void) objectForKey:(NSString*) str {
assert(NO); // 這里的assert(NO)是必須的,不允許該函數正常運行
}
@end
2.然后將斷點打在assert(NO)之前即可
3.調試完記得刪除這些代碼段點
一個錯是把objectForKey這個NSDictionary的方法,發給了NSString對像,NSString沒有這個方法所以出錯。調試的原理就是用類別的方式給NSString加上這一個這樣的方法,調用到時就有斷點。
11.iOS中Objective-C與JavaScript之間相互調用的實現(實現了與Android相同的機制)
http://www.tuicool.com/articles/QryuE3a
//調用oc方法,忽略警告
pragma clang diagnostic ignored "-Warc-performSelector-leaks"
SEL selector = NSSelectorFromString([function stringByAppendingString:@":"]);
12.UITextView在iphone4s真機上,文字顯示不完全的問題,在模擬器上沒問題。將Editable屬性設置成YES就可以了。
13.ios8下xml解析不能內嵌調用即不能在它的回調函數中調[parser parse],解決方法是把[parser parse]替代為:
dispatch_queue_t reentrantAvoidanceQueue = dispatch_queue_create("reentrantAvoidanceQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(reentrantAvoidanceQueue, ^{
NSXMLParser* parser = [[NSXMLParser alloc] initWithData:xml];
[parser setDelegate:self];
if (![parser parse]) {
NSLog(@"There was an error=%@ parsing the xml. with data %@", [parser parserError], [[NSString alloc] initWithData:xml encoding: NSASCIIStringEncoding]);
}
[parser release];
});
dispatch_sync(reentrantAvoidanceQueue, ^{ });
14.[_mySearchBar sizeToFit]; //修復搜索條下方,搜索范圍不顯示的問題。
15.ios7導航欄遮蓋內容的總問題:
在iOS 7中,蘋果引入了一個新的屬性,叫做[UIViewController setEdgesForExtendedLayout:],它的默認值為UIRectEdgeAll。當你的容器是navigation controller時,默認的布局將從navigation bar的頂部開始。這就是為什么所有的UI元素都往上漂移了44pt。
修復這個問題的快速方法就是在方法- (void)viewDidLoad中添加如下一行代碼:
1
self.edgesForExtendedLayout = UIRectEdgeNone;
這樣問題就修復了。
16.GCD****的另一個用處是可以讓程序在后臺較長久的運行。
在沒有使用GCD時,當app被按home鍵退出后,app僅有最多5秒鐘的時候做一些保存或清理資源的工作。但是在使用GCD后,app最多有10分鐘的時間在后臺長久運行。這個時間可以用來做清理本地緩存,發送統計數據等工作。
讓程序在后臺長久運行的示例代碼如下:
// AppDelegate.h文件
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;
// AppDelegate.m文件
(void)applicationDidEnterBackground:(UIApplication *)application
{
[self beingBackgroundUpdateTask];
// 在這里加上你需要長久運行的代碼
[self endBackgroundUpdateTask];
}(void)beingBackgroundUpdateTask
{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}(void)endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
17.ios7****以上對tableview的適配問題。
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
self.edgesForExtendedLayout = NO;
}
18.計算 UIWebView 顯示內容后實際高度
兩種方法,方法1可以得到內容的實際高度,方法2得到了將內容顯示完整后的 webView 的尺寸(包含 UIEdgeInsets)
-
(void)webViewDidFinishLoad:(UIWebView *)wb
{
//方法1
CGFloat documentWidth = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById('content').offsetWidth"] floatValue];
CGFloat documentHeight = [[wb stringByEvaluatingJavaScriptFromString:@"document.getElementById("content").offsetHeight;"] floatValue];
NSLog(@"documentSize = {%f, %f}", documentWidth, documentHeight);//方法2
CGRect frame = wb.frame;
frame.size.width = 768;
frame.size.height = 1;
// wb.scrollView.scrollEnabled = NO;
wb.frame = frame;
frame.size.height = wb.scrollView.contentSize.height;
NSLog(@"frame = %@", [NSValue valueWithCGRect:frame]);
wb.frame = frame;
}
19.xcode7下編譯舊工程的問題。ios9
1.Build Settings中enable bitcode設置為NO。
2.Info中添加App Transport Security Settings(dictionanry)在其中添加Allow Arbitrary Loads 值為YES。
20.解決在iphone6上運行時,上下有黑邊的問題。
設置相應的啟動圖片,來解決。
21.一個神奇的問題。
在xcode7下編譯后,詳情界面卡在DetailTopicTableViewCell *topicCell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell" forIndexPath:indexPath];
最后排查,發現是storyboard中cell里的UITextview設置了默認的text值。
22.將NSLog輸出到文件的方法。
在appDelegate文件中添加如下代碼:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); //關鍵在stderr
23.經緯度定位,回調函數沒有被調用的問題。didUpdateLocations
是Xcode6 和 iOS8 的原因
1.在Info.plist中加入兩個缺省沒有的字段,設置值為YES。
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
2.需要在使用CoreLocation前調用方法
[self.locationManager requestWhenInUseAuthorization];或者[self.locationManager requestAlwaysAuthorization];用來詢問用戶是否同意定位。
24.https ssl 訪問網絡的問題。
在啟用NSURLProtocol 攔截網絡數據的時候,用NSURLConnection訪問https的有問題會報9813的錯誤,換用NSURLSession就不會,可見NSURLSession是不會被攔截的。
25.用代碼關閉自動調整大小。
view.autoresizingMask = UIViewAutoresizingNone;