`
fantom
  • 浏览: 140419 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

地址薄源码分析

阅读更多
ContactViewController.h 头文件
#import <UIKit/UIKit.h>
//基库,一系列的Class(类)来建立和管理iPhone OS应用程序的用户界面接口、应用程序对象、事件控制、绘图模型、窗口、视图和用于控制触摸屏等的接口
#import <AddressBook/AddressBook.h>
//地址薄框架提供联系人数据库,通讯数据库等
#import <AddressBookUI/AddressBookUI.h>
//地址薄UI框架提供编辑,选择,创建通讯数据库
#import "IIAddressBook.h"
//提供解析通讯录数据的一系列方法

@interface ContactViewController : UIViewController<ABPeoplePickerNavigationControllerDelegate,
ABNewPersonViewControllerDelegate,
ABPersonViewControllerDelegate,
ABUnknownPersonViewControllerDelegate>
/**
 委托接口主要功能
ABPeoplePickerNavigationController:显示整个通讯录并可以选择一个联系人的信息
ABPersonViewController:显示一个具体联系人的信息
ABNewPersonViewController:增加一个新的联系人
ABUnknownPersonViewController:完善一个联系人的信息
 **/


@property (nonatomic, strong) IBOutlet UILabel *label;
/*
 多线程,strong<>IBOutlet引用计数加2,IBOutlet<>IB控件关联
 监听中间区域 lable文本
*/

/**
 IBAction<>IB控件动作相关联
 **/
- (IBAction)showPeoplePickerController; //监听查看按钮
- (IBAction)showNewPersonViewController;//监听新增按钮
- (IBAction)showPersonViewController; //监听编辑按钮
- (IBAction)showUnknownPersonViewController;//监听未知按钮
@end


ContactViewController.m 文件
#import "ContactViewController.h"

@implementation ContactViewController

//合成器
@synthesize label;

/**
 手动初始化ViewController nibNameOrNil 直需要文件名,无需扩展即contact(.nib不需要)
**/

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    //初始化父类ViewController
    if (self) {
        // Custom initialization
    }
    return self; //跳出
}

/**
  当内存不足时,释放不需要的内存,缓存,未显示的view等
 **/
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
/**
 视图加载时调用
 **/
- (void)viewDidLoad
{
    // 增加Code按钮,可跳转至教学页面
    //初始化一个新的按钮,使用指定的标题,样式,目标(自己还是其他view视图),出口方法。
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Code" style:UIBarButtonItemStyleBordered target:self action:@selector(code)];
    //设置按钮到右侧
    self.navigationItem.rightBarButtonItem = item;
    //初始化
    [super viewDidLoad];
}

/**
 当内存不足时,最大程度释放内存
 **/
- (void)viewDidUnload
{
    self.label = nil; //重置lable
    [super viewDidUnload]; //释放内存
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

/**
 是否支持iphone 旋转
 **/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //系统默认不支持旋转功能
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



#pragma mark -
#pragma mark Show all contacts
// Called when users tap "Display Picker" in the application. Displays a list of contacts and allows users to select a contact from that list.
// The application only shows the phone, email, and birthdate information of the selected contact.
/**
 查看联系人数据
 1、先初始化系统通讯控件
 2、设置需要显示的信息,手机等
 3、用presentModalViewController 呈现
 **/
-(void)showPeoplePickerController
{
    // 查看联系人   系统的通讯录控件
	ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self; //委托给自己
	// Display only a person's phone, email, and birthdate
   
	/**
    消息设置版
    [picker setDisplayedProperties:[NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
                                    [NSNumber numberWithInt:kABPersonEmailProperty],
                                    [NSNumber numberWithInt:kABPersonBirthdayProperty], nil]];
    
    //属性版
     picker.displayedProperties = NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
     [NSNumber numberWithInt:kABPersonEmailProperty],
     [NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
    */
    
     //将手机,邮箱,生日数据压入数组
    NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],
                               [NSNumber numberWithInt:kABPersonEmailProperty],
                               [NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
	
    //设置需要显示的数据
	picker.displayedProperties = displayedItems;
     
	// Show the picker
    //显示数据 使用 presentModalViewController可创建模式对话框,可用于视图之间的切换 
	[self presentModalViewController:picker animated:YES];

}


#pragma mark Display and edit a person
// Called when users tap "Display and Edit Contact" in the application. Searches for a contact named "Appleseed" in 
// in the address book. Displays and allows editing of all information associated with that contact if
// the search is successful. Shows an alert, otherwise.
/**
  编辑地址薄数据
 1、ABAddressBookCreate 得到地址薄
 2、查找是否有张三数据
 3、如果没有提示用户,有则进入编辑视图
 **/
-(void)showPersonViewController
{
	// 得到地址薄 iphone6.0不推荐使用
	ABAddressBookRef addressBook = ABAddressBookCreate();
	// 寻找名为“CC”的联系人
	NSArray *people = (__bridge NSArray *)ABAddressBookCopyPeopleWithName(addressBook, CFSTR("zhangsan"));
	// Display "Appleseed" information if found in the address book
    //如果有数据
	if ((people != nil) && [people count])
	{
        //ABRecordRef 一个指向Core Foundation对象的通用指针
		ABRecordRef person = (__bridge ABRecordRef)[people objectAtIndex:0];
		ABPersonViewController *picker = [[ABPersonViewController alloc] init]; //初始化
		picker.personViewDelegate = self;
		picker.displayedPerson = person;
        //精简版 picker.displayedPerson = (__bridge ABRecordRef)[people objectAtIndex:0];
		// Allow users to edit the person’s information
		picker.allowsEditing = YES;
        //更新联系人数据到编辑视图
		[self.navigationController pushViewController:picker animated:YES];
	}
	else  //没有数据则给一些提示信息
	{
		// Show an alert if "Appleseed" is not in Contacts
        //UIAlertView常用于应用界面信息警告提示 
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
														message:@"Could not find 张三 in the Contacts application" 
													   delegate:nil 
											  cancelButtonTitle:@"Cancel" 
											  otherButtonTitles:nil];
		[alert show]; //弹出提示
	}
	
	CFRelease(addressBook); //释放
}


#pragma mark Create a new person
// Called when users tap "Create New Contact" in the application. Allows users to create a new contact.
-(void)showNewPersonViewController
{
    // 新增联系人
	ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
	picker.newPersonViewDelegate = self;
	
	UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
	[self presentModalViewController:navigation animated:YES];
		
}


#pragma mark Add data to an existing person
// Called when users tap "Edit Unknown Contact" in the application. 
-(void)showUnknownPersonViewController
{
	ABRecordRef aContact = ABPersonCreate();
	CFErrorRef anError = NULL;
	ABMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);
	bool didAdd = ABMultiValueAddValueAndLabel(email, @"John-Appleseed@mac.com", kABOtherLabel, NULL);
	
	if (didAdd == YES)
	{
		ABRecordSetValue(aContact, kABPersonEmailProperty, email, &anError);
		if (anError == NULL)
		{
			ABUnknownPersonViewController *picker = [[ABUnknownPersonViewController alloc] init];
			picker.unknownPersonViewDelegate = self;
			picker.displayedPerson = aContact;
			picker.allowsAddingToAddressBook = YES;
		    picker.allowsActions = YES;
			picker.alternateName = @"John Appleseed";
			picker.title = @"John Appleseed";
			picker.message = @"Company, Inc";
			
			[self.navigationController pushViewController:picker animated:YES];
		
		}
		else 
		{
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
															message:@"Could not create unknown user" 
														   delegate:nil 
												  cancelButtonTitle:@"Cancel"
												  otherButtonTitles:nil];
			[alert show];
		}
	}	
	CFRelease(email);
	CFRelease(aContact);
}


#pragma mark ABPeoplePickerNavigationControllerDelegate methods
// Displays the information of a selected person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    self.label.text = [IIAddressBook getFullName:person];
    //[self dismissModalViewControllerAnimated:YES];
    //return NO;
	return YES;
}


// Does not allow users to perform default actions such as dialing a phone number, when they select a person property.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
								property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
	return NO;
}


// Dismisses the people picker and shows the application when users tap Cancel. 
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
{
	[self dismissModalViewControllerAnimated:YES];
}


#pragma mark ABPersonViewControllerDelegate methods
// Does not allow users to perform default actions such as dialing a phone number, when they select a contact property.
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person 
					property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
	return NO;
}


#pragma mark ABNewPersonViewControllerDelegate methods
// Dismisses the new-person view controller. 
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
	[self dismissModalViewControllerAnimated:YES];
}


#pragma mark ABUnknownPersonViewControllerDelegate methods
// Dismisses the picker when users are done creating a contact or adding the displayed person properties to an existing contact. 
- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownPersonView didResolveToPerson:(ABRecordRef)person
{
	[self dismissModalViewControllerAnimated:YES];
}


// Does not allow users to perform default actions such as emailing a contact, when they select a contact property.
- (BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person 
						   property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
	return NO;
}

// 跳转至教学页面
- (void)code
{
    CodeViewController *controller = [[CodeViewController alloc] initWithNibName:@"CodeViewController" bundle:nil];
    NSString *name = [NSString stringWithUTF8String:object_getClassName(self)];
    controller.className = name;
    
    [self.navigationController pushViewController:controller animated:YES];
}

@end

分享到:
评论

相关推荐

    mybatis源码分析视频

    mybatis的源码分析视频,有详细的视频和文档。 视频地址亲测有效。失效了,请联系我。

    Linux网络地址转换NAT源码分析[定义].pdf

    Linux网络地址转换NAT源码分析[定义].pdf

    MapReduce 2.0源码分析与编程实

    《MapReduce2.0源码分析与编程实战》比较系统地介绍了新一代MapReduce2.0的理论体系、架构和程序设计方法。全书分为10章,系统地介绍了HDFS存储系统,Hadoop的文件I/O系统,MapReduce2.0的框架结构和源码分析,...

    lvs源码分析 ipvs源码分析

    lvs源码分析,介绍的很详细。均衡调度算法,IPVS 的连接管理,IPVS 的协议管理,IPVS 的数据包发送,IPVS 的应用管理

    Linux常见驱动源码分析(kernel hacker修炼之道全集)--李万鹏

    Linux常见驱动源码分析(kernel hacker修炼之道)--李万鹏 李万鹏 IBM Linux Technology Center kernel team 驱动资料清单内容如下: Linux设备模型(中)之上层容器.pdf Linux设备模型(上)之底层模型.pdf Linux...

    openswan的Pluto源码分析以及linux的IPsec内核源码分析

    对openswan的Pluto源码和linux的IPsec内核源码进行了详细的分析,对于理解openswan的运行机制以及linux的IPsec实现很有帮助

    Hadoop源码分析(完整版)

    Hadoop源码分析(完整版),详细分析了Hadoop源码程序,为学习Hadoop的人提供很好的入门指导

    ANDROID源码分析实录

    《Android源码分析实录》共分为15章,主要内容包括走进Android世界、硬件抽象层详解、分析JNI(Java本地接口)层、Android内存系统分析、Andmid虚拟机系统详解、IPC通信机制详解、Zygote进程/System进程和应用程序...

    深入理解Spark+核心思想与源码分析.pdf

    深入理解Sp深入理解SPARK:核心思想与源码分析》结合大量图和示例,对Spark的架构、部署模式和工作模块的设计理念、实现源码与使用技巧进行了深入的剖析与解读。 《深入理解SPARK:核心思想与源码分析》一书对Spark...

    redis源码分析PDF

    redis源码级别的分析。需要具备c语言基础的开发学习,十分详细,带你全程深入了解redis。

    lighttpd源码分析

    各基本数据结构的分析整理 lighttpd源码最核心的东西(比如配置信息的加载 比如对客户请求访问的响应 等)很齐全的源码分析资料,是掌握反向代理的好东东,后续敬请期待,不断推出中

    易语言源码易语言电话薄源码.rar

    易语言源码易语言电话薄源码.rar 易语言源码易语言电话薄源码.rar 易语言源码易语言电话薄源码.rar 易语言源码易语言电话薄源码.rar 易语言源码易语言电话薄源码.rar 易语言源码易语言电话薄源码.rar易语言源码...

    Zookeeper源码分析

    zookeeper源码分析(一)工作原理概述 zookeeper源码分析(二)FastLeader选举算法 Zookeeper源码分析之Paxos算法之旅

    VLC源码及源码分析文档

    1: VLC的最新源代码: 2011.02.24 2: 阅读VLC源代码时总结的笔记,结构比较清晰!!! (阅读笔记是从网上搜集的,与源码打包在一起,方便大家阅读学习!)

    Linux内核源码分析(0.11版本)

    最近因为课程的关系,学到操作系统原理了,忍不住想要...2.Linux内核源码分析 3.linux-0.00-041217.tar.gz 4.linux-0.11-040327-rh9.tar.gz 说明:最后两个都是Linux早期版本源码包,估计已经很难找到了(反正我是没找到)

    scratch2.0的源码分析

    这是一篇自己关于scratch2.0源码的一些见解,由于自己知识有限,不能保证十分正确,大家可以且看且怀疑

    duilib之源码分析

    帮助初学者学习duilib,里面有相关API函数的注释。

    入侵检测snort源码分析经典版(详细)

    九贱的源码分析,非常经典

    sqlite源码及分析

    文档包含sqlite源码,源码分析,以及使用教程

    thingsboard源码分析,绝对有用

    学习thingsboard,这个资料绝对的精品,分享出来,希望能够帮到大家!

Global site tag (gtag.js) - Google Analytics