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阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容