Thursday, 17 April 2014

open mapview with any address name from iphone application to apple map application

with bellow code you can open any location with its name to apple's mapview application with bellow code..


//    [[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];// you can open user current location with this 1 line  

    Class mapItemClass = [MKMapItem class];
    if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)])
    {
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder geocodeAddressString:yourAddressText
                     completionHandler:^(NSArray *placemarks, NSError *error) {
                         
                         // Convert the CLPlacemark to an MKPlacemark
                         // Note: There's no error checking for a failed geocode
                         CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc]
                                                   initWithCoordinate:geocodedPlacemark.location.coordinate
                                                   addressDictionary:geocodedPlacemark.addressDictionary];
                         
                         // Create a map item for the geocoded address to pass to Maps app
                         MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
                         [mapItem setName:geocodedPlacemark.name];
                         [mapItem openInMapsWithLaunchOptions:nil];

    }

Load URL in UIWebView with special character , append some string with bellow code.

Here you can add any code or string in your local file url with bellow code.

if your url like user/.../filename.htm and if you want to add some hash code of word after that url like user/.../filename.htm#green then you can use my bellow function.

-(void)loadWebView{
    NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"filename" ofType:@"html"];//here write your file name and its extention
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURL *fullURL = [NSURL URLWithString:@"#green" relativeToURL:url];//pass any code or string with append in url baseurl like this
    [yourWebView loadRequest:[NSURLRequest requestWithURL:fullURL]];
    [yourWebView setBackgroundColor:[UIColor clearColor]];
    [yourWebView setOpaque:NO];

}

Tuesday, 4 March 2014

Set and get Date in NSString format or NSDate format with different date format with my bellow method

You can get date with string return type and also set date format in which you want to get date For Ex: dd-MMM-yyyy (i.e. 05-Mar-2014)

-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    
    NSDate *date = [dateFormatter dateFromString:stringDate];
    [dateFormatter setDateFormat:getwithFormat];
    
    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSLog(@"Converted String : %@",convertedString);
    return convertedString;
}

NSString  *stringVar  = [self changeDateFormat:yourDateString dateFormat:@"yyyy-MM-dd" getwithFormat:@"dd MMM yyyy"];// set your date string and its date format and after set your required new date format

You can get date with NSDate return type and also set date format in which you want to get date For Ex: dd-MMM-yyyy (i.e. 05-Mar-2014) same like above but here you get return date in NSDate.

-(NSDate *)convertStringToDate:(NSString *)date dateFormat:(NSString *)dateFormat getWithFormat:(NSString *)getWithFormat {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDate *nowDate = [[NSDate alloc] init];
    [formatter setDateFormat:dateFormat];// set format here which format in string date
    date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
    nowDate = [formatter dateFromString:date];
    [formatter setDateFormat:getWithFormat];
    
    NSString *convertedString = [formatter stringFromDate:nowDate];
    nowDate = [formatter dateFromString:convertedString];
    // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
    return nowDate;

}

set size of UILable with its content text

With my bellow method you can set UILable size with its content text.

-(CGRect)setDynamicHeight:(UILabel *)lbl{
    CGRect myLabelFrame = [lbl frame];
  
    NSString *text = lbl.text;
    CGFloat width = lbl.frame.size.width;
    UIFont *font = lbl.font;
    NSAttributedString *attributedText =
    [[NSAttributedString alloc]
     initWithString:text
     attributes:@
     {
     NSFontAttributeName: font
     }];
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;
    
    myLabelFrame.size.height = size.height;
    
    return myLabelFrame;

}

and use this method with my bellow code

    [yourLable setFrame:[self setDynamicHeight:yourLable]];

Monday, 3 March 2014

How to add animation in View for any view or any Control.



With bellow code you can set animation for any control or any view...

        CATransition *animation = [CATransition animation];
        [animation setDuration:1.0];// Set Duration
        [animation setType:kCATransitionPush];
        [animation setSubtype:kCATransitionFromTop];// set different type of Transition
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        

        [[yourView layer] addAnimation:animation forKey:@"SwitchToView1"];// here you can set any view or any Control For Ex: UITableViewCell

Monday, 17 February 2014

Get Date and Time difference with different format with my custom method

You can get difference with bellow code

   NSString *strDifference = [self getDateDifferencewithTimeDay:yourOldDateString strDate2:[self StringFromDate:[NSDate date]]];
NSLog(@"\n Difference  =====>> %@", strDifference);

 Here i set old string and current string to get difference between that both dates.
Here i use two methods in above code one is getDateDifferencewithTimeDay for get difference between that dates and second is StringFromDate for get data in string format.


-(NSString *)getDateDifferencewithTimeDay:(NSString *)strDate1 strDate2:(NSString *)strDate2{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    NSDate *Date1;
    NSDate *Date2;
    NSString *strAnswer;
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    Date1 = [formatter dateFromString:strDate1];
    Date2 = [formatter dateFromString:strDate2];
    
    NSTimeInterval timeDifference = [Date2 timeIntervalSinceDate:Date1];
    
    double minutes = timeDifference / 60;
    
    double hours = minutes / 60;
    
    double seconds = timeDifference;
    
    double days = minutes / 1440;
    
    if (seconds < 60) {
        strAnswer = [NSString stringWithFormat:@"%.0f Seconds ago.",seconds];
    }
    else if (minutes < 60){
        strAnswer = [NSString stringWithFormat:@"%.0f Minutes ago.",minutes];
    }
    else if (hours < 24){
        strAnswer = [NSString stringWithFormat:@"%.0f Hours ago.",hours];
    }
    else{
        strAnswer = [NSString stringWithFormat:@"%.0f Days ago.",days];
    }
    return strAnswer;
}

And also this bellow method is for convert NSDate to NSString format

-(NSString *)StringFromDate:(NSDate *)DateLocal{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormat stringFromDate:DateLocal];
    NSLog(@"Date is HERE  =====>> %@",dateString);
    return dateString;
}

Monday, 6 January 2014

How to Get Data , insert data and Delete data in SQLite

-(void)GetDataFromDB{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"RideRecord.sqlite"];/// Table Name
    
    NSMutableArray *arrayTemp = [[NSMutableArray alloc]init];
    
    sqlite3 *database2;
    
    if (sqlite3_open([dbPath UTF8String], &database2) == SQLITE_OK) {
        
        const char *sqlStatement3 = "select * from NewRideTable";//Table Name
        
        sqlite3_stmt *compileStatement;
        
        if (sqlite3_prepare_v2(database2, sqlStatement3, -1, &compileStatement, NULL) == SQLITE_OK) {
            
            
            while (sqlite3_step(compileStatement) == SQLITE_ROW) {
                
                NSLog(@"one record");
                
                NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,0)] forKey:@"TimeStamp"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,1)] forKey:@"Speed"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,2)] forKey:@"Distance"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,3)] forKey:@"LatitudeDegrees"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,4)] forKey:@"LongitudeDegrees"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,5)] forKey:@"Altitude"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,6)] forKey:@"Temperature"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,7)] forKey:@"HeartRate"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,8)] forKey:@"Cadence"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,9)] forKey:@"Powers"];
                [dict setObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(compileStatement,10)] forKey:@"PointOrder"];

                [arrayTemp addObject:dict];
            }

            [dictRides setObject:arrayTemp forKey:@"Ridedata"];
            [dictRides retain];
            
            NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictRides
                                                               options:0
                                                                 error:nil];
            
            if (!jsonData) {
                NSLog(@"Error");
            } else {
                
                strRidesJson = [[NSString alloc] initWithBytes:[jsonData  bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];
                [strRidesJson retain];
                NSLog(@"\n\n JSON String ==>> %@",strRidesJson);
                
            }
        }
    } else {

        NSLog(@"error in database");
    }
    sqlite3_close(database2);
}
-(void)InsertRideData{
    NSDate *date = [[NSDate alloc]init];
    strTimeStamp = [[NSString stringWithFormat:@"%@",date]retain];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"RideRecord.sqlite"];
    
    sqlite3 *database2;
    
    if (sqlite3_open([dbPath UTF8String], &database2) == SQLITE_OK) {
        static sqlite3_stmt *compiledStatement = nil;
//        NSString *insertSQL = @"INSERT INTO NewRideTable (TimeStamp,Speed,Distance,LatitudeDegrees,LongitudeDegrees,Altitude,Temperature,HeartRate,Cadence,Powers,PointOrder) VALUES ('2013-11-20 13:27:52 +0000', '5.169240','20','22.996843','72.498082','72.918686','8','67','20','200','72.498082','1')";
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO NewRideTable (TimeStamp,Speed,Distance,LatitudeDegrees,LongitudeDegrees,Altitude,Temperature,HeartRate,Cadence,Powers,PointOrder) VALUES ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%d')",strTimeStamp,strSpeed,strDistance,strLat,strLong,strAltitude,strTemperture,strHeartrateVal,lblCadenceVal.text,lblPowerVal.text,PointOrder];
        [insertSQL retain];
        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(database2,insert_stmt, -1, &compiledStatement, NULL);
//        if (sqlite3_prepare_v2(database2, insert_stmt, -1, &compiledStatement, NULL) == SQLITE_OK) {
        if(sqlite3_step(compiledStatement)==SQLITE_DONE)
        {
            NSLog(@"Working");
        }
        sqlite3_finalize(compiledStatement);
    }
    sqlite3_close(database2);
    PointOrder++;
}
-(void) DeleteNewRideRecords {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"RideRecord.sqlite"];
    
    sqlite3 *database2;
    
    if (sqlite3_open([dbPath UTF8String], &database2) == SQLITE_OK) {
        NSString *query = @"delete from NewRideTable";
        const char *sqlStatement = [query UTF8String];
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database2, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                NSLog(@"result is here");
            }
            
            // Release the compiled statement from memory
            sqlite3_finalize(compiledStatement);
        }
    }
    PointOrder = 0;
    sqlite3_close(database2);

//    [self GetDataFromDB];

}