Monday, October 10, 2011

iPhone Development (Object C) Tip

  1. How to create (access) AppDelegate object

                 projectNameAppDelegate* sharedApplication = (projectNameAppDelegate *) 
                                                                                                         [[UIApplication sharedApplication] delegate];



                 Using this Object you can access main Window also.
      
         2.   Scheduling to method  


                [NSTimer scheduledTimerWithTimeInterval:1.0f target:self
                                       selector:@selector(didFinishMainURL) userInfo:nil repeats:NO]; 
     
     

          3. Re-create frame parameter from existing frame

               
                UIImageView *imageView = [productRowData objectForKey:@"product_image"];
                CGRect rect = imageView.frame;
                rect.size.height = 80;
                rect.size.width = 75;
                imageView.frame = rect;
                imageView.tag = 0;    // tag our images for later use when we place them in serial fashion
                CGRect frame = imageView.frame;
                //frame.origin = CGPointMake(15, 3);
                frame.origin = CGPointMake(0, 0);
                imageView.frame = frame;

    Get Location Data (GPS) in iPhone

    Using this post you can learn about 
    • How to create shared object in Obective C
    • How to create custom 'init' method
    • How to get GPS location using iPhone 

    //Reciving data for main screen
        POLocationService* locationService = [POLocationService sharedLocationService];
        locationService.delegate = self;
        [locationService requestLocationInformationFor:@"for finding deals near to you!"];*/


     POLocationService.h   file


    #import "Foundation/Foundation.h"
    #import "CoreLocation/CoreLocation.h"

    @interface POLocationService : NSObject "CLLocationManagerDelegate"{
        CLLocationManager *locationManager_;
        id delegate_;
        NSString* locationString_;
    }

    @property (nonatomic, retain) CLLocationManager *locationManager;
    @property (nonatomic, assign) id delegate;
    @property (nonatomic, retain) id locationString;

    + (POLocationService *)sharedLocationService;
    - (void)requestLocationInformationFor:(NSString *)purpose;
     
    @end



     POLocationService.m   file


    #import "POLocationService.h"

    static POLocationService *sharedLocationService = nil;


    @implementation POLocationService

    @synthesize locationManager = locationManager_;
    @synthesize delegate = delegate_;
    @synthesize locationString = locationString_;

    + (POLocationService *)sharedLocationService
    {
        @synchronized(self) {
            if (sharedLocationService == nil) {
                sharedLocationService = [[self alloc] init];
            }
        }
        return sharedLocationService;
    }

    - (id)init
    {
        if (self = [super init]) {
            self.locationManager = [[CLLocationManager alloc] init];
        }
       
        return self;
    }

    - (void)requestLocationInformationFor:(NSString *)purpose
    {
        //didRequest_ = YES;
        locationManager_.delegate = self;
        locationManager_.purpose = purpose;
        locationManager_.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        locationManager_.distanceFilter = 200;
        [locationManager_ startUpdatingLocation];
    }

    /*
     * We want to get and store a location measurement that meets the desired accuracy. For this example, we are
     *      going to use horizontal accuracy as the deciding factor. In other cases, you may wish to use vertical
     *      accuracy, or both together.
     */
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
        NSString *location = [NSString stringWithFormat:@"%lf;%lf",newLocation.coordinate.latitude,newLocation.coordinate.longitude];

        self.locationString = location;
       
        [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:location withObject:[NSNumber numberWithInt:0]]; // Third arg is whether user denied
    }

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
        // The location "unknown" error simply means the manager is currently unable to get the location.
        if ([error code] != kCLErrorLocationUnknown) {
            [locationManager_ stopUpdatingLocation];
            locationManager_.delegate = nil;
            [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
        } else if ([error code] != kCLErrorDenied || [error code] != kCLErrorRegionMonitoringDenied) {
            [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
        } else {
            [delegate_ performSelector:@selector(didFindLocation:userDenied:) withObject:nil withObject:[NSNumber numberWithInt:0]];
        }

    }

    - (void)dealloc
    {
        [locationManager_ stopUpdatingLocation];
        locationManager_.delegate = nil;
        [locationManager_ release];
        [super dealloc];
    }
    @end

    Saturday, October 1, 2011

    How to create shared object in Obective C


    static AASharedObjectClassName *sharedObjectName = nil;


    @implementation AASharedObjectClassName

    + (AASharedObjectClassName *)sharedObjectService
    {
        @synchronized(self) {
            if (sharedObjectName == nil) {
                sharedObjectName = [[self alloc] init];
            }
        }
        return sharedObjectName;
    }

    Friday, September 30, 2011

    How to create custom 'init' method in Objective C


    - (id)init
    {
        if (self = [super init]) {
           
             //you can implement custom class here

        }
       
        return self;
    }

    Tuesday, September 27, 2011

    Radio buttons for iPhone application


    Add buttons on view of controller


            for (int i = 0; i < 3; i++) {
                UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
                [but setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal];
                [but setImage:[UIImage imageNamed:@"checkedbox.png"] forState:UIControlStateSelected];
                [but setFrame:CGRectMake(0, 0, 25, 25)];
                [but setCenter:CGPointMake(410, 385+i*40)];
                if (i == 0) {
                    [but setSelected:YES];
                }
                [but setTag:i];
                [but addTarget:self action:@selector(checkboxButton:) 
                                                              forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:but];
            }


    Action for buttons:


    - (void)checkboxButton:(UIButton *)button
    {
        for (UIButton *but in [self subviews]) { // This is the view that button are in
            if ([but isKindOfClass:[UIButton class]] && ![but isEqual:button]) {
                [but setSelected:NO];
            }
        }
        if (!button.selected) {
            button.selected = !button.selected;
        }
    }