registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later, so?

Some body must have faced the problem like me that the registerForRemoteNotificationTypes: doesn’t works on iOS 8 or later device. I found the solution and I think this is the better way to keep backwards compatibility and make workable the remote notification for iOS 8.0 and later versions if we go with this approach, it is working for my case and will hope work for you. Also pretty easy to understand.

So to register write this code in AppDelegate’s - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { method:


    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
             (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
    }

Now you will get a confirmation dialog at the starting time. if you all this app for push notifications, then you will get the device token for notification service from following delegate:(if supports all the versions we have)

#pragma mark - Notification delegates
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"content---%@", token);
    
}

If it fails for any reason like certification error or any other reason, following delegate will be called.

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    
    NSLog(@"Error %@", err);
    
}

So, now you have device token and you can do anything you like..

How to make a UITableView scroll to the text field and also prevent from the hiding by keyboard when text field is selected

Most of us faced some problem with UITextField is hidden by keyboard, I found a solution for my case, I hope it will also help you. My solution as follows. I am expecting that you already have a cell with UITextField. So on preparing the cell just keep the row index into the text field’s tag.

    cell.textField.tag = IndexPath.row;

Create an activeTextField, instance of UITextField with global scope as below:

    @interface EditViewController (){

        UITextField *activeTextField;

    }

So, now you just copy paste my code at the end. And also don’t forget to add UITextFieldDelegate

    #pragma mark - TextField Delegation

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

        activeTextField = textField;

        return YES;
    }

    - (void)textFieldDidEndEditing:(UITextField *)textField{

        activeTextField = nil;

    }

Registers keyboard notifications

    #pragma mark - Keyboard Activity

    - (void)registerForKeyboardNotifications

    {

        [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWasShown:)

                                                 name:UIKeyboardDidShowNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(keyboardWillBeHidden:)

                                                 name:UIKeyboardWillHideNotification object:nil];

    }

Handles Keyboard Notifications:

Called when the UIKeyboardDidShowNotification is sent.

    - (void)keyboardWasShown:(NSNotification*)aNotification

    {

        NSDictionary* info = [aNotification userInfo];

        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

        [self.tableView setContentInset:contentInsets];

        [self.tableView setScrollIndicatorInsets:contentInsets];

        NSIndexPath *currentRowIndex = [NSIndexPath indexPathForRow:activeTextField.tag inSection:0];

        [self.tableView scrollToRowAtIndexPath:currentRowIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];

    }

Called when the UIKeyboardWillHideNotification is sent

    - (void)keyboardWillBeHidden:(NSNotification*)aNotification

    {

        UIEdgeInsets contentInsets = UIEdgeInsetsZero;

        [self.tableView setContentInset:contentInsets];

        [self.tableView setScrollIndicatorInsets:contentInsets];

    }

Now one thing is left, Call the registerForKeyboardNotifications method in to ViewDidLoad method as follows:

    - (void)viewDidLoad {

        [super viewDidLoad];

        // Registering keyboard notification

        [self registerForKeyboardNotifications];

        // Your codes here...

    }

You are done, hope your text fields will no longer hidden by the keyboard.

How to use the “tilt” feature on an iphone

You can do this by accelerometer. Accelerometer will give you the value for x y z axis value when you tilt the device. to this add a property which is type of UIAccelerometer class.

    UIAccelerometer*  theAccelerometer;
	

now you define the frequency and the delegate. you should write this code from where you want to initiate receiving.

    theAccelerometer = [UIAccelerometer sharedAccelerometer];
    theAccelerometer.updateInterval = 1 / 50;
    theAccelerometer.delegate = self;
	

Now you have to add the delegation method

    -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
        myX = acceleration.x;
        myY = acceleration.y;
        myZ = acceleration.z;
    }
	

Ok, now you have the value updated frequently at frequency 50 [means 20 mili second]. Now, if you want to stop receiving these values and obviously you should stop receiving if you leave this view and that will as follows:

    theAccelerometer.delegate = nil
	

Again you can start receiving accelerometer data again assigning the delegate to self

    theAccelerometer.delegate = self;
	

if you have any further question please feel free to contact.

Objective-C and sqlite’s DATETIME type

Well, I am sharing here just the core things regarding date formatting for saving and retrieving the data from Sqlite to Objective C or vice versa. If you have any problem with this code snippet then I will share the full code that I used for my project.

When you save your data, bind your date value in the sql statement like this way:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString=[dateFormat stringFromDate:[NSDate date];];

    sqlite3_bind_text(saveStmt, 1, [dateString UTF8String] , -1, SQLITE_TRANSIENT);
	

and when you retrieve data you have to write this code:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *myDate =[dateFormat dateFromString:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)]];
	

now you have a variable myDate of NSDate type which you can render in your way:

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
    NSLog(@"My Date was : %@", [formatter stringFromDate:myDate]);
	

You must have to remember 3 things:

  1. in your SQLite date field type should be DATETIME
  2. Date format should be same when you store and when you retrieve
  3. Now you can show in your won way but following the format. Bellow the format details has given.

Format:

    'dd' = Day 01-31
   'MM' = Month 01-12
   'yyyy' = Year 2000
   'HH' = Hour in 24 hour
   'hh' = Hour in 12 hour
   'mm' = Minute 00-59
   'ss' = Second 00-59
   'a' = AM / PM