Problem with my app delegate - iphone

I am trying to make an app that contains a class that is created in the app delegate.
I initialize it with:
Mobile *tmp = [[Mobile alloc] init];
mobile = tmp;
[tmp release];
and then I try to use it in other classes in my app with this:
projectAppDelegate *delegate = (projectAppDelegate *)[[UIApplication sharedApplication] delegate];
mobile = delegate.mobile;
but when I do something like:
[mobile enter:x :y];
it crashes...
Is there something I did wrong, or is there any solution for making a class that all the other classes in the app can use?
Thank you.

If you want to use instances of your object you have to store them as properties of app delegate.
//appdelegate.h
//
//...
//
#interface AppDelegate : NSObject <UIApplicationDelegate> {
Mobile *tmp;
}
//...
//appdelegate.m
//
//...
//
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
mobile = [[Mobile alloc]init];
}
//...
- (void)dealloc {
[mobile release];
[super dealloc];
//...
}
Than you have to get a pointer to your application delegate shared instance and call your mobile property.
//... Somewhere
AppDelegate* ref = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSLog(#"%#", [ref mobile]);
//...

In your first code snippet you are effectively creating and immediately destroying the object. If the object is supposed to persist after that method is done executing you should just use
mobile = [[Mobile alloc] init];

Related

Google analytics doesn't show tracking of my App

I would like to Integrate Google Analytics Tracking into my IOS APP.
I have integrated Google Analytics Library and Add It To my Application.
Here is my code snippet,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[UIApplication sharedApplication].statusBarHidden = NO;
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 1;
[GAI sharedInstance].debug=YES;
[[GAI sharedInstance] trackerWithTrackingId:#"UA-43556575-1"];
return YES;
}
Code In my FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.trackedViewName = #"Home";
....
....
}
List of Librarys which i have added, (First i have copied all this library from download folder then added to my project folder and then in Xcode i am taking reference from project folder)
GAI.h
GAITrackedViewController.h
GAITracker.h
GAITransaction.h
GAITransactionItem.h
libGoogleAnalytics.a
Almost 5 days and still stuck with same problem :(
:
Please Help ...
Thank you for reading and thanks in advance.
I think this may be one case.
Your UIViewController class must be a subclass of the GAITrackedViewController class,
#interface FirstViewController : GAITrackedViewController
And Must override these function.
-(void)viewDidLoad
{
[super viewDidLoad];
self.trackedName = #"Home";
// your code here
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// your code here
}
As viewDidLoad and viewDidAppear: are important methods.
And Remember the profile must be for mobile app.

calling a method inside someClass from AppDelegate Objective-C IOS

I'm trying to call a method that's inside someClass from the AppDelegate class.
I usually create an instance and then using that instance, call the method. Like so:
FFAppDelegate *delegate = (FFAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate someMethod];
I use that ^^ quite a bit in my code and it works perfectly. What I want to do, is switch it around. Instead of calling a method INSIDE the AppDelegate, I want to call a method inside another class FROM the AppDelegate.
SomeClass *test = (SomeClass *) [[UIApplication sharedApplication] delegate];
[test someMethod];
In this case, I keep getting "Terminating app due to uncaught exception 'NSInvalidArgumentException'" error due to "unrecognized selector sent to instance".
Any light shed on the matter would be greatly appreciated! :)
[[UIApplication sharedApplication] delegate]; return your AppDelegate class , not SomeClass
you can use like this :
FFAppDelegate *delegate = (FFAppDelegate *) [[UIApplication sharedApplication] delegate];
[delegate someMethodForSomeClass];
And then in your AppDelegate code someMethodForSomeClass like this :
- (void)someMethodForSomeClass
{
SomeClass *someClass = _yourSomeClass;
[someClass someMethod];
}
Instantiate an instance of the class that you want to send the request from and make the method public (in the .h file). Then import that class into the app delegate and call it.
Like so...
YourClass * yourClassInstance = [[YourClass alloc] init];
[yourClassInstance someMethod];
in YourClass.h below the #interface you would declare the method like so
-(void)someMethod;
So anyone could access it.
For Example if you want to Create AlertView only Once and Use it in any UiViewController
So, you can make Method For UIAlertView and Call the Method When you Want
1)In your appDelegate.h file
#property (nonatomic, strong) UIAlertView *activeAlertView;
2) In your AppDelegate.m File
-(void)openAlert
{
NSString *strMsgPurchase = #"Write your message";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Buy" message:strMsgPurchase delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Buy",#"Restore", nil];
[alert setTag:100];
[alert show];
self.activeAlertView = alert;
}
3) To Call the Method in which uiview you want
[((AppDelegate *)[[UIApplication sharedApplication] delegate])openAlert];
Note: Define -(void)openAlert Method in Appdelegate.h file

Need Help with applicationDidBecomeActive

I have been trying for days to get this code to work, but I have no idea what I am doing wrong. Everytime the app wakes up from sleep, or the user closes the app and opens it again (without closing the app from multitasking), I want a label value to change.
In my applicationDidBecomeActive, I am running a counter, which I want to display on whatever viewcontroller is open at that moment.
Code:
- (void)applicationDidBecomeActive:(UIApplication *)application {
counter = counter + 1;
W1G1 *view1 = [[[W1G1 alloc] initWithNibName:#"W1G1" bundle:nil] retain];
[view1 setlabel];
}
In my viewcontroller W1G1, I have the following code:
Code:
- (void) setlabel {
NSString *string = [NSString stringWithFormat:#"%d", counter];
vocabword.text = string;
}
I have imported W1G1 in my appdelegate, but the code does not run :( Please help!
Thanks
In the AppDelegate.m file, where you have
- (void)applicationDidBecomeActive:(UIApplication *)application {
counter = counter + 1;
W1G1 *view1 = [[[W1G1 alloc] initWithNibName:#"W1G1" bundle:nil] retain];
[view1 setlabel];
}
the variable counter being incremented is confined to the AppDelegate. In other words, your view controller doesn't know that it has been incremented.
I would suggest that you use NSUserDefaults to store the value of counter so that you can easily pass it between these view controllers. Either that, or you could allow for an input into the method setLabel, e.g.
- (void) setlabel:(int)counter {
NSString *string = [NSString stringWithFormat:#"%d", counter];
vocabword.text = string;
}
and then in the AppDelegate you'll want to do:
- (void)applicationDidBecomeActive:(UIApplication *)application {
counter = counter + 1;
W1G1 *view1 = [[[W1G1 alloc] initWithNibName:#"W1G1" bundle:nil] retain];
[view1 setlabel:counter]; // <-- now you're using counter
[self.window addSubview:view1];
}
1) When you say 'the code does not run' do you mean that? That is, if you put NSLogs in applicationDidBecomeActive: and in setLabel does it show the code is run?
2) I would suspect the code is running. But your code won't "show the counter on whatever view controller is open at that moment". Your code creates a new view (view1), but that view won't be displayed. It is not added as a subview to anything. Your code will also leak. You create a W1G1 object, but it is never released and you throw away any reference you have to it.
To achieve what you want, you could add a subview to the application's window. Depending how your app delegate is set up, something like the following should do the trick:
counter++;
W1G1 *viewController1 = [[W1G1 alloc] initWithNibName:#"W1G1" bundle:nil];
[viewController1 setlabel: counter];
[[self window] addSubview: [viewController1 view]]
// you'll want to save a reference to the viewController somehow so you can release it at a later date
Then in W1G1
- (void) setlabel: (int) counter;
{
NSString *string = [NSString stringWithFormat:#"%d", counter];
vocabword.text = string;
}
There are, of course, lots of other approaches you could take towards this problem. And you'll need some strategy for removing the W1G1 view that you are adding at some stage, otherwise you'll just get more and more views added.
Update: You ask (in comments) how to keep track of your viewController throughout lifetime of the app... One approach is to keep track of it in your appDelegate. In the header have something like:
#class W1G1;
#interface MyAppDelegate : : NSObject <UIApplicationDelegate>
{
// other decelerations
int counter;
W1G1 * _myW1G1
}
#property (nonatomic, retain) W1G1* theW1G1
In the .m file include
#synthesize theW1G1 = _myW1G1;
Probably in application:didFinishLaunchingWithOptions: create the viewController, set the property to refer to it, and add its view to the view hierarchy.
W1G1* theViewController = [[W1G! alloc] initWithNibName: #"W1G1" bundle: nil];
[[self window] addSubview: [theViewController view]];
[self setTheW1G1: theViewController];
[theViewController release];
Then when you want to access the viewController again from with the app delegate use [self theW1G1], e.g.
[[self W1G1] setlabel: counter];

Wikitude SDK remove from superview

I have implement Wikitude SDK and I manage to initialize using this code:
-(IBAction)launchAR:(id)sender{
wikitudeAR = [[WikitudeARViewController alloc] initWithDelegate:self applicationPackage:nil applicationKey:nil applicationName:nil developerName:nil];
}
- (void) verificationDidSucceed {
BuddyFinderAppDelegate *appDel = (BuddyFinderAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDel.window addSubview: [wikitudeAR start]];
[window makeKeyAndVisible];
}
How I will go back to my MainViewController View?
(I want to call it from object "custombutton")
I guess your WikitudeARViewController delegate is application delegate.
1) Implement your own View controller , from where you want to launch AR Browser (on button click , preferably).
2) Instantiate WikitudeARViewController from your ViewController(button event) and making ViewController as delegate.
3) make view returned from [wikitudeAR start] to the ViewController.view (and not window.view).
Hope this helps.
you can use CustomMenuButtonDelegateImpl1.m
#implementation CustomMenuButtonDelegateImpl1
- (void) customMenuButtonPressed:(WTPoi *)currentSelectedPoi {
//NSLog(#"addpoi");
[[WikitudeARViewController sharedInstance] hide];
[UIApplication sharedApplication].statusBarHidden = NO;
}
#end
and it woroks very well

networkActivityIndicatorVisible

Is this code correct to use with the networkActivityIndicatorVisible?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIApplication* app2 = [UIApplication sharedApplication];
app2.networkActivityIndicatorVisible = YES;
[self loadSources]; // Loads data in table view
app2.networkActivityIndicatorVisible = NO;
}
Teo
Since NetworkActivityIndicatorVisible can be set from several points while a connection is still active, you need to track the number of calls that enable/disable it. The following UIApplication category does just that using a static variable:
// file UIApplication+NetworkActivity.h
#interface UIApplication (NetworkActivity)
- (void)showNetworkActivityIndicator;
- (void)hideNetworkActivityIndicator;
#end
// file UIApplication+NetworkActivity.m
#import "UIApplication+NetworkActivity.h"
static NSInteger activityCount = 0;
#implementation UIApplication (NetworkActivity)
- (void)showNetworkActivityIndicator {
if ([[UIApplication sharedApplication] isStatusBarHidden]) return;
#synchronized ([UIApplication sharedApplication]) {
if (activityCount == 0) {
[self setNetworkActivityIndicatorVisible:YES];
}
activityCount++;
}
}
- (void)hideNetworkActivityIndicator {
if ([[UIApplication sharedApplication] isStatusBarHidden]) return;
#synchronized ([UIApplication sharedApplication]) {
activityCount--;
if (activityCount <= 0) {
[self setNetworkActivityIndicatorVisible:NO];
activityCount=0;
}
}
}
#end
Now import UIApplication+NetworkActivity.h in your client code and call
// on connection started:
[[UIApplication sharedApplication] showNetworkActivityIndicator];
// on connection finished:
[[UIApplication sharedApplication] hideNetworkActivityIndicator];
If your concern is that the indicator blinks for only a second, you don't need a background process. Just call [self performSelector:#selector(loadSources) withObject:Nil afterDelay:0.1] so the UI thread has time to start the network indicator animation before you block the main thread.
If you're not using AFNetworking (https://github.com/AFNetworking/AFNetworking) already you can check out their network activity indicator implementation in AFNetworkingActivityIndicatorManager.
If you do choose to use this library for your network access, they handle the network activity indicator for you automatically. All you need to do is make one call in your AppDelegate to set it up, they do the rest of the work for you.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
}
An shorter solution for tracking multiple activities - again using a UIApplication category and a static variable:
#interface UIApplication (NetworkActivityIndicator)
- (void)toggleNetworkActivityIndicatorVisible:(BOOL)visible;
#end
#implementation UIApplication (NetworkActivityIndicator)
-(void)toggleNetworkActivityIndicatorVisible:(BOOL)visible {
static int activityCount = 0;
#synchronized (self) {
visible ? activityCount++ : activityCount--;
self.networkActivityIndicatorVisible = activityCount > 0;
}
}
#end
I finally solved it. I used performSelectorInBackground to execute the load data into tableView
-(void)beginLoadSources {
[self loadSources]; // Loads data in table view
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self performSelectorInBackground:#selector(beginLoadSources) withObject:nil];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
Use thins code in your ExempleUIWebView.m
before - (void)viewDidLoad
(void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[ProgressHUD dismiss];
}
and use this after - (void)viewDidLoad
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[ProgressHUD show:#"Loading Privacy Policy" Interaction:NO];