Core Loation not outputing current location - xcode

I've been working on a app that displays the current location of the user in a UILabel using the core location framework in Xcode When a button is clicked the app calls the CLLocationManager that gets the users latitude and longitude. I also included reverse geocoding to present the coordinates in a human readable form. I have this code here.
Header file
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
#property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
#property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
#property (strong, nonatomic) IBOutlet UILabel *addressLabel;
- (IBAction)getCurrentLocation:(id)sender;
#end
Implementation file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController {
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
_longitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
_latitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
#end
I got this output
Message from debugger: Terminated due to signal 15

Related

showsUserLocation action attached to button not working

I am having problems with a map view in my app. I have created a button that when clicked should show users location on the map, but nothing happens (no error messages occur).
I believe the issue may lie in the way I've written the delegates. The code from the relevant .h and .m files is below:
mapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#interface mapViewController : UIViewController {
MKMapView *mapview;
}
#property (nonatomic, retain) IBOutlet MKMapView *mapview;
-(IBAction)setMap:(id)sender;
-(IBAction)getCurrentLocation:(id)sender;
#property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
#end
mapViewController.m
#import "mapViewController.h"
#interface mapViewController ()
#end
#implementation mapViewController {
CLLocationManager *locationManager;
}
#synthesize mapview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.requestWhenInUseAuthorization;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setMap:(id)sender {
switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
case 0:
mapview.mapType = MKMapTypeStandard;
break;
case 1:
mapview.mapType = MKMapTypeSatellite;
break;
case 2:
mapview.mapType = MKMapTypeHybrid;
break;
default:
break;
}
}
-(IBAction)getCurrentLocation:(id)sender {
mapview.showsUserLocation = YES;
}
#end
Any help would be greatly appreciated, thanks
You have to implement the MapKit delegates. (Make sure you add the delegate signature in .h of your view controller)
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.005;
span.longitudeDelta = 0.005;
CLLocationCoordinate2D location;
location.latitude = userLocation.coordinate.latitude;
location.longitude = userLocation.coordinate.longitude;
region.span = span;
region.center = location;
[mapView setRegion:region animated:YES];
}
Extra: To handle App is on foreground or not:
We add 2 event observers to observe the App is entering background / returning active:
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appToBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)appToBackground{
[mapview setShowsUserLocation:NO];
}
- (void)appReturnsActive{
[mapview setShowsUserLocation:YES];
}

Compilation fails due to prototype cell

I have a TableViewController subclass with one prototype cell designed in storyboard. Then I am passing the references to the class header etc and for some reason it fails to compile it. But the connections seem fine. I provide you the code and pictures of the build errors and the storyboard. I am using Xcode 4.3.3. Any help is really appreciated.
favTable.h
#import <UIKit/UIKit.h>
#interface favTable : UITableViewController <NSFetchedResultsControllerDelegate>
{
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
NSArray *favArr;
NSMutableArray *favName;
NSMutableArray *favScore;
}
#property (nonatomic, retain) NSArray *favArr;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, strong) NSMutableArray *favName;
#property (nonatomic, strong) NSMutableArray *favScore;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (strong, nonatomic) IBOutlet UITableViewCell *celldes;
#property (strong, nonatomic) IBOutlet UIImageView *cellimage;
#property (strong, nonatomic) IBOutlet UILabel *cellname;
#property (strong, nonatomic) IBOutlet UILabel *cellmanu;
#property (strong, nonatomic) IBOutlet UILabel *cellscore;
#end
favTable.m
#import "favTable.h"
#import "ecoAppDelegate.h"
#interface favTable ()
#end
#implementation favTable
#synthesize favArr;
#synthesize managedObjectContext;
#synthesize fetchedResultsController;
#synthesize favName;
#synthesize favScore;
#synthesize celldes;
#synthesize cellimage;
#synthesize cellname;
#synthesize cellmanu;
#synthesize cellscore;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Favorites";
self.navigationController.navigationBar.translucent = NO;
// passing the array of addedtofavorites to the total one with all favorites
self.managedObjectContext = ((ecoAppDelegate *) [UIApplication sharedApplication].delegate).managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:#"FavoritesInfo" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
fetchRequest.resultType = NSDictionaryResultType;
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:#"name", nil]];
NSError *error=nil;
self.favArr=[[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]mutableCopy];
if (error!=nil) {
NSLog(#" fetchError=%#,details=%#",error,error.userInfo);
}
self.favName = [[self.favArr valueForKey:#"name"] mutableCopy];
self.favScore = [[self.favArr valueForKey:#"score"] mutableCopy];
}
- (void)viewDidUnload
{
[self setCelldes:nil];
[self setCellimage:nil];
[self setCellname:nil];
[self setCellmanu:nil];
[self setCellscore:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [favName count];;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
// Configure the cell...
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cellname.text = #"Test";
return cell;
}
#end
I moved manually the IB outlets into a separate custom cell class and then linked the references to the storyboard (maybe this is fixed in newer versions)
Then applied that cell style in the prototype cell and simply changed the content of the UI items of my cell using the identifier (no tag was needed)

How take a picture from camera of iPhone - iOS 7

my program always an error of threads.
error that appears at compile time
http://imgur.com/6rGkwKd
1. My .h
import
#interface PerfilViewController : UIViewController
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *fotoPerfil;
#property (weak, nonatomic) IBOutlet UIButton *botaoSalvarFoto;
#property (nonatomic) UIImagePickerController *pegarFoto;
-(IBAction)salvarFoto:(UIButton *)sender;
#end
2. My .m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_pegarFoto = [[UIImagePickerController alloc]init];
_pegarFoto.delegate = self;
_pegarFoto.sourceType = UIImagePickerControllerSourceTypeCamera;
}
-(IBAction)salvarFoto:(UIButton *)sender {
[self presentViewController:_pegarFoto animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
_fotoPerfil.image = img;
[[picker parentViewController] dismissViewControllerAnimated:YES completion:nil];
}

Save UIImageView When Unload A View - Xcode 4.5.1

Creating an app on Xcode 4.5.1 and wondering how do i save the current image in the specified UIImageView when i navigate to a different view. Then when i return to the View it loads up the saved image into the UIImageView?
Thank a lot, appreciate it :D
The code I've posted below involves passing a uiimage to a second view controller. It also includes a button on each controller to move back and forth between the two. The saved image will stay in the uiimageview when you switch to and from the views.
SecondView.h
#import <UIKit/UIKit.h>
#interface SecondView : UIViewController {
IBOutlet UIImageView *yourphotoview;
UIImage *yourimage;
}
#property (retain, nonatomic) UIImage *yourimage;
- (IBAction)back;
#end
SecondView.m
#import "ViewController.h"
#import "SecondView.h"
#interface SecondView ()
#end
#implementation SecondView
#synthesize yourimage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
yourphotoview.image = image;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)back {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)dealloc {
[image release];
[super dealloc];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
#interface ViewController : UIViewController
{
IBOutlet UISlider *compression;
IBOutlet UISwitch *smurferderp;
SecondView *SecondViewdata;
}
#property (retain, nonatomic) SecondView *SecondViewdata;
#property (retain, nonatomic) IBOutlet UIImageView *theimage;
- (IBAction)switchview:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "SecondView.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize SecondViewdata, theimage;
- (IBAction)switchview:(id)sender {
SecondView *secondview = [self.storyboard instantiateViewControllerWithIdentifier:#"rr"];
self.SecondViewdata = secondview;
SecondViewdata.yourimage = theimage.image;
[self presentViewController: secondview animated:YES completion:NULL];
}
- (void)dealloc {
[theimage release];
[super dealloc];
}
#end

How are dependent keys registered? (Key-Value Observing)

I have created a simple Cocoa Application. In MainMenu.xib I have added an NSDatePicker and an NSTextField. Both of these objects have Value bindings to properties of the App Delegate. I expect that when the user changes the date in the NSDatePicker, the NSTextField will be updated. This isn't happening. Here is the App Delegate:
// AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, strong) NSDate *dateFromPicker;
#property (nonatomic, readonly) NSString *dateString;
#end
// AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
- (NSString *)dateString
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
return [formatter stringFromDate:self.dateFromPicker];
}
+ (NSSet *)keyPathsForValuesAffectingDateString
{
return [NSSet setWithObject:#"dateFromPicker"];
}
#end
Updated code with an observer for dateFromPicker and some NSLog statements:
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self addObserver:self forKeyPath:#"dateFromPicker" options:0 context:NULL];
self.dateFromPicker = [NSDate dateWithNaturalLanguageString:#"12/12/12"];
}
- (NSString *)dateString
{
NSLog(#"dateString was called.");
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
return [formatter stringFromDate:self.dateFromPicker];
}
+ (NSSet *)keyPathsForValuesAffectingDateString
{
NSLog(#"keyPathsForValuesAffectingDateString was called.");
return [NSSet setWithObject:#"dateFromPicker"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change: (NSDictionary *)change context:(void *)context
{
NSLog(#"dateFromPicker changed.");
}
#end
Here is the log:
2012-08-10 15:37:15.086 ... keyPathsForValuesAffectingDateString was called.
2012-08-10 15:37:15.087 ... dateString was called.
2012-08-10 15:37:15.116 ... dateFromPicker changed.
2012-08-10 15:37:15.117 ... dateString was called.
2012-08-10 15:37:19.831 ... dateFromPicker changed.
2012-08-10 15:37:19.831 ... dateString was called.
I know this is not exactly what you were asking, but ...
Why don't you bind the NSTextField to the NSDate property and add a formatter to it in Interface Builder?

Resources