Wednesday, May 16, 2012

Macport installing sudo: port: command not found

While installing the Macport on your mac, if you happen to find following error:
sudo: port: command not found

Following lines are useful to run before you do sudo port -v selfupdate.
as a result, above error will be gone.


export PATH=$PATH:/opt/local/bin
export MANPATH=$MANPATH:/opt/local/share/man
export INFOPATH=$INFOPATH:/opt/local/share/info


source

Monday, April 30, 2012

How to find if NSString contains decimal + certain other characters.


NSMutableCharacterSet *_mySet = [NSMutableCharacterSet characterSetWithCharactersInString:@"aAfF"];
[_mySet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet]];
NSCharacterSet *myStringSet = [NSCharacterSet characterSetWithCharactersInString:mystring];

if ([_mySet isSupersetOfSet: myStringSet])
{
//This is will be true if myString contains ONLY " decimal numbers(0-9) and characters a,A,f,F"
        
}



Note: First 2 code lines tell you how to join 2 character sets.

How to check if NSString contains only numbers.




NSCharacterSet *_NumericOnly = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *myStringSet = [NSCharacterSet characterSetWithCharactersInString:mystring];
    
if ([_NumericOnly isSupersetOfSet: myStringSet])
{
    //String entirely contains decimal numbers only.
   
        
}






Friday, April 27, 2012

How to capitalize first letter of NSString ?


NSString *myString = @"i want first letter capitalized.";
NSString *newString = [myString stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[myString substringToIndex:1] capitalizedString]];
sdsad

This will give output of newString as  I want first letter capitalized

Fetch address for a contact from addressbook.


    ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonAddressProperty);
    
    if(multi)
    {
        NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(multi);
        for (int i = 0; i < [address count]; i++) {
        {
            NSDictionary *addressDict=[address objectAtIndex:i];
            NSString *country = [addressDict objectForKey:(NSString *)kABPersonAddressCountryKey];
            NSString *zip = [addressDict objectForKey:(NSString *)kABPersonAddressZIPKey];
            NSString *city = [addressDict objectForKey:(NSString *)kABPersonAddressCityKey];
            NSString *street = [addressDict objectForKey:(NSString *)kABPersonAddressStreetKey];
            
        }
    }


Similarly, you can fetch phone numbers as well as email address and other details by changing property kABPersonAddressProperty to corresponding property.

How to append phone number to an existing contact.


    //We need to create a mutable copy of ABMultiValueRef with property kABPersonPhoneProperty
    
    ABMultiValueRef Phones = ABRecordCopyValue(contact, kABPersonPhoneProperty);
    ABMutableMultiValueRef Phone = ABMultiValueCreateMutableCopy(Phones);
    ABMultiValueAddValueAndLabel(Phone,@"413-675-1234", kABOtherLabel, NULL);
    ABMultiValueAddValueAndLabel(Phone@"413-675-1235", kABOtherLabel, NULL); 
    ABRecordSetValue(contact, kABPersonPhonePropertyPhone,nil);
    CFRelease(Phone);

    //This will add 2 numbers with others title under phone section of contact.
    //You can use other label too like following kABWorkLabel or kABPersonPhoneMobileLabel 

    ABMultiValueAddValueAndLabel(Phone,@"413-675-1234"kABPersonPhoneMobileLabelNULL);
    ABMultiValueAddValueAndLabel(Phone@"413-675-1235"kABWorkLabelNULL); 


    //Dont forget to save your addressbook after that.

    ABAddressBookSave(addressbook, &error);
    if (error != NULL
    {
        NSLog(@"failed");

    }
      NSLog(@"success.");

Note: If you don't make a  mutable copy, you will get following error for trying add  phone number to ABMultiValueRef instead of ABMutableMultiValueRef. As you created ABMultiValueRef by copying from ABRecordRef using method ABRecordCopyValue.
Assertion failed: (((ABCMultiValue *)multiValue)->flags.isMutable), function ABMultiValueAddValueAndLabel





Remove contact from address book

Remove a record from address book.


ABAddressBookRef addressbook = ABAddressBookCreate();
ABAddressBookRemoveRecord(addressbook, record, nil);
ABAddressBookSave(addressbook, nil);
CFRelease(addressbook);


----------------------------------------------------------------------------------------


Remove  record(s) from address book with specific name.

ABAddressBookRef addressbook = ABAddressBookCreate();
CFStringRef nameRef = (CFStringRef)[NSString stringWithFormat:@"%@ %@"@"Davis"@"Scott"];
CFArrayRef  AllRecords_ = ABAddressBookCopyPeopleWithName(addressbook, nameRef);
if (AllRecords_ != NULL
{
    int count = CFArrayGetCount(AllRecords_);          
    for (int i = 0; i < count; ++i) 
    {                
           ABRecordRef contact = CFArrayGetValueAtIndex(AllRecords_, i);
           ABAddressBookRemoveRecord(addressbook, contactnil);
    }
    
}
ABAddressBookSave(addressbook, nil);
CFRelease(addressbook);


Add new contact in address book



    CFErrorRef error = NULL;
    ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
    ABRecordRef newPerson = ABPersonCreate();
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty,@"Davis", &error);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Scott", &error);
    
    
    //Add my phone number
    ABMutableMultiValueRef PhoneVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(PhoneVar,valueForKey:@"6176352360", kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhonePropertyPhoneVar,nil);
    CFRelease(PhoneVar);
    
    
    //Add my email address
    ABMutableMultiValueRef EmailVar = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(EmailVar,@"d.scott@hotmail.com" , kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailPropertyEmailVar,nil);
    CFRelease(EmailVar);
    
   
    //Add my mailing address
    ABMutableMultiValueRef Address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
    [addressDictionary setObject:streetString forKey:(NSString *) kABPersonAddressStreetKey];
    [addressDictionary setObject:@"Boston" forKey:(NSString *)kABPersonAddressCityKey];
    [addressDictionary setObject:@"02124" forKey:(NSString *)kABPersonAddressZIPKey];
    [addressDictionary setObject:@"US" forKey:(NSString *)kABPersonAddressCountryKey];
    ABMultiValueAddValueAndLabel(AddressaddressDict, kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonAddressPropertyAddress,&error);
    CFRelease(Address);
    
    //Finally saving the contact in the address book
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    ABAddressBookSave(iPhoneAddressBook, &error);
    if (error != NULL
    {
        NSLog(@"Saving contact failed.");
    }


    NSLog(@"Contact saved.");

Wednesday, April 18, 2012

How to create a transparent background in CGContext ?



    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGRect a=self.frame;
    a.origin.x=0;
    a.origin.y=0;
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);    
    CGContextAddRect(context, a);
    CGContextFillPath(context);
    CGContextRestoreGState(context);



Don't forget to add the background color as clear color.



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}




How to compare CGPoint




if (CGPointEqualToPoint(point1,point2) == YES)
{
       NSLog(@"Points are equal");
}



Tuesday, April 17, 2012

MultiLine text on a UIButton



aButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
[aButton setTitle: @"View MIMChartLib \n Created by Reetu Raj" forState: UIControlStateNormal];