iOS

iOS开发|奇巧淫技

持续更新中...

Posted by Jack on November 15, 2014
打印某控件所有子控件(私有方法,请勿提交AppStore)
NSLog(@"%@", [item performSelector:@selector(recursiveDescription)]);
像Safari一样动态隐藏navigationBar和toolbar
navigationController.hidesBarsOnSwipe = YES;	// 支持iOS8及以上版本

/// When the user swipes, the navigation controller’s navigationBar & toolbar will be hidden (on a swipe up) or shown (on a swipe down). The toolbar only participates if it has items.

设置UITableView不显示没有内容的Cell
self.tableView.tableFooterView = [[UIView alloc] init];
调整默认UITableViewCell的separaLine位置
self.tableView.separatorInset = UIEdgeInsetsMake(0, 16, 0, 0);
修改UITextField.placeholder字体颜色
[_textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];   
NSString切分为NSArray,NSArray合并为NSString
// 将NSString切分为NSArray
NSString *string = @"oop:ack:bork:greeble:ponies";
NSArray *array = [string componentsSeparatedByString:@":"];

// 将NSArray中元素合并为NSString
NSString *string = [array componentsJoinedByString:@":"];
读取本地JSON数据进行接口调试

当接口数据格式已经商定完毕,但服务端接口还待完成的情况下,这个小技巧会非常方便客户端进行接口调试和相关界面搭建。关键在于要创建能通过JSON格式校验的数据文件,此处我们假设本地JSON格式数据文件名为:JSON.md,具体读取代码如下:

NSString *fileName = [[NSBundle mainBundle] pathForResource:@"JSON" ofType:@"md"];
NSData *JSONData = [NSData dataWithContentsOfFile:fileName];
NSError *error;
id JSONObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
跳转系统设置页面
  • 跳转至系统设置自己APP选项

    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if([[UIApplication sharedApplication] canOpenURL:url]) {
    	[[UIApplication sharedApplication] openURL:url];
    }
    
  • 跳转至系统设置通用选项(General)

    NSURL *url = [NSURL URLWithString:@"prefs:root=General"];
    if([[UIApplication sharedApplication] canOpenURL:url]) {
    	[[UIApplication sharedApplication] openURL:url];
    }
    
  • 其他跳转字段举例:

    Settings - prefs:root=Settings

    Wi-Fi - prefs:root=WIFI

    Bluetooth - prefs:root=Bluetooth

    Notification - prefs:root=NOTIFICATIONS_ID

    Location Services - prefs:root=LOCATION_SERVICES

    Photo & Camera - prefs:root=Photo

    General - prefs:root=General

    Auto-Lock - prefs:root=General&path=AUTOLOCK

    Profile - prefs:root=General&path=ManagedConfigurationList