Wednesday 2 July 2014

Push Notification in ios example

https://github.com/NicosKaralis/pushmeup

http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1

Friday 6 June 2014

scheduled timer in background in iphone sdk


UIBackgroundTaskIdentifier backgroundTask;

timer = [NSTimer scheduledTimerWithTimeInterval:1200.0
                                                        target:self
                                                      selector:@selector(runTimer)
                                                      userInfo:nil
                                                       repeats:YES];
   backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"Background handler called. Not running background tasks anymore.");
        [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
       backgroundTask = UIBackgroundTaskInvalid;
    }];

-(void)runTimer{
}

-(void)stopTimer{
 [timer invalidate];
    timer = nil;
    if (self.backgroundTask != UIBackgroundTaskInvalid)
    {
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
        self.backgroundTask = UIBackgroundTaskInvalid;
    }
}

Wednesday 4 June 2014

How to make slider menu in iphone sdk?

https://github.com/mikefrederick/MFSideMenu

get Contact email and name using ABAddressBook


-(void)getContactsList{
    CFErrorRef error = NULL;
    
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    
    __block BOOL accessGranted = NO;
    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        
    }
    else { // we're on iOS 5 or older
        accessGranted = YES;
    }
    
    if (accessGranted) {
        ABAddressBookCreateWithOptions(NULL, &error);
    if (addressBook != nil)
    {
        NSLog(@"Succesful.");
        
        NSArray *allContacts = (__bridge_transfer NSArray
                                *)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger i = 0;
        for (i = 0; i < [allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
            NSString *firstName = (__bridge_transfer NSString
                                   *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName =  (__bridge_transfer NSString
                                   *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
            NSString *fullName = [NSString stringWithFormat:@"%@ %@",
                                  firstName, lastName];
            ABMutableMultiValueRef multiEmail;
            multiEmail = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);
            NSString *emailAddress;
            emailAddress = (__bridge NSString *) ABMultiValueCopyValueAtIndex(multiEmail, 0);
            NSMutableDictionary *myAddressBook = [NSMutableDictionary dictionary];

            [myAddressBook setValue:emailAddress forKey:@"email"];
            [myAddressBook setValue:fullName forKey:@"fullName"];
            
            [arrContacts addObject:myAddressBook];
            
            //[arrContacts addObject:[myAddressBook valueForKey:@"email"]];
        }
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"fullName" ascending:YES];
        [arrContacts sortUsingDescriptors:[NSArray arrayWithObject:sort]];
    }          
    
    CFRelease(addressBook);
    }

}

Tuesday 3 June 2014

NSMutableArray sorting in alphabatically


 NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"your Key" ascending:YES];

       
 [yourarray sortUsingDescriptors:[NSArray arrayWithObject:sort]];

Wednesday 28 May 2014

Textfield hide when keyboard open

//write this code in viewdidload method

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:self.view.window];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:self.view.window];

    keyboardIsShown = NO;


- (void)keyboardWillHide:(NSNotification *)n
{
    NSDictionary* userInfo = [n userInfo];
    
    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    
    // resize the scrollview
    CGRect viewFrame = scrView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height += (keyboardSize.height - 100);
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [scrView setFrame:viewFrame];
    [UIView commitAnimations];
    
    keyboardIsShown = NO;
}

- (void)keyboardWillShow:(NSNotification *)n
{
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the `UIScrollView` if the keyboard is already shown.  This can happen if the user, after fixing editing a `UITextField`, scrolls the resized `UIScrollView` to another `UITextField` and attempts to edit the next `UITextField`.  If we were to resize the `UIScrollView` again, it would be disastrous.  NOTE: The keyboard notification will fire even when the keyboard is already shown.
    if (keyboardIsShown) {
        return;
    }
    
    NSDictionary* userInfo = [n userInfo];
    
    // get the size of the keyboard
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    // resize the noteView
    CGRect viewFrame = scrView.frame;
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
    viewFrame.size.height -= (keyboardSize.height - 100);
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [scrView setFrame:viewFrame];
    [UIView commitAnimations];
    [scrView scrollRectToVisible:activeField.frame animated:YES];
    keyboardIsShown = YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    activeField = textField;
    return YES;
}

Textfield text search on tableview


txtSearch.returnKeyType = UIReturnKeyDone;
    txtSearch.autocorrectionType = UITextAutocorrectionTypeNo;
    [txtSearch addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];


-(void)textFieldDidChange:(UITextField *)txtFld {
    NSString * match = txtFld.text;
    NSArray *listFiles = [[NSMutableArray allocinit];
    NSPredicate *sPredicate = [NSPredicate predicateWithFormat:
                               @"SELF CONTAINS[cd] %@", match];
    
    listFiles = [NSArray arrayWithArray:[[arrGetTask valueForKey:@"task_name"]
                                         filteredArrayUsingPredicate:sPredicate]];
    // Now if you want to sort search results Array
    //Sorting NSArray having NSString as objects
    NSString *strFilter = [match substringFromIndex:0];
    NSLog(@"%@",strFilter);
    if ([strFilter isEqualToString:@"#"]) {
        arrFilter = nil;
    }
    else{
        arrFilter = [[NSMutableArray alloc]initWithArray: [listFiles
                                                           sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
    }
    
   
    
    NSLog(@"%@",arrFilter);
    
    [tblView reloadData];
    //Use sorted array as your Table’s data source
   // -make your table unhidden: yourTbl.hidden = FALSE;
   // - reload your table  : [yourTbl reloadData];

}