IOS开发9个常用方法

1、存储写入文件


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:文件名];


2、读取资源文件地址


NSString *picPath = [[NSBundle mainBundle] pathForResource:@"文件名" ofType:@"类型"];


3、RGB值配色
       

NSString *str = @"#63729F";

 unsigned green,red,blue;

   
NSRange range;
   
range.length = 2;
  
range.location = 1;
   
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&red];
  
 range.location = 3;
   
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&green];
   
range.location = 5;
   
[[NSScanner scannerWithString:[str substringWithRange:range]] scanHexInt:&blue];
   
UIColor *color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1];


4、UINavigationBar属性设置



 UINavigationBar *nav = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
 
[nav setBackgroundImage:[[Resources sharedResources] getAppLogo]forBarMetrics:UIBarMetricsDefault];
 UIBarButtonItem *closeButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeWebView)];
 
UINavigationItem *navgationItem = [[UINavigationItem alloc] initWithTitle:nil];
 
[nav pushNavigationItem:navgationItem animated:NO];
 
[navgationItem setRightBarButtonItem:closeButtonItem];
 
[view addSubview:nav];


[closeButtonItem release];
 
[nav release];  
 
[navigationItem setTitle:@"TITLE"]; 


5、获取SIM卡运营商



- (NSString *)theTelephoneNetworkInfo
{
    NSString *thePhoneNetworkInfo = nil;
    
     CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    
     CTCarrier *carrier = [info subscriberCellularProvider];
    
     [info release];
    
     thePhoneNetworkInfo = carrier.carrierName;
    
     return thePhoneNetworkInfo;
    
}

6、判断设备版本


#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 50000  (50000 表示  5.0)

#endif


7、label根据字符串长度自动换行并增加label的高度


CGSize size ;

size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(300, 2000.0f) lineBreakMode:UILineBreakModeCharacterWrap];
label.numberOfLines = 0;

label.size = size;
label.lineBreakMode = UILineBreakModeCharacterWrap;

300----->label的宽度

//字符串去空格
NSString *temp = [str  stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//看剩下的字符串的长度是否为零
if ([temp length]!=0) {
        return;
    }

8、IMEI


IMEI由15位数字组成

1、前6位数(TAC,TYPE  APPROVAL CODE)是“型号核准号码”,一般代表机型。

2、接着的两位数字(FAC,Final Assembly Code)是“最后装配号”,一般代表产地。

3、之后的6位数(SNR)是“串号”,一班代表生产顺序号。

4、最后一位数(SP)通常是“0”,为校验码,目前暂备用。


9、判断网络连接

程序中首先需要添加Reachability.h 和 Reachability.m文件,导入SystemConfiguration.framework

- (void)isHaveInternet{
    
Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    
switch ([r currentReachabilityStatus]) 
{
        
case NotReachable:  // 没有网络连接
            
NSLog(@"没有网络");
            
            
break;
        
case ReachableViaWWAN:  // 使用3G网络
            
NSLog(@"正在使用3G网络");
            
break;
        
case ReachableViaWiFi: // 使用WiFi网络
            
NSLog(@"正在使用wifi网络");
            
break;
    
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值