2014年2

1.NSJSONSerialization解析錯(cuò)誤的問題。
可以通過返回的error查找問題,可能是有一些轉(zhuǎn)義字符是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.獲取輸入法鍵盤高度調(diào)整控件位置的方法。

  • (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
    {
    // 獲取鍵盤動(dòng)態(tài)高度
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

    NSInteger nDisHight = 89;
    //判斷是高于ios7版本
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    {
    nDisHight -= 64;
    }
    //調(diào)整位置
    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;
    }
    //調(diào)整位置
    CGRect rectFrame = self.replyView.frame;
    rectFrame.origin.y = [UIScreen mainScreen].applicationFrame.size.height-nDisHight;
    [self.replyView setFrame:rectFrame];
    }

4.在view controller中,重寫方法來處理點(diǎn)擊事件。
/************************************************
參數(shù): 自動(dòng)檢測(cè) 觸碰對(duì)象 和 觸碰事件
功能: 根據(jù)label的位置大小選擇觸發(fā)事件
************************************************/

  • (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:@"無網(wǎng)頁地址數(shù)據(jù),無法跳轉(zhuǎn)"];
    }
    else{
    NSURL *cleanURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", textURL]];
    [[UIApplication sharedApplication] openURL:cleanURL];
    }
    }
    }

5.ios7對(duì)uiscrollview的高度做了自動(dòng)適配狀態(tài)欄,多出了20的高度.可以在view controller中設(shè)置self.automaticallyAdjustsScrollViewInsets = NO;

6.獲取配置文件中的版本號(hào)的方法。
NSString *strVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey];

NSString *strVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

7.在storyboard中啟用了”Use Auto Layout”后,在代碼中無法改變storyboard中tableViewCell中控件的位置。如果取消了auto layout還是不行,就卸載重裝試試。還發(fā)現(xiàn)如果要讓textview可以調(diào)整大小位置,還要設(shè)置scrolling enable為NO。

8.獲得應(yīng)用的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 運(yùn)行出現(xiàn)類似-[__NSCFString objectForKey:]: unrecognized selector sent to instance的調(diào)試方法
1.在程序中任意的.m文件(最好在特定的文件中,如為解決此類問題單獨(dú)建一個(gè)統(tǒng)一的.m文件)中添加類似以下代碼
@implementation NSString (NSStringDebug)
-(void) objectForKey:(NSString*) str {
assert(NO); // 這里的assert(NO)是必須的,不允許該函數(shù)正常運(yùn)行
}
@end
2.然后將斷點(diǎn)打在assert(NO)之前即可
3.調(diào)試完記得刪除這些代碼段點(diǎn)
一個(gè)錯(cuò)是把objectForKey這個(gè)NSDictionary的方法,發(fā)給了NSString對(duì)像,NSString沒有這個(gè)方法所以出錯(cuò)。調(diào)試的原理就是用類別的方式給NSString加上這一個(gè)這樣的方法,調(diào)用到時(shí)就有斷點(diǎn)。

11.iOS中Objective-C與JavaScript之間相互調(diào)用的實(shí)現(xiàn)(實(shí)現(xiàn)了與Android相同的機(jī)制)
http://www.tuicool.com/articles/QryuE3a
//調(diào)用oc方法,忽略警告

pragma clang diagnostic ignored "-Warc-performSelector-leaks"

SEL selector = NSSelectorFromString([function stringByAppendingString:@":"]);

12.UITextView在iphone4s真機(jī)上,文字顯示不完全的問題,在模擬器上沒問題。將Editable屬性設(shè)置成YES就可以了。

13.ios8下xml解析不能內(nèi)嵌調(diào)用即不能在它的回調(diào)函數(shù)中調(diào)[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]; //修復(fù)搜索條下方,搜索范圍不顯示的問題。
15.ios7導(dǎo)航欄遮蓋內(nèi)容的總問題:
在iOS 7中,蘋果引入了一個(gè)新的屬性,叫做[UIViewController setEdgesForExtendedLayout:],它的默認(rèn)值為UIRectEdgeAll。當(dāng)你的容器是navigation controller時(shí),默認(rèn)的布局將從navigation bar的頂部開始。這就是為什么所有的UI元素都往上漂移了44pt。
修復(fù)這個(gè)問題的快速方法就是在方法- (void)viewDidLoad中添加如下一行代碼:
1

self.edgesForExtendedLayout = UIRectEdgeNone;

這樣問題就修復(fù)了。

16.GCD****的另一個(gè)用處是可以讓程序在后臺(tái)較長(zhǎng)久的運(yùn)行。
在沒有使用GCD時(shí),當(dāng)app被按home鍵退出后,app僅有最多5秒鐘的時(shí)候做一些保存或清理資源的工作。但是在使用GCD后,app最多有10分鐘的時(shí)間在后臺(tái)長(zhǎng)久運(yùn)行。這個(gè)時(shí)間可以用來做清理本地緩存,發(fā)送統(tǒng)計(jì)數(shù)據(jù)等工作。
讓程序在后臺(tái)長(zhǎng)久運(yùn)行的示例代碼如下:
// AppDelegate.h文件
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;

// AppDelegate.m文件

  • (void)applicationDidEnterBackground:(UIApplication *)application
    {
    [self beingBackgroundUpdateTask];
    // 在這里加上你需要長(zhǎng)久運(yùn)行的代碼
    [self endBackgroundUpdateTask];
    }

  • (void)beingBackgroundUpdateTask
    {
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    [self endBackgroundUpdateTask];
    }];
    }

  • (void)endBackgroundUpdateTask
    {
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundUpdateTask = UIBackgroundTaskInvalid;
    }

17.ios7****以上對(duì)tableview的適配問題。

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    self.edgesForExtendedLayout = NO;
}

18.計(jì)算 UIWebView 顯示內(nèi)容后實(shí)際高度
兩種方法,方法1可以得到內(nèi)容的實(shí)際高度,方法2得到了將內(nèi)容顯示完整后的 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設(shè)置為NO。
2.Info中添加App Transport Security Settings(dictionanry)在其中添加Allow Arbitrary Loads 值為YES。

20.解決在iphone6上運(yùn)行時(shí),上下有黑邊的問題。
設(shè)置相應(yīng)的啟動(dòng)圖片,來解決。

21.一個(gè)神奇的問題。
在xcode7下編譯后,詳情界面卡在DetailTopicTableViewCell *topicCell = [tableView dequeueReusableCellWithIdentifier:@"TopicCell" forIndexPath:indexPath];
最后排查,發(fā)現(xiàn)是storyboard中cell里的UITextview設(shè)置了默認(rèn)的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); //關(guān)鍵在stderr

23.經(jīng)緯度定位,回調(diào)函數(shù)沒有被調(diào)用的問題。didUpdateLocations
是Xcode6 和 iOS8 的原因
1.在Info.plist中加入兩個(gè)缺省沒有的字段,設(shè)置值為YES。
NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription
2.需要在使用CoreLocation前調(diào)用方法
[self.locationManager requestWhenInUseAuthorization];或者[self.locationManager requestAlwaysAuthorization];用來詢問用戶是否同意定位。

24.https ssl 訪問網(wǎng)絡(luò)的問題。
在啟用NSURLProtocol 攔截網(wǎng)絡(luò)數(shù)據(jù)的時(shí)候,用NSURLConnection訪問https的有問題會(huì)報(bào)9813的錯(cuò)誤,換用NSURLSession就不會(huì),可見NSURLSession是不會(huì)被攔截的。

25.用代碼關(guān)閉自動(dòng)調(diào)整大小。
view.autoresizingMask = UIViewAutoresizingNone;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容