iOS 修改手机号,添加号码到通讯录

类似微信点击某个手机号 弹出识别提示框

引入头文件

#import<AddressBookUI/AddressBookUI.h>
#import <AddressBook/ABAddressBook.h>

需要实现的委托

<ABNewPersonViewControllerDelegate,
ABPersonViewControllerDelegate,
ABPeoplePickerNavigationControllerDelegate>

点击某个号码弹出提示框 由于本文只做了通信录操作呼叫和复制并没有实现

- (void)selectorPhoneButton:(UIButton*)sender
{
    NSString * phone =[NSString stringWithFormat:@"%@:可能是一个电话号码",@"18976373826"];

    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:phone
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * callAction = [UIAlertAction actionWithTitle:@"呼叫"
                                                style:UIAlertActionStyleDefault
                                             handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:callAction];

    UIAlertAction * copyAction = [UIAlertAction actionWithTitle:@"复制"
                                                style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:copyAction];

    UIAlertAction * remarkAction = [UIAlertAction actionWithTitle:@"添加备注"
                                                 style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:remarkAction];
    
    UIAlertAction * addColleagueAction = [UIAlertAction actionWithTitle:@"添加到手机通讯录"
                                                        style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                            
       [self addColleague];
    }];
    [alert addAction:addColleagueAction];
    
    UIAlertAction * defaultAction = [UIAlertAction actionWithTitle:@"取消"
                                                   style:UIAlertActionStyleCancel
                                                    handler:^(UIAlertAction * action) {
                                                              
                                                    }];
    
    [alert addAction:defaultAction];
    
    
    [self presentViewController:alert animated:YES completion:nil];
}

点击添加到手机通讯录后 再次弹出提示框

- (void)addColleague
{
    NSString * phone = self.editPersonPhone;
    NSString * msg =[NSString stringWithFormat:@"%@:可能是一个电话号码",phone];
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:nil
                                                                   message:msg
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * callAction = [UIAlertAction actionWithTitle:@"创建新联系人"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                              [self createNewPerson:phone];
                                                        }];
    [alert addAction:callAction];
    
    UIAlertAction * copyAction = [UIAlertAction actionWithTitle:@"添加到现有联系人"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * _Nonnull action) {
                                                            [self updatePersonPhone:phone];
                                                        }];
    [alert addAction:copyAction];
    
    UIAlertAction * defaultAction = [UIAlertAction actionWithTitle:@"取消"
                                                             style:UIAlertActionStyleCancel
                                                           handler:^(UIAlertAction * action) {
                                                            
                                                           }];
    
    [alert addAction:defaultAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

 

- (ABRecordRef)addABRecordRef:(NSString*)phone
{
    ABRecordRef newPerson = ABPersonCreate();
    CFErrorRef error = NULL;
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFTypeRef)(phone), kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,&error);
    CFRelease(multiPhone);
    return newPerson;
}
/** 创建新联系人*/
- (void)createNewPerson:(NSString*)number
{
    //新添加联系人
    ABNewPersonViewController * newPerView = [[ABNewPersonViewController alloc] init];
    UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:newPerView];
    newPerView.newPersonViewDelegate = self;
    newPerView.displayedPerson = [self addABRecordRef:number];
    newPerView.title = @"新添加联系人";
    [self presentViewController:nav animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }];
    
}
//新创建联系人回调
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
    //用于编辑新联系人后做的事件,比如说模态退出,比如说不保存联系人到通讯录
    if (person!=NULL) {
        ABAddressBookRef adbk= ABAddressBookCreateWithOptions(NULL, NULL);
        //从数据库中删除新加的联系人
        ABAddressBookRemoveRecord(adbk, person, NULL);
        CFStringRef name=ABRecordCopyCompositeName(person);
        NSLog(@"name%@",name);//对新加的联系人做处理,不一定要保存到通讯录数据库
        CFRelease(name);
        CFRelease(adbk);
        
    }
    [self dismissViewControllerAnimated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        
    }];
}


/** 编辑先有联系人,添加新号码*/
- (void)updatePersonPhone:(NSString*)number{
    
    self.editPersonPhone = number;
    ABPeoplePickerNavigationController * picker =[[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    }];
    
}

#pragma mark ABPeoplePickerNavigationControllerDelegate

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person
{
    self.selectEditPerson = person;
    [self dismissViewControllerAnimated:YES completion:^{
        //选择联系人后编辑保存
        [self unknownPerson:self.selectEditPerson];
    }];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    return YES;
}
//编辑联系人
- (void)unknownPerson:(ABRecordRef)person
{
    //获取联系人所有电话
    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
    ABMutableMultiValueRef multi=ABMultiValueCreateMutableCopy(phoneNumbers);
    //添加新的字段
    ABMultiValueAddValueAndLabel(multi,(__bridge CFStringRef)self.editPersonPhone, kABPersonPhoneMobileLabel, nil);
    //修改联系人电话
    ABRecordSetValue(person, kABPersonPhoneProperty, multi, nil);
    
    //显示和编辑一个联系人信息
    ABPersonViewController * personVc=[[ABPersonViewController alloc]init];
    personVc.personViewDelegate = self;
    personVc.displayedPerson    = person;
    CFRelease(person);
    personVc.allowsEditing      = YES;
    personVc.title = @"编辑";
    [personVc setEditing:YES animated:YES];
    
    //把联系人信息作为导航栏的根视图,才可以显示编辑按钮和 完成按钮,返回按钮
    UINavigationController * personNav=[[UINavigationController alloc]initWithRootViewController:personVc];
    personNav.navigationBarHidden = NO;
    UINavigationBar * subNavBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    subNavBar.barStyle = UIBarStyleDefault;
    
    UINavigationItem *navItem  = [[UINavigationItem alloc] init];
    UIBarButtonItem *backItem  = [[UIBarButtonItem alloc]
                                  initWithTitle:@"取消"//kValue(@"common_title_cancel")
                                  style:UIBarButtonItemStylePlain
                                  target:self
                                  action:@selector(cancelEdit:)];
    UIBarButtonItem *okItem    = [[UIBarButtonItem alloc]
                                  initWithTitle:@"完成"//kValue(@"common_title_done")
                                  style:UIBarButtonItemStylePlain
                                  target:self
                                  action:@selector(okEdit:)];
    
    navItem.leftBarButtonItem  = backItem;
    navItem.rightBarButtonItem = okItem;
    
    subNavBar.items = [NSArray arrayWithObjects:navItem, nil];
    [personNav.view addSubview:subNavBar];
    
    self.editPersonNavigation = personNav;
    
    [self presentViewController:personNav animated:YES completion:^{
        SEL selAction = [personVc editButtonItem].action;
        [[personNav editButtonItem].target performSelector:selAction withObject:nil afterDelay:0];
    }];
}
//取消通讯录编辑
- (void)cancelEdit:(id)sender
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [self.editPersonNavigation.topViewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}
//保存修改后通讯录中联系人
- (void)okEdit:(id)sender
{
    //获得电话本
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    //保存电话本
    ABAddressBookAddRecord(addressBook,  (__bridge ABRecordRef)(self.editPersonPhone), nil);
    ABAddressBookSave(addressBook, NULL);
    [self cancelEdit:nil];
    
}

 

 

 

ReactNative 技术

一  前言

FaceBook早期开源发布了React Native For IOS,终于在2015年9月15日也发布了React Native for Android,虽然Android版本的项目发布比较迟,但是也没有阻挡了广大开发者的热情。可以这样讲在2015年移动平台市场上有两个方向技术研究比较火,第一种为阿里,百度,腾讯,携程,360等一线互联网公司的插件化,热修改等技术,第二种就是广大开发者一直在讨论的React Native技术。前几天同我在美国的童鞋了解到,在国外现在很多创业型互联网公司都在使用React Native技术,由此可以看出该项技术的前景还是不错的。我在这边我自己就大胆预测一下吧(呵呵,勿喷哦~):2016年将是React Native大力发展的一年。所以对于我自己来而言,2016年的主要技术方向,React Native是占了主角了。在接下来的很长一段时间内,我的博客会持续更新React Native的应用开发教程,欢迎大家关注以及相关交流!

二 React Native介绍

React Native 是伟大的互联网公司Facebook与2015年9月15日发布的,该可以让我们广大开发者使用JavaScript和React开发我们的应用,该提倡组件化开发,也就是说React Native给我们提供一个个封装好的组件让开发者来进行使用,甚至我们可以相关嵌套形成新的组件。使用React Native我们可以维护多种平台(Web,Android和IOS)的同一份业务逻辑核心代码来创建原生应用。现阶段Web APP的的体验还是无法达到Native APP的体验,所以这边fackbook更加强调的是learn once,write everywhere,应用前端我们使用js和React来开发不同平台的UI,下层核心模块编写复用的业务逻辑代码,提供应用开发效率。

 

改变navigationController 跳转动画

在iOS 项目中页面跳转是特别常见的事情。但要使用pushViewController跳转动画是由系统决定的。但有时需要修改系统跳转动画就比较麻烦了,需要重写navigationController 的 pushViewController 动画。不多说直接上代码吧。

在代码之前是需要引入QuartzCore库

#import <QuartzCore/QuartzCore.h>

在要跳转的方法里面写

pushViewController 
{
    CATransition *animation = [CATransition animation];
    
    [animation setDuration:0.5];
    
    [animation setType: kCATransitionMoveIn];
    
    [animation setSubtype: kCATransitionFromBottom];
    
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    

    YKProductSizeDetailViewController *sizeDetail=[[YKProductSizeDetailViewControlleralloc] initWithNibName:@"YKProductSizeSelectController"bundle:nil];

    //sizeDetail.delegate=self;

    sizeDetail.tag=fromTag;

    sizeDetail.product=product;

    sizeDetail.inpusPage=99;

    sizeDetail.needTipToSelectSize=(fromTag!=YK_TAG_SIZE_SELECT_FROM_SIZE_DETAIL ?YES:NO);

    [sizeDetail setSizeList:[productdetailSizeList]];

    [[selfnavigationController] pushViewController:sizeDetail animated:NO];

  [self.navigationController.view.layer addAnimation:animation forKey:nil];

}

 

返回

popViewControllerAnimated

{

    CATransition *animation = [CATransitionanimation];

    [animation setDuration:0.5];

    [animation setType: kCATransitionMoveIn];

    [animation setSubtype: kCATransitionFromTop];

    [animation setTimingFunction:[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionDefault]];

    [self.navigationController.view.layeraddAnimation:animation forKey:nil];

    [[selfnavigationController] popViewControllerAnimated:NO];

}

 

 

 

 

 

Objective-C 基础知识

Objective-C运行时定义了几种重要的类型。

Class:定义Objective-C类

Ivar:定义对象的实例变量,包括类型和名字。

Protocol:定义正式协议。

objc_property_t:定义属性。叫这个名字可能是为了防止和Objective-C 1.0中的用户类型冲突,那时候还没有属性。

Method:定义对象方法或类方法。这个类型提供了方法的名字(就是**选择器**)、参数数量和类型,以及返回值(这些信息合起来称为方法的**签名**),还有一个指向代码的函数指针(也就是方法的**实现**)。

SEL:定义选择器。选择器是方法名的唯一标识符。

IMP:定义方法实现。这只是一个指向某个函数的指针,该函数接受一个对象、一个选择器和一个可变长参数列表(varargs),返回一个对象