Wednesday, 22 February 2012

Showing You Tube Video in a view.


//in .h , add 
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame;




- (void)viewDidLoad
{
    [super viewDidLoad];
    [self embedYouTube:@"http://www.youtube.com/watch?v=fp14M7yQV-0" frame:CGRectMake(0, 44, 320, 200)];
    
}

- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
    NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";
    NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
    UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
   // videoView.center = CGPointMake(200, 100 );
    
    [videoView loadHTMLString:html baseURL:nil];
    [self.view addSubview:videoView];
    [videoView release];
}


Tuesday, 21 February 2012

Adding data to a sever with the data and the URL seperately in the POST method.

NSString *post =[[NSString alloc] initWithFormat:@"account_type=%@&user_id=%@&user_name=%@&comment_text=%@&post_id=%@",type,[acct username],[acct accountDescription],item.text,commentID];
                       
                        NSURL *url=[NSURL URLWithString:@"http://iphone.jiujitsucoach.com/index.php?action=newcomment"];
                       
                        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
                       
                        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
                       
                        NSMutableURLRequest *request1 = [[[NSMutableURLRequest alloc] init] autorelease];
                        [request1 setURL:url];
                        [request1 setHTTPMethod:@"POST"];
                        [request1 setValue:postLength forHTTPHeaderField:@"Content-Length"];
                        [request1 setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                        [request1 setHTTPBody:postData];
                       
                       
                        [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
                       
                        NSError *error;
                        NSURLResponse *response;
                        NSData *urlData=[NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
                       
                        NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                        NSLog(@"here...%@",data);

Adding Activity indicator programatically as sub view

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
   
    self.view.userInteractionEnabled=NO;
     UIActivityIndicatorView* indicator = [[UIActivityIndicatorView alloc] init];
    indicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhiteLarge;
    //indicator.color=  [UIColor  colorWithRed:0.655 green:0.729 blue:0.812 alpha:1.0];
    indicator.color=  [UIColor whiteColor];
    [indicator setFrame:CGRectMake(150, 170, 20, 20)];
    [self.view addSubview:indicator];
    [indicator startAnimating];
   
}

//Where the indicator needs to stop

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[indicator stopAnimating];

Direction from one location to another in Maps application

-(IBAction)directionButtonpress:(id)sender{

        NSString *destnlat=@"40.984042";
        NSString *destlong=@"-74.009993";
       
       // NSString *currentlat=[[NSUserDefaults standardUserDefaults]objectForKey:@"latitude"];
       // NSString *currentlong=[[NSUserDefaults standardUserDefaults]objectForKey:@"longitude"];
       
        NSString *currentlat=@"40.982435";
        NSString *currentlong=@"-74.016427";
       
        NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%@,%@&daddr=%@,%@",currentlat,currentlong,destnlat,destlong];
       
        NSLog(@"%@",url);
       
        [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

}

User Current Location Coordinates-iPhone

Add coreLocation frame work and then import
#import <CoreLocation/CoreLocation.h>     in the view the location cordinates is required.

//.h file

@interface HomeView : UIViewController<CLLocationManagerDelegate>{
  CLLocationManager *m_locationManager;
}

//.m file

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   // [self.navigationController.navigationBar setAlpha:0.0];
   
    m_locationManager = [[CLLocationManager alloc] init];
    m_locationManager.delegate = self;
    m_locationManager.distanceFilter = kCLDistanceFilterNone;
    m_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [m_locationManager startUpdatingLocation];
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{   
    [manager stopUpdatingLocation];
    double lati = newLocation.coordinate.latitude+0.0;
    double longi = newLocation.coordinate.longitude+0.0;   
   
    NSNumber *lat = [NSNumber numberWithDouble:lati];
    NSNumber *lg = [NSNumber numberWithDouble:longi];
    NSLog(@"Current location-- %@-%@",lat,lg);
   
    [[NSUserDefaults standardUserDefaults] setObject:lat forKey:@"latitude"];
    [[NSUserDefaults standardUserDefaults] setObject:lg forKey:@"longitude"];
   
  }

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
   
    [manager stopUpdatingLocation];
       
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Device not Supported!" message:@""
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
   
    if(error != NULL)
        return;
}