Tuesday 21 October 2014

Push-Notification Change in iOS8

in ios 8 so many things change in one of them one change is related to push notification.

If you run your old push notification code in Xcode 6 then it will give you warning also some methods deprecated in ios 8
registerForRemoteNotificationTypes is deprecated in ios 8 instead of it in ios 8 we use UIUserNotificationSettings like this way:

           #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
            if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
                UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
                [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
            } else {
                [[UIApplication sharedApplication]
                 registerForRemoteNotificationTypes:
                 (UIRemoteNotificationTypeAlert |
                  UIRemoteNotificationTypeBadge |
                  UIRemoteNotificationTypeSound)];
            }
        #else
            [[UIApplication sharedApplication]
             registerForRemoteNotificationTypes:
             (UIRemoteNotificationTypeAlert |
              UIRemoteNotificationTypeBadge |
           
 
     UIRemoteNotificationTypeSound)];
    #endif


and for delegate methods,



      #pragma mark - Push Notification
 
    - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
   
    // Prepare the Device Token for Registration (remove spaces and < >)
    NSString *TokenID = [[[[deviceToken description]
                               stringByReplacingOccurrencesOfString:@"<"withString:@""]
                              stringByReplacingOccurrencesOfString:@">" withString:@""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];
   
     
        [ApplicationPreferences setTokenId:TokenID];
    if (DEBUG_MODE) {
    NSLog(@"device token - %@",[NSString stringWithFormat:@"Device Token = %@",TokenID]);
            NSUUID *oNSUUID = [[UIDevice currentDevice] identifierForVendor];
            NSLog(@"Vendor token - %@",[NSString stringWithFormat:@"Device Token = %@",[oNSUUID UUIDString]]);
    }
 
    }
 
    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
    {
        //register to receive notifications
        [application registerForRemoteNotifications];
    }
 
    - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
        if (DEBUG_MODE) {
    NSLog(@"Push Error- %@",[NSString stringWithFormat: @"Error: %@", err]);
        }
    }
    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
     
    }

Thanks .

Sunday 12 October 2014

ios 8 changes for actionsheet


In ios 8 apple has changed so many things i here faced one problem in my own project so , i just want to provide the solution in replacement of UIActionsheet.

   in ios 8 UIActionSheet deprecated as well its delegate methods also deprecated. Instead of that UIAlertController with prefferedstyle 
UIAlertControllerStyleActionSheet  is used. you can check this thread as well:

https://developer.apple.com/library/ios/documentation/Uikit/reference/UIActionSheet_Class/index.html

This example will work both in ios 8 and previous version of it.



in .h file declare this variable


@interface demoProfileForBuyer{
 UIActionSheet *ac_sheet_fordate;
    UIToolbar *pickerToolBar_fordate;

    UIDatePicker *optionPickerDate;
UIAlertController *alertController;
}

in .m file

- (void)viewDidLoad
{

    [super viewDidLoad];
    pickerToolBar_fordate = [[UIToolbar alloc] initWithFrame:CGRectMake(-8, 0, 320, 44)];
    
    pickerToolBar_fordate.barStyle = UIBarStyleBlack;
    [pickerToolBar_fordate sizeToFit];
    
    NSMutableArray *barItemsDate = [[NSMutableArray allkioc] init];
    
    
    UIBarButtonItem *flexSpaceDate = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItemsDate addObject:flexSpaceDate];
    
    UIBarButtonItem *doneBtnDate = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerViewDoneBirthday)];
    [barItemsDate addObject:doneBtnDate];
    
    [pickerToolBar_fordate setItems:barItemsDate animated:YES];
    
    optionPickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(-8, 44, 320, 200)];
    
    
    optionPickerDate.datePickerMode = UIDatePickerModeDate;
    [optionPickerDate setMaximumDate: [NSDate date]];
    optionPickerDate.datePickerMode = UIDatePickerModeDate;
    optionPickerDate.hidden = NO;
    optionPickerDate.date = [NSDate date];
    optionPickerDate.backgroundColor = [UIColor whiteColor];
    
    if(IS_IOS_8){

        alertController = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:alertAction];
        [alertController addAction:alertAction];
        [alertController addAction:alertAction];
        [alertController addAction:alertAction];        
        [alertController.view addSubview:pickerToolBar_fordate];
        [alertController.view addSubview:optionPickerDate];        
    } else {
    ac_sheet_fordate = [[UIActionSheet alloc] initWithTitle:@"test"
                                                   delegate:self
                                          cancelButtonTitle:@"cancel"
                                     destructiveButtonTitle:nil
                                          otherButtonTitles:@"dev",@"dev", nil];
    [ac_sheet_fordate setActionSheetStyle:UIActionSheetStyleBlackOpaque];
    [ac_sheet_fordate setBackgroundColor:[UIColor blackColor]];
    [ac_sheet_fordate addSubview:pickerToolBar_fordate];
    [ac_sheet_fordate addSubview:optionPickerDate];
    }
    }

// when date picker open 

   - (IBAction)showBirthdayPicker:(id)sender{
    //    self.view.frame = CGRectMake(0, -220, self.view.frame.size.width, self.view.frame.size.height);
    UIButton *btnsender = sender;
    //[ac_sheet_fordate showInView:self.view];
    self.scrForEditProfile.contentSize = CGSizeMake(320, 1000);
    if(IS_IOS_8){
            popover = alertController.popoverPresentationController;
            if (popover)
            {
                popover.sourceView = sender;
                popover.sourceRect = CGRectMake(0, btnsender.bounds.origin.y, btnsender.bounds.size.width, btnsender.bounds.size.height);
                
                popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
            }
                [self presentViewController:alertController animated:YES completion:nil];
            
    } else {
    if(IS_IOS_7){
        [ac_sheet_fordate showInView:[UIApplication sharedApplication].keyWindow];
        ac_sheet_fordate.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-ac_sheet_fordate.frame.size.height, [UIScreen mainScreen].bounds.size.width, ac_sheet_fordate.frame.size.height);
    }
    else{
        [ac_sheet_fordate showInView:self.view];
    }
    }
    if(IS_IOS_7==FALSE && IS_HEIGHT_GTE_568==FALSE){
        [self.scrForEditProfile scrollRectToVisible:CGRectMake(self.txtDOB.frame.origin.x, self.txtDOB.frame.origin.y + 200, self.txtDOB.frame.size.width, self.txtDOB.frame.size.height) animated:NO];
    }
    else{
        [self.scrForEditProfile scrollRectToVisible:CGRectMake(self.txtDOB.frame.origin.x, self.txtDOB.frame.origin.y +300, self.txtDOB.frame.size.width, self.txtDOB.frame.size.height) animated:NO];
    }
    
}

// when date picker open user tap on done button

- (IBAction)pickerViewDoneBirthday{
    
    if(IS_IOS_8){
        [self.presentedViewController dismissViewControllerAnimated:NO completion:nil];
        
    } else {
        [ac_sheet_fordate dismissWithClickedButtonIndex:0 animated:YES];
   
    }

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    birthday_date = optionPickerDate.date;
    [self.btnShowDatePicker setTitle:[formatter stringFromDate:optionPickerDate.date] forState:UIControlStateNormal];
    [self.btnShowDatePicker setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btnShowDatePicker setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    self.scrForEditProfile.contentSize=CGSizeMake(320 , 700);
    [self.scrForEditProfile scrollsToTop];
    
}


I hope this will help, and this code will display the same action sheet kind of view as it displayed in ios 7 .