iOS判断手机号格式 |
ios,手机号格式,手机号 |
|
-(BOOL)isCorrectWhenCheckPhoneNumberForMobilePhone:(NSString*)phoneNumber
{
NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,1-9]))\\d{8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:phoneNumber];
if (!isMatch) {
return NO;
}
return YES;
}
|
iOS本地TXT文件内容 |
ios,txt文本内容 |
|
//文字内容
NSString*filePath=[[NSBundle mainBundle] pathForResource:@"文件名"ofType:@"txt"];
NSString*str=[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
|
iOS初始化UITabBarController |
ios,uitabbarcontroller |
|
//初始化标签栏
-(void)initUITabBarController
{
//首页
HomeViewController *homeVC = [[HomeViewController alloc] init];
homeVC.hidesBottomBarWhenPushed = false;
homeVC.tabBarItem.title=@"首页";
homeVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"homeSelected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
homeVC.tabBarItem.image = [[UIImage imageNamed:@"home.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UINavigationController *homeNav=[[UINavigationController alloc]initWithRootViewController:homeVC];
//订单
OrderViewController *orderVC = [[OrderViewController alloc] init];
orderVC.hidesBottomBarWhenPushed = false;
orderVC.tabBarItem.title=@"订单";
orderVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"orderSelected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
orderVC.tabBarItem.image = [[UIImage imageNamed:@"order.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UINavigationController *orderNav=[[UINavigationController alloc]initWithRootViewController:orderVC];
//个人中心
UserCenterViewController *userCenterVC = [[UserCenterViewController alloc] init];
userCenterVC.hidesBottomBarWhenPushed = false;
userCenterVC.tabBarItem.title=@"我的";
userCenterVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"userCenterSelected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
userCenterVC.tabBarItem.image = [[UIImage imageNamed:@"userCenter.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UINavigationController *userCenterNav=[[UINavigationController alloc]initWithRootViewController:userCenterVC];
//标签栏
NSArray *itemArray = @[homeNav, orderNav, userCenterNav];
UITabBarController *SOLTB=[[UITabBarController alloc]init];
SOLTB.viewControllers=itemArray;
//标签栏背景颜色
SOLTB.view.backgroundColor=[UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1];
//标签栏字体的颜色
SOLTB.view.tintColor=[ColorManager getTextColorLikeOrange];
self.window.rootViewController =SOLTB;
}
|
UIButton、UILabel 添加下划线 |
ios,添加下划线 |
|
//按钮添加下划线
-(void)addAttributeForButton:(UIButton*)btn
{
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:btn.titleLabel.text];
NSRange strRange = {0,[str length]};
[str addAttribute:NSForegroundColorAttributeName value:btn.titleLabel.textColor range:strRange]; //设置颜色
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[btn setAttributedTitle:str forState:UIControlStateNormal];
}
|
处理键盘遮挡输入框第三方:IQKeyboardManager |
ios第三方,键盘遮挡,ios键盘遮挡 |
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//自动处理键盘事件
IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
manager.enable = YES;
manager.shouldResignOnTouchOutside = YES;
manager.shouldToolbarUsesTextFieldTintColor = YES;
manager.enableAutoToolbar = NO;
}
|
iOS发送短信功能 |
ios系统功能,ios发送短信,ios短信 |
|
//程序外调用系统发短信,这个方法其实很简单,直接调用openURL即可:
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"sms://13888888888"]];
//程序内调用系统发短信
//(1)导入MessageUI.framework,并引入头文件:
#import<MessageUI/MessageUI.h>
//(2)实现代理方法MFMessageComposeViewControllerDelegate
-(void)messageComposeViewController:(MFMessageComposeViewController*)controllerdidFinishWithResult:(MessageComposeResult)result
{
[selfdismissViewControllerAnimated:YEScompletion:nil];
switch(result){
caseMessageComposeResultSent:
//信息传送成功
break;
caseMessageComposeResultFailed:
//信息传送失败
break;
caseMessageComposeResultCancelled:
//信息被用户取消传送
break;
default:
break;
}
}
//(3)发送短信
-(void)showMessageView:(NSArray*)phonestitle:(NSString*)titlebody:(NSString*)body
{
if([MFMessageComposeViewControllercanSendText])
{
MFMessageComposeViewController*controller=[[MFMessageComposeViewControlleralloc]init];
controller.recipients=phones;
controller.navigationBar.tintColor=[UIColorredColor];
controller.body=body;
controller.messageComposeDelegate=self;
[selfpresentViewController:controlleranimated:YEScompletion:nil];
[[[[controllerviewControllers]lastObject]navigationItem]setTitle:title];//修改短信界面标题
}
else
{
UIAlertView*alert=[[UIAlertViewalloc]initWithTitle:@"提示信息" message:@"该设备不支持短信功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];
[alertshow];
}
}
//参数phones:发短信的手机号码的数组,数组中是一个即单发,多个即群发。
//(4)调用发短信的方法
[self showMessageView:[NSArrayarrayWithObjects:@"13888888888",@"13999999999",nil] title:@"test" body:@"你是土豪么,么么哒"];
|
iOS拨打电话功能 |
ios系统功能,ios拨打电话,ios电话 |
|
//1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"18825046805"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
//2,这种方法,打完电话后还会回到原来的程序,也会弹出提示,推荐这种
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"18825046805"];
UIWebView * callWebview = [[UIWebView alloc] init];
[callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
[self.view addSubview:callWebview];
//3,这种方法也会回去到原来的程序里(注意这里的telprompt),也会弹出提示
NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"18825046805"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
|
iOS 单例类的代码实现 |
ios单例,设计模式,单例 |
|
+(id)sharedInstane
{
static id *s=nil;
if (!s)
{
s=[[id alloc]init];
}
return s;
}
|
iOS 监听网络的当前状态及变化 |
ios 网络监听,ios 网络 |
|
//监听手机的网络状态变化
-(void)observerNetworkStatus
{
static AFHTTPSessionManager *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
[_sharedClient.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status)
{
case AFNetworkReachabilityStatusReachableViaWWAN:
{
NSLog(@"-------AFNetworkReachabilityStatusReachableViaWWAN------");
//移动通讯
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
{
NSLog(@"-------AFNetworkReachabilityStatusReachableViaWiFi------");
//WiFi
}
break;
case AFNetworkReachabilityStatusNotReachable:
{
NSLog(@"-------AFNetworkReachabilityStatusNotReachable------");
//无网络
}
break;
default:
break;
}
}];
[_sharedClient.reachabilityManager startMonitoring];
});
}
|
iOS cookie值的获取、清除设置 |
ios,ios cookie |
|
1、cookie 值的获取
//遍历
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [cookieJar cookies])
{
//cookie中的值
NSString *cookieName = cookie.name;
}
2、cookie 值的清除
//清楚cookie值
-(void)clearCookie
{
//清除cookie值
NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *_tmpArray = [NSArray arrayWithArray:[cookieJar cookies]];
for (id obj in _tmpArray) {
[cookieJar deleteCookie:obj];
}
3、cookie 值的设置
//添加版本号cookie
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
//创建cookie
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"ios_user_ver" forKey:NSHTTPCookieName];
[cookieProperties setObject:version forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"112.74.130.254" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"112.74.130.254" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
[cookieProperties setValue:[NSDate dateWithTimeIntervalSinceNow:60*60*24*30*12] forKey:NSHTTPCookieExpires];
//设置cookie
NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[cookieJar setCookie:cookie];
|