2014年2

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;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,002評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,400評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,136評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,714評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,452評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,818評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,812評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,997評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,552評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,292評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,510評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,721評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,121評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,429評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,235評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,480評論 2 379

推薦閱讀更多精彩內容