I have associated my app with a UTI so that users can launch KML attachments. In the iPad app delegate of my universal app I can see the launchOptions and from these I get an NSURL for the file being launched. I want to store this as a global so that I can access it from elsewhere in my app, I am doing this using a singleton called Engine. This is my App Delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Engine *myEngine=[Engine sharedInstance];
StormTrackIpad *newVC=[[StormTrackIpad alloc] initWithNibName:#"StormTrackIpad" bundle:nil];
[window addSubview:newVC.view];
NSURL *launchFileURL=(NSURL *)[launchOptions valueForKey:#"UIApplicationLaunchOptionsURLKey"];
myEngine.launchFile=launchFileURL;
/* Show details of launched file
NSString *message =launchFileURL.absoluteString;
NSString *title = [NSString stringWithFormat:#"Opening file"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
*/
[window makeKeyAndVisible];
return YES;
}
My Engine class looks like this:
// Engine.h
#import <Foundation/Foundation.h>
#interface Engine : NSObject {
NSURL *launchFile;
}
+ (Engine *) sharedInstance;
#property (nonatomic, retain) NSURL *launchFile;
#end
// Engine.m
#import "Engine.h"
#implementation Engine
#synthesize launchFile;
static Engine *_sharedInstance;
- (id) init
{
if (self = [super init])
{
// custom initialization
}
return self;
}
+ (Engine *) sharedInstance
{
if (!_sharedInstance)
{
_sharedInstance = [[Engine alloc] init];
}
return _sharedInstance;
}
#end
My problem is that when I try to access the launchFile variable from the Engine elsewhere in my app (from a View Controller) the debugger shows the value of Engine.launchFile to be . I am accessing the variable like this:
- (void)viewDidLoad {
[super viewDidLoad];
Engine *myEngine=[Engine sharedInstance];
NSURL *launchFile=myEngine.launchFile;
NSString *title = [NSString stringWithFormat:#"Opening file"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:launchFile.absoluteString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
Any help?
Your code looks OK at first glance - can you set a breakpoint on myEngine.launchFile = just to see what myEngine is pointing at? This should make sure that your singleton code is working.
Also, have you checked that [launchOptions valueForKey:#"UIApplicationLaunchOptionsURLKey"] definitely returns an object? Did you mean to type this :
[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
Sam
P You should read the answer to this question on creating singleton objects, there's a few overrides that you should put into your Engine class ;)
Related
I am currently developing an iPhone app that makes use of a UIImagePickerController with a custom overlay to take photos.
Unfortunately I do not have direct access to an iPhone 4S but several testers have reported that the camera picker is drawing a green border around faces exactly like this: http://cdn.iphonehacks.com/wp-content/uploads/2012/03/camera_faces.jpg
Due to the nature of this app this is not desirable.
A thorough search of the UIImagePickerController docs didn't turn up anything and similarly everything I could find on here relating to face detection was providing instructions in how to use a CIDetector or similar.
How can I disable face detection in my UIImagePickerController?
Here is my initialisation code for the UIImagePickerController:
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
[cameraPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[cameraPicker setCameraDevice:UIImagePickerControllerCameraDeviceRear];
if ([UIImagePickerController isFlashAvailableForCameraDevice:cameraPicker.cameraDevice]){
[cameraPicker setCameraFlashMode:UIImagePickerControllerCameraFlashModeOn];
}
[cameraPicker setShowsCameraControls:NO];
[cameraPicker setCameraOverlayView:cameraOverlayView];
cameraPicker.delegate = self;
[self presentModalViewController:cameraPicker animated:YES];
Try This -->
Lets Say We have one UIViewController named as - RecordVideoViewController
Implementation of -- RecordVideoViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/UTCoreTypes.h>
#import <AssetsLibrary/AssetsLibrary.h>
#interface RecordVideoViewController : UIViewController
- (IBAction)recordAndPlay:(id)sender;
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controllerusingDelegate:
(id)delegate;
-(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error
contextInfo(void*)contextInfo;
#end
Implementation of -- RecordVideoViewController.m
- (IBAction)recordAndPlay:(id)sender {
[self startCameraControllerFromViewController:self usingDelegate:self];
}
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller
usingDelegate:(id )delegate
{
// 1 - Validattions
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ==
NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
// 2 - Get image picker
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose movie capture
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
// 3 - Display image picker
[controller presentViewController:cameraUI animated:YES completion:nil];
return YES;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
// Handle a movie capture
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) ==
kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(moviePath,
self,#selector(video:didFinishSavingWithError:contextInfo:),nil);
}
}
}
-(void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:
(void*)contextInfo {
if (error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Video Saving
Failed"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Video Saved" message:#"Saved To
Photo Album" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
Implement This code it, i hope this will help you .
Check out this post. There are some hints here, but still can't find much info outside of Apple's docs on this API.
Proper usage of CIDetectorTracking
This piece of code which is supposed to show an alert window with a text input:
self.alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];
Causes this error:
Thread 7: Program received signal: "EXC_BAD_ACCESS"
This is how self.alert is defined:
#interface MyClass : NSObject
{
UIAlertView *alert;
id <MyClassDelegate> __unsafe_unretained delegate;
}
#property (nonatomic, retain) UIAlertView *alert;
#property (unsafe_unretained) id <MyClassDelegate> delegate;
The problem is maybe because of the customize.
I do not know why, but appear to me that the problem is because of the use of threads + customize of your alert.
Can you try to show this alert on the main thread? What happen?
You probably get an error in this line:
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
What you need to do if yes, is perform this in the main thread.
- (void) yourMethod{
[self performSelectorOnMainThread:#selector(yourMethod2) withObject:nil waitUntilDone:NO];
}
- (void) yourMethod2{
self.alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];
}
Sorry to can't help you more than that, but I do not know exactly what happen, but I already read about issues when editing things to show, in other threads.
Hope it help you!
The EXC_BAD_ACCESS is caused by accessing a released object. To avoid this make your call to UIAlertView kind of modal:
Function body:
-(void)checkSaving
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Do you want to add these results to your database?"
message:#"\n\n"
delegate:self
cancelButtonTitle:#"No"
otherButtonTitles:#"Save", nil];
alert.alertViewStyle = UIAlertViewStyleDefault;
[alert show];
//this prevent the ARC to clean up :
NSRunLoop *rl = [NSRunLoop currentRunLoop];
NSDate *d;
d= (NSDate*)[d init];
while ([alert isVisible])
{
[rl runUntilDate:d];
}
}
Your choice result:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if (buttonIndex == 1)//Save
{
//do something
}
if (buttonIndex == 0)//NO
{
//do something
}
}
Register the functions in the interface declaration:
#interface yourViewController ()
-(void)checkSaving
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
#end
To call:
[self checkSaving];
I wish this will help you.
I just started coding in Objective C, and am making an app that will track the users location, and send an alert with the latlng in it. This code isn't complete in the slightest, but I ran into a problem with trying to use the variable I created "lat" in the "viewDidLoad" for the alert. I declared the variable in the CLLocationManager delegate/method(I don't really know what it's called) and I don't know how to use it in other places.
- (void)viewDidLoad
{
locationManager =[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = 100.0f;
[locationManager startUpdatingLocation];
UIAlertView *message = [[UIAlertView alloc] initWithTitle: #"Your current Latitude is:"
message:This is where I want to put the variable "lat"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void) locationmanager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
NSString *lat = [[NSString alloc] initWithFormat: #"%g",newLocation.coordinate.latitude];
NSString *lng = [[NSString alloc] initWithFormat:#"%g", newLocation.coordinate.longitude];
}
Any help would be appreciated!
Simple solution.
Your ViewController .h should look something like this
#interface ViewController : UIViewController {
NSString *lat;
NSString *lng;
}
then this becomes
-(void) locationmanager: (CLLocationManager *) manager
didUpdateToLocation: (CLLocation *) newLocation
fromLocation: (CLLocation *) oldLocation
{
lat = [[NSString alloc] initWithFormat: #"%g",newLocation.coordinate.latitude];
lng = [[NSString alloc] initWithFormat:#"%g", newLocation.coordinate.longitude];
NSLog(lat);
NSLog(lng);
}
NSlog simple out puts to the console which is locate just below where you code
remove
UIAlertView *message = [[UIAlertView alloc] initWithTitle: #"Your current Latitude is:"
message:This is where I want to put the variable "lat"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
Because that will just show an alert as soon as the view is loaded
Hope this helps
I have created an error message that should display if the user can not connect to google.com by either WI-FI or WWAN. I do not get any coding errors, so nothing seems to be wrong. I am using a UIWebview that goes to twitter.com. The issue may be that I am trying to get the error to display when the view loads and not the UIWebview, but I am not sure. Another issue that I am having is that I can not get "reachability" recognized as a name in the TwitterViewController.h file. Here is the code:
TwitterViewController.h
#import <UIKit/UIKit.h>
#import "Reachability.h"
#interface TwitterViewController : UIViewController {
Reachability *reachability;
}
#end
TwitterViewController.m
#import "TwitterViewController.h"
#import <SystemConfiguration/SystemConfiguration.h>
#include <netinet/in.h>
#import "TwitterView.h"
#implementation TwitterViewController
- (void)viewDidLoad {
[super viewDidLoad];
Reachability *r = [Reachability reachabilityWithHostName:#"http://www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
UIAlertView *NEalert = [[UIAlertView alloc] initWithTitle: #"Error"
message: #"Could not load the webpage. Please check your internet connection."
delegate: nil
cancelButtonTitle: #"Dismiss"
otherButtonTitles: nil];
[NEalert show];
[NEalert release];
}
}
#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
[super dealloc];
}
#pragma mark -
#pragma mark Initialisation
- (id)init {
self = [super init];
if (self) {
self.hidesBottomBarWhenPushed = YES;
UINavigationBar objects
self.title = #"Twitter";
UIView *twitterView = [[TwitterView alloc] initWithParentViewController:self];
self.view = twitterView;
[twitterView release];
}
return self;
}
#pragma mark -
#pragma mark Action Methods
- (void)twitterBUTTONAction {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"NFAGaming's Twitter"
message: #"To follow NFAGaming on twitter, go to: twitter.com/NFAGaming"
delegate: nil
cancelButtonTitle: nil
otherButtonTitles: #"Ok", nil];
[alert show];
[alert release];
}
#pragma mark -
#pragma mark UIViewController Delegates
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
#end
EDIT:
This is what ended up working perfectly for me
{{Reachability *r = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if (internetStatus == NotReachable) {
UIAlertView *tNEalert = [[UIAlertView alloc] initWithTitle: #"Error"
message: #"Internet Connection Required"
delegate: self
cancelButtonTitle: #"Dismiss"
otherButtonTitles: nil];
[tNEalert show];
[tNEalert release];
}
else {
UIView *twitterView = [[TwitterView alloc] initWithParentViewController:self];
self.view = twitterView;
[twitterView release];
}
}}
I used to use reachability like this:
self.reachability = [Reachability reachabilityForInternetConnection];
if ( [self.reachability currentReachabilityStatus] == NotReachable) {
UIAlertView *noConnectionAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Alert",#"Alert Title")
message:#"Internet connection is required." delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[noConnectionAlert show];
[noConnectionAlert release];
}
Use should use reachabilityWithHostName: only if you need to determine reachability to a specific host. See if this will work.
I have a problem.
This is my code.
When i try to use the IBAction SavePosition the "arrayPosition" isn't update.
Else if i initialize the "arrayPosition" in "SavePosition" the value is stored in the array.
Why this anomaly?
#import <UIKit/UIKit.h>
#interface AccelerometroViewController : UIViewController <UIAccelerometerDelegate, UITextFieldDelegate, UIAlertViewDelegate>{
//.....
NSMutableArray *arrayPosizioni;
NSMutableArray *arrayPosizioniCorrenti;
NSString *nomePosizioneCorrente;
}
-(IBAction)salvaPosizione;
//...
#property (nonatomic, assign) NSMutableArray *arrayPosizioni;
#property (nonatomic, assign) NSMutableArray *arrayPosizioniCorrenti;
#property (nonatomic, assign) NSString *nomePosizioneCorrente;
#end
#import "AccelerometroViewController.h"
#import "Position.h"
#implementation AccelerometroViewController
float actualX;
float actualY;
float actualZ;
#synthesize arrayPosition;
#synthesize arrayCurrentPosition;
#synthesize nameCurrentPosition;
-(id)init {
self = [super init];
if (self != nil) {
arrayPosition = [[NSMutableArray alloc]init];
arrayCurrentPosition = [[NSMutableArray alloc]init];
nameCurrentPosition = [NSString stringWithFormat:#"noPosition"];
actualX = 0;
actualY = 0;
actualZ = 0;
}
return self;
}
-(void)updateTextView:(NSString*)nomePosizione
{
NSString *string = [NSString stringWithFormat:#"%#", nameCurrentPosition];
textEvent.text = [textEvent.text stringByAppendingString:#"\n"];
textEvent.text = [textEvent.text stringByAppendingString:string];
}
-(IBAction)savePosition{
Posizione *newPosition;
newPosition = [[Position alloc]init];
if([newPosition setValue:(NSString*)fieldNomePosizione.text:(float)actualX:(float)actualY:(float)actualZ]){
//setValue is a method of Position. I'm sure that this method is correct
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Salvataggio Posizione" message:#"Posizione salvata con successo" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
[arrayPosition addObject:newPosition];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Salvataggio osizione" message:#"Errore nel salvataggio" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
Whats going on?
I bet you're creating your view controller inside a xib file?
If you set a breakpoint inside your init method on the line
arrayPosition = [[NSMutableArray alloc]init];
I bet it never runs. This means that when you get to the line
[arrayPosition addObject:newPosition];
arrayPosition is still nil so nothing happens.
How to fix it?
If you're initializing a UIViewController it's either called inside initWithNibName:bundle: if you've created it in code or in initWithCoder: if it's created inside a xib file.
You need to do something like this :
- (void) initialise {
arrayPosition = [[NSMutableArray alloc] init];
arrayCurrentPosition = [[NSMutableArray alloc] init];
nameCurrentPosition = #"noPosition";
actualX = 0;
actualY = 0;
actualZ = 0;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if (self = [super initWithNibName:nibName bundle:bundle]) {
[self initialise];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
[self initialise];
}
return self;
}
This will call initailise regardless of how the view controller is created.
This:
[newPosition setValue:(NSString*)fieldNomePosizione.text:(float)actualX:(float)actualY:(float)actualZ]
... is gibberish. I don't even see how it compiles. I assume its a typo. However, if the function does not return a boolean it will return a void pointer and always evaluate to false so the block is never called.
More generally, your problem is most likely that you are not using the self.propertyName notation to refer to your properties so they are not being retained. E.g.
[arrayPosition addObject:newPosition];
should be:
[self.arrayPosition addObject:newPosition];
... as should all other references to arrayPosition in the code.