What's the difference between [UIApplication sharedApplication] and application argument of didFinishLaunchingWithOptions? - ios

I'm now learning about background fetch functionality in iOS 7 and Xcode 5, and I've read a few tutorials explaining how to set setMinimumBackgroundFetchInterval within application: didFinishLaunchingWithOptions: method.
One type of sample code I've read is the following one:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchMinimum];
return YES;
}
And the other is the following, which utilizes the application argument to set its background initialization:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setMinimumBackgroundFetchInterval: UIApplicationBackgroundFetchMinimum];
return YES;
However, I don't get what makes one different from the other in the two codes above. The shareApplication class method returns UIApplication *, which is exactly the same as the type of the application argument.
And if there are NOT any differences between the two, what's the point of using the former? As far as I read, there are more samples taking the first approach, but I always feel the simpler is better (again, if there is NO difference - I think some differences exists, as mentioned above).

There are no diferences, using the method parameter is useful when you don't know which class sent that message (for example, imagine that you have several UITableView, all of them with the same delegate, you want to know which one called the delegate).
In this case, there's only one UIAplication per App, and that's the one that is in your [UIApplication sharedApplication] and the one that calls the delegate.
Summarising, is just the same object and there's no diference, just style.
Extending the example, imagine that you have a variable like this:
#property (nonatomic, strong) UITableView *myTable;
Yo do:
self.myTable.delegate = self;
When the delegate is called, theres are equivalents:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//self.myTable = tableView;
}

In this case, there is no difference.
It's probably a matter of style/preference as to which option you use.
I use [UIApplication sharedApplication].

There is no difference UIApplication is a singleton class and [UIApplication sharedApplication] is a way to access that shared variable of singleton class If you are calling the method from UIApplication class itself then using application is enough to access the method.

Related

How to manage state in ios objective c

I’m new in iOS development. My question is, I’ve two view controllers.
viewController - A viewController - B
Now, if i killed the app from the viewController - A and than relaunch the app. than app must be open the viewController - A. and if i killed the app from the viewController - B and than relaunch the app. than app must be open the viewController - B.
Can anyone help me, I’ve done the RND but can not find the proper solution.
Thanks
Create a sharedDelegate in AppDelegate.m file
+(AppDelegate *)sharedDelegate {
return (AppDelegate *) [UIApplication sharedApplication].delegate;
}
in AppDelegate.h
+ (AppDelegate *)sharedDelegate;
#property (nonatomic, strong) NSString *currentViewContoller;
when push to any contoller then set AppDelegate's currentViewContoller to new VC
YourViewController *vc=[[YourViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[AppDelegate sharedDelegate].currentViewContoller = NSStringFromClass([YourViewController class]);
now when app is terminated
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[[NSUserDefaults standardUserDefaults]setObject:[AppDelegate sharedDelegate].currentViewContoller forKey:#"currentVC"];
}
now when app launched first time check previous controller when app terminated
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *string=[[NSUserDefaults standardUserDefaults] valueForKey:#"currentVC"];
and push this class
UIViewController *object = [[NSClassFromString(string) alloc] init...];
}
applicationWillTerminate you can use but it will only get called if user quit app from forground If your app is in background and then user quit app then applicationWillTerminate will not get called.
So, you have to take care of applicationDidEnterBackground.
So, when app enter in background (i.e call applicationDidEnterBackground ) or call applicationWillTerminate save state(your current VC) in your user defaults.
Now in your didFinishLaunchingWithOptions set that view controller as rootviewcontroller or whatever way that you want to manage it.
reference : Apple documentation for applicationWillTerminate
PS : You should not manage app like this. It is horrible way!! If possible then run your app as normal flow!
If you're using Storyboards you can use the Restoration Identifier to communicate the App which controller to launch as first
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html

Will I ever need the UIApplication* application from the ApplicationDelegate functions?

When would the application parameter in any of the app delegates not be equal to the shared instance?
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplication* sharedapp = [UIApplication sharedApplication];
if(application == sharedapp){
//things are as I expect
}else{
//some other situation I can't think of
}
}
No, there wouldn't be any situation like that.
From Documentation:
Every app has exactly one instance of UIApplication. When an app is launched, the system calls the UIApplicationMain function; among its other tasks, this function creates a singleton UIApplication object. Thereafter you access the object by calling the sharedApplication class method.
The call to [UIApplication sharedApplication] will always return a pointer to your app's shared application object. That's what the method is for.
What is the variable sharedApp you are comparing against? Presumably that's an instance variable that you defined and set to contain a pointer to your application object with an identical call to [UIApplication sharedApplication]?

How to get that application launch from spotlight search?

I have implemented spotlight search in my application, Everything is working fine, But in some situation I want to require to know that application launch from spotlight search or not ? please help me how can I know this.
AppDelegate.m
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray *restorableObjects))restorationHandler{
self.isSportlight = YES;
self.strSportlightUnitname = [userActivity.userInfo valueForKey:#"kCSSearchableItemActivityIdentifier"];
return YES;
}
rootViewcontroller.m
- (void)viewDidLoad
{
if(!appDel.isSportlight){
[self OnLaunchSettings];
}else{
[self setupSportLightEvent];
}
}
Aspected result is appDel.isSportlight = TRUE But appDel.isSportlight always got FALSE because "continueUserActivity:(NSUserActivity *)userActivity restorationHandler" method call after "ViewDidLoad"
The root view controller shouldn't be checking in with the app delegate, that relationship is the wrong way round. The app delegate should be telling the root view controller (or possibly posting a global notification if other controllers need to know about it) so that it can respond at any time, not just when it's initially setting things up. You need to write your code so that you can switch between different states. That would usually be done by pushing or adding/removing child view controllers so you compartmentalise the functionality of each mode.

Global Device Orientation

I want to keep separate Class "Example: Device Manager" with one property "currentDeviceOrientation". from this how to achieve the following state.
When ever app launch first time i want to update the "currentDeviceOrientation" property.
When ever app coming from background to foreground i want to update the
"current DeviceOrientation" property.
According the value of "currentDeviceOrientation" my rootViewController view should get load from - (void)loadView method.
Kindly give good way to solve my problem.
You can use the already built-in methods in AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}

Calling methods from another class

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
facebook = [[Facebook alloc] initWithAppId:#"xxx" andDelegate:self];
}
I declared Facebook in my AppViewDelegate. I want use this Facebook class from my UIViewController or another class. I searched sharedApplication title but i didn't that.
Thanks for your answer.
It is possible to get it by doing:
[(AppViewDelegate *)[[UIApplication sharedApplication] delegate] facebook];
I put the cast to AppViewDelegate assuming that is the name of your AppDelegate. However, the problem with this is that you normally don't want to reference your AppDelegate from another class to use it as a class that holds a global variable.
What I would do in this situation is to use a Singleton pattern for Facebook class. Check this link Singleton Pattern

Resources