Thursday, July 18, 2013

Can not save NSDictionary with NSUserDefaults!!

One of the reason is one of which is of type NSNull which is not a property list value. iterate over the keys of the dictionary I just produced so conveniently with dictionaryWithValuesForKeys and remove those of type NSNull



NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:currentNullValueDic];
 for( id key in [dict allKeys] )
  {
       if( [[dict valueForKey:key] isKindOfClass:[NSNull class]] )
          {
                          [dict setObject:@"" forKey:key];
          }
}

Tuesday, May 28, 2013

Alternative to iPhone device ID (UDID)

You can get iPhone device ID using this "udid" method and you can download OpenUDID class from https://github.com/ylechelle/OpenUDID 

+ (NSString *)udid
{

    NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
   
    static NSString *uuid = nil;
   
    // try to get the NSUserDefault identifier if exist
    if (uuid == nil) {
       
        uuid = [standardUserDefault objectForKey:@"UniversalUniqueIdentifier"];
    }
   
    // if there is not NSUserDefault identifier generate one and store it
    if (uuid == nil) {
       
        uuid = [self genarateUDID];
        [standardUserDefault setObject:uuid forKey:@"UniversalUniqueIdentifier"];
        [standardUserDefault synchronize];
    }
   
    return uuid;
}

+ (NSString *)genarateUDID
{
    NSString *uniqiPhoneID;
   

    if (!NSClassFromString(@"ASIdentifierManager")) {
        // This is will run before iOS6 and you can use openUDID, per example...
        uniqiPhoneID = [OpenUDID value];
    }
    if (uniqiPhoneID == nil || [uniqiPhoneID isEqual:[NSNull null]])
    {
       
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        CFStringRef udidString = CFUUIDCreateString(NULL, uuid);
        CFRelease(uuid);
       
        uniqiPhoneID = (__bridge NSString *)(udidString);
       
    }
   
    return uniqiPhoneID;

}

How to Check Null or Empty in given String

+ (BOOL)isNullOrEmpty:(NSString *)string
{
    if (string == nil)  {
        return YES;
    }
    if ((![string isKindOfClass:[NSNumber class]]) && (![string isKindOfClass:[NSString class]]) && (![string isKindOfClass:[NSDictionary class]]) && (![string isKindOfClass:[NSArray class]])) {
        return YES;
    }
   
    if (([string isKindOfClass:[NSString class]]) && ([string isEqualToString:@""])){
        return YES;
    }
   
    return NO;
}

How to compare date

//-------compare date ------------------------///
    NSDate *today = [NSDate date]; // it will give you current date

    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MM/dd/yyyy"];
    NSDate *todayFormat = [format dateFromString:[format stringFromDate:today]]; // your date
   
    NSComparisonResult result;
    //has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
   
    result = [todayFormat compare:historyDate]; // comparing two dates
    NSString *dateString;
   
    if(result==NSOrderedAscending || result==NSOrderedDescending){
        [format setDateFormat:@"EEEE MMM dd yyyy"];
        dateString = [format stringFromDate:historyDate];
    }else{
        dateString  = @"Today";
    }

Get Local time when have GMT time

- (NSDate *)converteLocalTimeWithGMT:(NSDate *)GMTTime
{
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MM/dd/yyyy"];
   
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    NSInteger timeStamp = [GMTTime timeIntervalSince1970];
    NSInteger fullTime = timeStamp + timeZone.secondsFromGMT;
   
    return [format dateFromString:[format stringFromDate:[NSDate dateWithTimeIntervalSince1970:fullTime]]];  // return Local time

}

Friday, January 18, 2013

Fixed Auto Rotate Issue in iOS6

In iOS6 "shouldAutorotateToInterfaceOrientation" method does not call but it is work fine in iOS 5.0 or 5.1. If you have this problem, just add this code to the AppDelegate.m class.


#pragma mark -
#pragma mark Rotation support

    // Ensure that the view controller supports rotation 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//    Here just add Orientation that you want.
    NSInteger orientationMask = 0;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeLeft])
        orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight])
        orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortrait])
        orientationMask |= UIInterfaceOrientationMaskPortrait;
    if ([self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationPortraitUpsideDown])
        orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
    return orientationMask;
}

And also make sure to set Orientation in info.plist