ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Email 和 addressbook

Email 和 addressbook

原创 Linux操作系统 作者:c00lsun 时间:2009-04-28 13:34:21 0 删除 编辑
不知是放在这里还是资源里,抱歉。

我个人感觉 iphone的 addressbook api 还是有些难度。学起来费时费力,做了个例子给大家。希望可以节省一些时间和精力。

把你iphone 上的私有数据用email发出
注:simulator上没有mail, 只能看到界面。要是真发Email,还得用device。

1。建立个简易界面和一个button 叫 “send Email”(action:doEmail:)
2。在doEmail 里
建立ABPeoplePickerNavigationController作为察看address book 的界面。
把self 作为 ABPeoplePickerNavigationControllerDelegate 的delegate
3。实现3个 ABPeoplePickerNavigationControllerDelegate 的方法
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
不要被长名字吓倒,实现起来不难。
4。实现一个helper方法:-(void)sendDataViaEmail:(NSString*)email
//主要是建立一个mailto的NSString 我把一个2D的array加在body里面。这样就可以把你的local数据以email发出。

最后,codes是在main()里。这种style我不提倡。为了upload方便不得已。
//
//  main.m
// EmailMe
//
//  Created by 张海男 on 2/17/09.
//
 
#import
#import
#import
 
@interface EmailMeViewController : UIViewController {
    NSMutableArray *data;
}
@end
 
@implementation EmailMeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.title = @"Email me";
        self.tabBarItem.image = [UIImage imageNamed:@"about_tabBar.png"];
 
        //let's set up data first:
        data = [NSMutableArray arrayWithObjects:[NSMutableArray arrayWithObjects:@"front1",@"back1",@"date1",nil],
                [NSMutableArray arrayWithObjects:@"front2",@"back2",@"date2",nil],
                [NSMutableArray arrayWithObjects:@"front3",@"back3",@"date3",nil],
                [NSMutableArray arrayWithObjects:@"front4",@"back4",@"date4",nil],
                [NSMutableArray arrayWithObjects:@"front5",@"back5",@"date5",nil],nil];
        [data retain];
    }
    return self;
}
 
- (void) loadView {
    self.view = [[UIView alloc] init];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(doEmail:)];
}
-(IBAction) doEmail: (id) sender{
    ABPeoplePickerNavigationController *pplPicker = [[ABPeoplePickerNavigationController alloc]init];
    pplPicker.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonEmailProperty]];
    pplPicker.addressBook = ABAddressBookCreate();
    pplPicker.peoplePickerDelegate = self;
    [self presentModalViewController:pplPicker animated:YES];
    [pplPicker release];
}
-(void)sendDataViaEmail:(NSString*)email{
    //building email content:
    NSString *emailContent = @"This%20is%20an%20Email%20from%20ProjectBlah,%0A";
    for (int i = 0;i<[data count];i++){
        emailContent = [emailContent stringByAppendingString: [[data objectAtIndex:i]objectAtIndex:0]];
        emailContent = [emailContent stringByAppendingString: @"%20"];
        emailContent = [emailContent stringByAppendingString: [[data objectAtIndex:i]objectAtIndex:1]];
        emailContent = [emailContent stringByAppendingString: @"%20"];
        emailContent = [emailContent stringByAppendingString: [[data objectAtIndex:i]objectAtIndex:2]];
        emailContent = [emailContent stringByAppendingString: @"%20%0A"];
    }
    NSString *urlString = [NSString stringWithFormat: @"mailto:%@?subject=%@&body=%@",email,@"Your%20Card%20from%20ProjectBlah",emailContent];
    NSURL *url = [NSURL URLWithString:urlString];
    [[UIApplication sharedApplication] openURL:url];
}
 
#pragma mark ABPeoplePickerNavigationControllerDelegate
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
    [self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    ABMultiValueRef emails = ABRecordCopyValue(person, property);
    CFIndex emailIndex = ABMultiValueGetIndexForIdentifier(emails, identifier);
    CFStringRef email = ABMultiValueCopyValueAtIndex(emails, emailIndex);
    [self sendDataViaEmail:(NSString*)email];
    CFRelease(email);
    CFRelease(emails);
 
    [self dismissModalViewControllerAnimated:YES];
    return NO;
}
- (void)dealloc {
    [data release];
    [super dealloc];
}
@end
 
@interface EmailMeAppDelegate : NSObject
{}
@end
@implementation EmailMeAppDelegate
 
- (void)applicationDidFinishLaunching:(UIApplication *)application {   
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after app launch
    EmailMeViewController *viewController = [[EmailMeViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:viewController];
    [viewController release];
    [window addSubview:nvc.view];
    [window makeKeyAndVisible];
}
 
 
- (void)dealloc {
    [super dealloc];
}
@end
 
int main(int argc, char *argv[]) {
 
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"EmailMeAppDelegate");
    [pool release];
    return retVal;
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9059159/viewspace-592074/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2009-03-23

  • 博文量
    31
  • 访问量
    46948