Browse Category: iOS

How To Update ProgressBar in iPhone

//———————
.h File
{
NSTimer *timer;
UIProgressView *myProgressBar;
int StartingValue;
}
@property (nonatomic, retain) UIProgressView *myProgressBar;

//————
.m File
@synthesize myProgressBar;

– (void)viewDidLoad
{
StartingValue = 0;

myProgressBar = [[UIProgressView alloc] init];
myProgressBar.frame = CGRectMake(25, 100, 235, 10);
myProgressBar.progress = 0.0f;
[self.view addSubview:myProgressBar];

timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
}
– (void) updateProgressBar
{
StartingValue ++;

float result =((float) StartingValue/(float)100)*100.0;
myProgressBar.progress = result/100.0f;
}

How to add an icon to your iPhone application?

In this post we will learn how to add icon file to iphone application.

First you must have icon file 57 x 57 pixels in png format.

Once your icon is ready in 57 x 57 pixels and named Icon.png, right click the resources folder and add the Icon.png to resources and do not forget to check the  ‘Copy items into Destination’s folder’ box. Then in Resources folder , find “YourAppName-Info.plist” and open it.

Edit the “Icon file” field and put the name of your icon in it i. e. Icon.png as shown in screenshot.

Now, you can run the application and can see the icon added by you for the application……

How to add MPMoviePlayerController in View

First Add Framework for MPMoviePlayerController : “MediaPlayer.Framework

.h File
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

@class MPMoviePlayerController;

@interface TestViewController : UIViewController
{
MPMoviePlayerController *moviePlayer;
}
—————————————————————————-
.m File
-(void)ViewDidLoad
{
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL      fileURLWithPath:[[NSBundle mainBundle]     pathForResource:@”Movie-1″      ofType:@”mp4″]]];

moviePlayer.view.frame = CGRectMake(0, 0, 320, 480);
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[moviePlayer setFullscreen:YES animated:YES];
moviePlayer.shouldAutoplay = YES;

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
}

-(void) moviePlayBackDidFinish:(NSNotification*)notification
{
[self dismissModalViewControllerAnimated:NO];
}

How to get Latitude,Longitude of two cities and Calculate Distance in iphone

import Framework
CoreLocation.framework
MapKit.framework

 
//————————————————————————
#import “ViewController.h”
#import <MapKit/MapKit.h>
 
NSMutableArray *citynamearray;
NSMutableArray *latarray,*longarray;
 

//————————————————————————
#import “ViewController.m”
– (void)viewDidLoad
{
[super viewDidLoad];
citynamearray = [[NSMutableArray alloc]init];
 
latarray = [[NSMutableArray alloc]init];
longarray =[[NSMutableArray alloc]init];
 
[citynamearray addObject:CityName1];
[citynamearray addObject:CityName2];
 
for (int i = 0; i < [citynamearray count]; i++)
{
NSString *urlpath = [NSString stringWithFormat:@”http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true”,[citynamearray objectAtIndex:i]];
 
NSURL *path = [NSURL URLWithString:urlpath];
NSData* data = [NSData dataWithContentsOfURL:path];
[self performSelectorOnMainThread:@selector(fetchedLatLong:) withObject:data waitUntilDone:YES];
}
 
[self CalculateDistance :[latarray objectAtIndex:0]  :[longarray objectAtIndex:0]  :[latarray objectAtIndex:1] :[longarray objectAtIndex:1]];
}
 
– (void)fetchedLatLong:(NSData *)responseData
{
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
 
NSArray *items = [json objectForKey:@”results”];
 
NSArray *items1 = [items valueForKey:@”geometry”];
NSArray *items2 = [items1 valueForKey:@”location”];
 
NSString *latlongstr = [NSString stringWithFormat:@”%@”,[items2 objectAtIndex:0]];
latlongstr = [latlongstr stringByReplacingOccurrencesOfString:@” ” withString:@””];
latlongstr = [latlongstr stringByReplacingOccurrencesOfString:@”n” withString:@””];
latlongstr = [latlongstr stringByReplacingOccurrencesOfString:@”}” withString:@””];
 
NSArray *latlngarr = [latlongstr componentsSeparatedByString:@”;”];
NSString *lat = [NSString stringWithFormat:@”%@”,[latlngarr objectAtIndex:0]];
lat = [lat stringByReplacingOccurrencesOfString:@”{lat=”” withString:@””];
lat = [lat stringByReplacingOccurrencesOfString:@””” withString:@””];
 
NSString *lng = [NSString stringWithFormat:@”%@”,[latlngarr objectAtIndex:1]];
lng = [lng stringByReplacingOccurrencesOfString:@”lng=”” withString:@””];
lng = [lng stringByReplacingOccurrencesOfString:@””” withString:@””];
 
[latarray addObject:lat];
[longarray addObject:lng];
 
NSLog(@”Lat %@ Long%@=”,latarray,longarray);
}
 
– (void) CalculateDistance : (NSString *)a : (NSString *)b :(NSString *)c :(NSString *)d
{
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:[a floatValue] longitude:[b floatValue]];
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:[c floatValue] longitude:[d floatValue]];
 
NSString *Distance = [NSString stringWithFormat:@”Distance :%f meters”,[location1 distanceFromLocation:location2] * 0.001];
NSLog(@”Distance =%@”,Distance);
 
[location1 release];
[location2 release];
}

How to manage application when orientation changes?

The problem is that when you rotate the device then

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

Method gets called but when you go to the next screen then this method does not get called. if you rotate your device again then that method gets called.

So if you want to change next screen orientation as device current orientation, then check the device’s current orientation with in viewDidLoad() method or ViewWillApper() method. Then according to that set your current view.

For finding touch location on screen in cocos2d (ios)

Try This code and get touch location in cocos2d (ios)


 - (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
         {
             UITouch *myTouch = [touches anyObject];
             CGPoint location = [myTouch locationInView:[myTouch view]];
             location = [[CCDirector sharedDirector] convertToGL:location];
         }

How to Create UILocalNotification, repeating every minute ?

There are two types of notifications in iOS, One is Local notification & other is PUsh notification. Added code to create Local notification.

// creating notification
NSDate *newDate = [NSDate dateWithTimeIntervalSinceNow:60];

// where 60 = seconds, so add your seconds here
Class cls = NSClassFromString(@”UILocalNotification”);
if (cls != nil) {
UILocalNotification *notification = [[cls alloc] init];
notification.fireDate = newDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = @“My Notification”;
notification.alertAction = @”Show me”;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
notification.repeatInterval = NSMinuteCalendarUnit;
NSString *str = @“String to pass”;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:str forKey:@“myNotification”];
notification.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
}

// receiving notification
– (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@”Notification: %@”, [notification.userInfo objectForKey:@“myNotification”]);
}


Need more help?

Hi there, was your problem or query resolved? If not & need more assistance, please do reach out to us at info@nanostuffs.com, we'll be more than delighted to help. Nanostuffs has 7+ years of extensive Salesforce & iOS/Android experience.
Holler Box