Category Archives: ios

Filtrar solo los contactos que tienen email

utilizo una clase auxiliar para guardar los datos y poder ordenarlos.
Contacto.h
#import

@interface Contacto : NSObject{
NSNumber *numero;
NSString *email;
NSString *nombre;
}

@property (nonatomic, strong) NSNumber *numero;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) NSString *nombre;

@end

el codigo es:
addressBook = ABAddressBookCreate();

self.people = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
self.filteredPeople = [NSMutableArray array];

for (id record in people)
{
ABRecordRef person = (__bridge ABRecordRef)record;
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

NSString *email;
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
if (ABMultiValueGetCount(emails)>0){
email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, 0);
if (![email isEqualToString:@””]){
// Match by name or organization
ABRecordID abRecordID = ABRecordGetRecordID(person);

// Add the matching abRecordID to filteredPeople
Contacto *c = [[Contacto alloc]init];
c.numero=[NSNumber numberWithInt:abRecordID];
c.email=email;
c.nombre=[NSString stringWithFormat:@”%@ %@”,[firstName capitalizedString],[lastName capitalizedString]];
[filteredPeople addObject:c];
}

}

}
//NSArray *sortedArray;
filteredPeople = [filteredPeople sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSString *first = [(Contacto*)a nombre];
NSString *second = [(Contacto*)b nombre];
return [first compare:second];
}];

ejemplo de action sheet

En el fichero .h hay que poner el delegado:
UIActionSheetDelegate

En el fichero .m

-(void)AbrirOpciones:(id)sender{
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIPickerView *chPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
chPicker.dataSource = self;
chPicker.delegate = self;
chPicker.showsSelectionIndicator = YES;
[pickerViewPopup addSubview:chPicker];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0,0,320, 464)];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
}
#pragma mark – opcion sobre el video
– (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@”Hemos elegido la opcion %d”,row);
switch (row) {
case 0:

[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
break;
case 1:

[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
break;
case 2:
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
break;
default:
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
break;
}

}

– (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 3;
}

– (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (row) {
case 0:
return @”texto 1″;
break;
case 1:
return @”texto 2″;
break;
case 2:
return @”texto 3″;
break;
default:
break;
}
return @””;
}
– (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}

hilos objective-c

[NSThread detachNewThreadSelector:@selector(startTheBackgroundJob) toTarget:self withObject:nil];
Mas información en : http://www.xprogress.com/snippet-10-example-of-threading-in-iphone-sdk-objective-c-using-nsthread/

saber si un hilo es principal
[NSThread isMainThread]

UIImage from URL

UIImage * img = [[UIImage alloc] initWithData:
[NSData dataWithContentsOfURL:
[NSURL URLWithString:@”http://example.com/image.png”]]];

Si queremos cargar la imagen de un archivo del proyecto:

[UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@”thefilename” ofType:@”jpg”]];