Creating shortcut for the app crashes on previous version - ios

I've created a shortcutItem in an App and when i run in ios 9.2 it works fine. But when I open the app which has ios 8.1 it crashes.
Thread 1:EXC_BAD_ACCESS (code=1, address=0x0)
How is the shortcutItem are created say if i create shortcutItem icon and title dynamically after (launchOption == nil) return YES Does the phone shows shortcutItems then?(because createShortcutItem is not called it should not show is what i think.) Will i be able to open shortcutItem once I opened the app is opened and minimized even though the shortcutItems and icons are not called in didFinishLaunchingwithoptions.
Am getting crashes in ios 8.1 at this line
shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
So i am returning if launchOptions == nil to fix the crash.
Below is the code am using in my app for using shortcut. Its created in my main app so I've modified names a little bit as an example.
How do i handle the shortcut which are not supported on early iOS ( from 8.0) or devices. I want my app to support both ios8 and greater versions.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
BOOL shouldPerformAdditionalDelegateHandling = YES;
// Shortcut creation and methods
[[MyAppShortcuts instance] createShortcutItem];
if (launchOptions == nil) {
return shouldPerformAdditionalDelegateHandling;
}
UIApplicationShortcutItem *shortcutItem;
shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
if ( shortcutItem != nil ){
// Open app from shortcut
self.rootViewCtrl_.shortcutItem_ = shortcutItem;
[[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
[[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];
// This will block "performActionForShortcutItem:completionHandler" from being called.
shouldPerformAdditionalDelegateHandling = NO;
}
return shouldPerformAdditionalDelegateHandling;
}
- (void) createShortcutItem {
UIApplicationShortcutIcon* firstIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:#"shortcutFirstItem"];
UIApplicationShortcutItem* firstItem;
firstItem = [[UIApplicationShortcutItem alloc]initWithType: firstItemType
localizedTitle: NSLocalizedString(#"First Item", nil)
localizedSubtitle: nil
icon: firstIcon
userInfo: nil];
//..... creating 2nd 3rd 4th item
[UIApplication sharedApplication].shortcutItems = #[firstItem, secondItem, thirdItem, fourthItem];
}
application:performActionForShortcutItem: method is called everytime when the app is opened using shortcut. If i create shortcutItems(icon, title and type) inside the method it calls everytime, does that affect opening shortcut in any way because creating items again and again?
- (void) application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
// Shortcut creation and methods
[ [ MyAppShortcuts instance ] createShortcutItem ];
[[FinAppShortcuts instance] handleShortCut:shortcutItem ];
}

You should place all logic regarding shortcut items after checking if it's available (iOS 9.1 onwards). I don't think launchOptions is nil for instances where it's not invoked by tapping the shortcut.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
BOOL shouldPerformAdditionalDelegateHandling = YES;
//checks if you could user shortcut items. only available in iOS 9.1 onwards
if ([UIApplicationShortcutItem class]){
// Shortcut creation and methods
[[MyAppShortcuts instance] createShortcutItem];
if (launchOptions[UIApplicationLaunchOptionsShortcutItemKey]){
// Open app from shortcut
self.rootViewCtrl_.shortcutItem_ = shortcutItem;
[[MyAppShortcuts instance] setMyAppShortcutIs:shortcutItem.type];
[[MyAppShortcuts instance] setAppLaunchedWithShortcut:YES];
// This will block "performActionForShortcutItem:completionHandler" from being called.
shouldPerformAdditionalDelegateHandling = NO;
}
}
return shouldPerformAdditionalDelegateHandling;
}

Related

iOS background fetch not working

I want to pull some data from server in every 30 min interval and set local notification to remind the user and i implemented below code to perform this operation.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSString *value = [[NSUserDefaults standardUserDefaults]objectForKey:#"bgFetch"];
if ([Utilities isInternetConnectionAvailable]) {
if ([self.window.rootViewController isKindOfClass:[HomeViewController class]]){
HomeViewController *homeController = (HomeViewController*)self.window.rootViewController;
[homeController fetchDatawithCompletionHandler:^(UserData *userData, NSString *errorResponse) {
if (userData) {
if (![homeController isNotificationAvailvableForTheData:userData]) {
[homeController scheduleLocalNotificationForUserData:userData];
completionHandler(UIBackgroundFetchResultNewData);
}
}else{
completionHandler(UIBackgroundFetchResultFailed);
}
}];
}else{
completionHandler(UIBackgroundFetchResultNoData);
}
}else{
completionHandler(UIBackgroundFetchResultFailed);
}
}
I also enabled background fetch in capability and added the key "Required background modes" in plist. When i checked after few hours, no local notification is set. Where am i doing wrong ?
I was facing the same issue. Whenever I simulated a background fetch via XCode it worked, whether with the emulator or a real device. If I unplugged the phone and just used it as I usually do. I never got a notification, respectively my code doesn't get executed. The solution is very simple and I think the difference to "simulate background fetch" is that your app is in a different stage then it is in your daily routine. To make it work, simply dispatch your code on a background thread. I just wrapped my code inside:
DispatchQueue.global(qos: .background).async
and it worked.

Getting notified when a phone call ends when app is in background

I know similar questions are asked but none of them helped.
My Observation
There are two cases: 1) When app is running and in the foreground, visible to the user 2) User presses home button and app moves to the background, not visible to the user. In case 1) when a phone call comes and end I get all the events with no problem. In case 2) I get no events, but when user opens the app, then I get all the events that has happened when app was in the background.
My Question
How can I get my code working even when app is in the background? Or, how can I move my app from background to foreground when those events happen?
What I've tried
I've tried enabling background fetch at Background Modes at Info.plist with an implementation of -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler. It had no effect.
Below is my code:
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// some other code above
self.callCenter = [[CTCallCenter alloc] init];
[self.callCenter setCallEventHandler:^(CTCall *call)
{
UINavigationController *nc = (UINavigationController *) [[(AppDelegate *) [[UIApplication sharedApplication] delegate] window] rootViewController];
MainViewController *vc = nc.viewControllers[0];
[vc setCallState:call.callState];
}
}
MainViewController.m
-(void)setCallState:(NSString *)callStateString
{
if ([callStateString isEqualToString:CTCallStateConnected])
{
NSLog(#"call connected!");
} else if ([callStateString isEqualToString:CTCallStateDialing])
{
NSLog(#"call dialing!");
} else if ([callStateString isEqualToString:CTCallStateDisconnected])
{
NSLog(#"call disconnected!");
} else if ([callStateString isEqualToString:CTCallStateIncoming])
{
NSLog(#"call incoming!");
} else
{
NSLog(#"unknown call state %#", callStateString);
}
}

After Receiving Notification how can i can get payload on app icon click?

My push notification works properly in Normal case but i have problem with following case:
1) when my app remove from background and get notification & tap on app icon then i want to push view controller and display payload data in that view controller.
2 ) when my app in background and get notification & tap on app icon then i want to push view controller and display payload data in that view controller.
following is my userInfo
{
aps = {
alert = "Call from rohan panchal";
appointmentId = 220;
badge = 0;
"call_token" = "T1==cGFydG5lcl9pZD00NTI1ODY1MiZzaWc9MzM1MmM0M2E2MjkwN2JiYWMzNjgyNjk0MjFlZWMyNWEzNTZmZmM3MjpzZXNzaW9uX2lkPTJfTVg0ME5USTFPRFkxTW41LU1UUTNNREl3TVRBd01qVXdOWDV3WXpCRFMyWTRlR2xhUWpGdU1YbFpNamhvV0hoNFVHTi1VSDQmY3JlYXRlX3RpbWU9MTQ3MDIwMTAwMiZyb2xlPXB1Ymxpc2hlciZub25jZT0xNDcwMjAxMDAyLjUyMDM0NDAzNjQzMjMmZXhwaXJlX3RpbWU9MTQ3MDgwNTgwMg==";
doctorId = 238;
"doctor_country" = US;
"doctor_name" = "John smith";
patientId = 239;
"patient_country" = US;
"patient_name" = "Lottry patel";
sessionId = "2_MX40NTI1ODY1Mn5-MTQ3MDIwMTAwMjUwNX5wYzBDS2Y4eGlaQjFuMXlZMjhoWHh4UGN-UH4";
sound = default;
};
}
following my code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// when i remove app from background & click on notification then following code run.
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if(notificationPayload)
{
NSLog(#"%#",notificationPayload);
WebViewController *DashBoard = [[WebViewController alloc]initWithNibName:#"WebViewController" bundle:nil];
self.navcntrl=[[UINavigationController alloc]initWithRootViewController:DashBoard];
}
else
{
DoctorMenuViewController *DoctorVC = [[DoctorMenuViewController alloc]initWithNibName:#"DoctorMenuViewController" bundle:nil];
self.navcntrl=[[UINavigationController alloc]initWithRootViewController:DoctorVC];
}
}
When I got notification then following method called.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(#"%#",userInfo);
WebViewController *DashBoard = [[WebViewController alloc]initWithNibName:#"WebViewController" bundle:nil];
[self.navcntrl pushViewController:DashBoard animated:YES];
}
Please help.Any help appreciated.
The push notification payload consists of:
alert - the alert string and actions
badge
sound
content-available
The key content-available is a new feature, and it is this key that makes silent push possible.
To enable, you also have to add remote-notifcation as your app UIBackgroundModes as described here.
This is what happens when content-available is in the payload:
If app is Suspended, the system will bring it into Background
If app was killed by user, nothing happens and app remains in Not Running
Read about app state changes.
A potential is pitfall:
You enable with content-available=1. But, it is WRONG to disable with content-available=0. To disable, you have to REMOVE the key in the payload.
plz use this
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
if(application.applicationState == UIApplicationStateInactive)
{
NSLog(#"Inactive");
//do your things when you click on notification
}
else if (application.applicationState == UIApplicationStateBackground)
{
NSLog(#"Background");
}
else if (application.applicationState == UIApplicationStateActive)
{
NSLog(#"Active");
}
}
for more information plz read this link http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/
application: didReceiveRemoteNotification:
is called when the OS receives a RemoteNotification and the app is running (in the background/suspended or in the foreground.)
and
application:(UIApplication *)application didFinishLaunchingWithOptions
is called when app is not running and you click on app icon.
1) when my app remove from background and get notification & tap on app icon -
TestViewController *testVC = your_test_vc;
UINavigationController *navVC = [[UINavigationController alloc]initWithRootViewController:testVC];
In TestViewController you can push your controller to show the payload data.
2) Whatever you wrote is correct.
Hope this will help you.
Apps have notification data delivered in a specific set of circumstances. If the user's interaction does not coincide with one of these, the data is not available. There are no exceptions.
the user taps a notification banner (or alert). It does not matter if the app was running or not.
the payload has the content-available field set to a true value, and iOS opts to deliver the notification.
Note: this answer does not explain how to invoke or handle any of the scenarios.
The field value from the userinfo is what you require then under
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{ //notification is received when your app is in background
//open the view controller you expected
}
else if(state == UIApplicationStateActive)
{
//notification is received when your app is in foreground
//do nothing
}
}

3D Touch Home Shortcuts In Obj-C

All of my apps are currently written in Obj-C. The link https://developer.apple.com/library/content/samplecode/ApplicationShortcuts/Introduction/Intro.html#//apple_ref/doc/uid/TP40016545 for the sample code of implementing Home Screen Shortcuts with 3D Touch is completely compiled in Swift. Anyone come across documentation for Obj-C, so I don't have to go through my AppDelegate and translate it all?
UPDATE:
After adding in all the shortcuts in Info.plist, I added in the AppDelegate.m:
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
UINavigationController *nav = (UINavigationController *) self.tabBarController.selectedViewController;
NSLog(#"%#", shortcutItem.type);
if ([shortcutItem.type isEqualToString:#"com.316apps.iPrayed.addPrayerRequest"]) {
Requests *gonow = [[Requests alloc] init];
[nav pushViewController:gonow animated:YES];
}
if ([shortcutItem.type isEqualToString:#"com.316apps.iPrayed.addPrayer"]) {
PrayerStats *controller = [[PrayerStats alloc] init];
[nav pushViewController:controller animated:YES];
}
if ([shortcutItem.type isEqualToString:#"com.316apps.iPrayed.addFast"]) {
FastStats *controller1 = [[FastStats alloc] init];
[nav pushViewController:controller1 animated:YES];
}
if ([shortcutItem.type isEqualToString:#"com.316apps.iPrayed.addStudy"]) {
StudyStats *controller2 = [[StudyStats alloc] init];
[nav pushViewController:controller2 animated:YES];
}
}
This allows it to work, without putting any other methods in, or adding anything to didFinishLaunchingWithOptions.
There are two states from where the user can open the app through Quick Actions.
TL;DR
You are always doing the same thing regardless of the state in which the app is when the quick action is done, that's why you only need to override application:performActionForShortcutItem:completionHandler: So if you wanted to do different things, then you would want to handle them in the two places, if not then just the overridden is enough.
One is if the app is killed or not running in background where we get the shortcut info on launch.
The other is if the app is running in background where we get the shortcut info on the new app delegate method.
To handle these Quick Action shortcuts when in background you need to override this method on App Delegate:
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
And for not running in background (killed) on your
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
or
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
you should check if the app was launched by a Quick Action:
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKey:UIApplicationLaunchOptionsShortcutItemKey];
(Link to related Apple Documentation)
Quote from the Official Apple Docs
It’s your responsibility to ensure the system calls this method
conditionally, depending on whether or not one of your app launch
methods (application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions:) has already handled a
quick action invocation. The system calls a launch method (before
calling this method) when a user selects a quick action for your app
and your app launches instead of activating.
The requested quick action might employ code paths different than
those used otherwise when your app launches. For example, say your app
normally launches to display view A, but your app was launched in
response to a quick action that needs view B. To handle such cases,
check, on launch, whether your app is being launched via a quick
action. Perform this check in your
application:willFinishLaunchingWithOptions: or
application:didFinishLaunchingWithOptions: method by checking for the
UIApplicationLaunchOptionsShortcutItemKey launch option key. The
UIApplicationShortcutItem object is available as the value of the
launch option key.
If you find that your app was indeed launched using a quick action,
perform the requested quick action within the launch method and return
a value of NO from that method. When you return a value of NO, the
system does not call the
application:performActionForShortcutItem:completionHandler: method.
Implement below 3 simple steps:
Step 1 : Write below method in AppDelegate class to Configure dynamic shortcut items.
NOTE : You can configure shortcut items in info.plist if you want it static.
(Refer Apple documentation.)
/**
* #brief config dynamic shortcutItems
* #discussion after first launch, users can see dynamic shortcutItems
*/
- (void)configDynamicShortcutItems {
// config image shortcut items
// if you want to use custom image in app bundles, use iconWithTemplateImageName method
UIApplicationShortcutIcon *shortcutSearchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
UIApplicationShortcutIcon *shortcutFavoriteIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeFavorite];
UIApplicationShortcutItem *shortcutSearch = [[UIApplicationShortcutItem alloc]
initWithType:#"com.sarangbang.QuickAction.Search"
localizedTitle:#"Search"
localizedSubtitle:nil
icon:shortcutSearchIcon
userInfo:nil];
UIApplicationShortcutItem *shortcutFavorite = [[UIApplicationShortcutItem alloc]
initWithType:#"com.sarangbang.QuickAction.Favorite"
localizedTitle:#"Favorite"
localizedSubtitle:nil
icon:shortcutFavoriteIcon
userInfo:nil];
// add all items to an array
NSArray *items = #[shortcutSearch, shortcutFavorite];
// add the array to our app
[UIApplication sharedApplication].shortcutItems = items;
}
Step 2 : In AppDelegate class application didFinishLaunchingWithOptions method write below code.
// UIApplicationShortcutItem is available in iOS 9 or later.
if([[UIApplicationShortcutItem class] respondsToSelector:#selector(new)]){
[self configDynamicShortcutItems];
// If a shortcut was launched, display its information and take the appropriate action
UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];
if(shortcutItem)
{
// When the app launch at first time, this block can not called.
//App launch process with quick actions
[self handleShortCutItem:shortcutItem];
}else{
// normal app launch process without quick action
}
}
Step 3 : Write below delegate method and completion handler in AppDelegate class.
/*
Called when the user activates your application by selecting a shortcut on the home screen, except when
application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) returns `false`.
You should handle the shortcut in those callbacks and return `false` if possible. In that case, this
callback is used if your application is already launched in the background.
*/
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem];
completionHandler(handledShortCutItem);
}
/**
* #brief handle shortcut item depend on its type
*
* #param shortcutItem shortcutItem selected shortcut item with quick action.
*
* #return return BOOL description
*/
- (BOOL)handleShortCutItem : (UIApplicationShortcutItem *)shortcutItem{
BOOL handled = NO;
NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;
NSString *shortcutSearch = [NSString stringWithFormat:#"%#.Search", bundleId];
NSString *shortcutFavorite = [NSString stringWithFormat:#"%#.Favorite", bundleId];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
if ([shortcutItem.type isEqualToString:shortcutSearch]) {
handled = YES;
SecondViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"secondVC"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
else if ([shortcutItem.type isEqualToString:shortcutFavorite]) {
handled = YES;
ThirdViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"thirdVC"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
}
return handled;
}
If you look at the sample code provided for apple, you'll see that they suggest that you write a method that handles your shortcut item so that you can handle it in all three places:
application: performActionForShortcutItem,
application: didFinishLaunchingWithOptions and
willFinishLaunchingWithOptions
An example of what I did was:
- (BOOL)handleShortCutItem:(UIApplicationShortcutItem *)shortcutItem {
BOOL handled = NO;
if (shortcutItem == nil) {
return handled;
}
if ([shortcutItem.type isEqualToString:kFavoritesQuickAction]) {
handled = YES;
}
if (handled) {
// do action here
}
return handled;
}
Then you would just call this method in any place where you are getting a shortcut item. This should help you along your way!
I make an objective-c demo project for home screen quick action.
3D touch home quick action demo : https://github.com/dakeshi/3D_Touch_HomeQuickAction
The Demo project implements the static quick action without Info.plist file to avoid unwanted situations before launching the app at first.
You can easily change it to the dynamic quick action.
As mentioned in apple documentation, you can handle quick action in application:didFinishLaunchingWithOptions: method. In that case, you should return NO to block to call application:performActionForShortcutItem:completionHandler: method.
It works on both swift 3 and 4 (only on home screen shortcuts)
//Add plist items as show in image and write following method in Appdelegate
//3D Touch Method shortcuts from home screen
func application(_ application: UIApplication, performActionFor shortcutItem:UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "Share" {
//handle action Share
let alert = UIAlertController(title: "3D touch Share", message: "Yahoo!!! 3D touch is working👌", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.window?.rootViewController?.present(alert,animated: true,completion: nil)
completionHandler(true)
} else if shortcutItem.type == "Logout" {
//handle action Type02
let alert = UIAlertController(title: "3D touch Logout", message: "Yahoo!!! 3D touch is working👌", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.window?.rootViewController?.present(alert,animated: true,completion: nil)
completionHandler(true)
} else {
completionHandler(false)
}
}

UIWindow's makeKeyAndVisible and background task

I've add the background task feature to my application. Here is my app delegate
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
[self.window setBackgroundColor:[UIColor blackColor]];
if (application.applicationState != UIApplicationStateBackground) {
// Application is launch in because user tap the app icon or from springboard
if ([application respondsToSelector:#selector(setMinimumBackgroundFetchInterval:)]) {
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
}
} else {
// Application is launch in background mode
}
RootViewController *rootViewController = [[RootViewController alloc] initByDevice];
[self.window setRootViewController:rootViewController];
[self.window makeKeyAndVisible];
return YES;
}
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[DataManager instance] updateDataWithMaxAttempt:5 block:^(BOOL success, NSArray *newData) {
if (success) {
if ([newData count] > 0) {
completionHandler(UIBackgroundFetchResultNewData);
} else {
completionHandler(UIBackgroundFetchResultNoData);
}
} else {
completionHandler(UIBackgroundFetchResultNoData);
}
}];
}
And this is my root view controller
// RootViewController.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(#"Did appear");
// Do something that I want it to happen only when the application is visible to user
}
When the user tap the app icon, application works like what I expected. I see "Did appear" in console and stuff is happening after that like I expected.
But when the application awake to perform background task (which not visible to user) the RootViewController's viewDidAppear still getting call because of this line
[self.window makeKeyAndVisible];
By calling makeKeyAndVisible, it makes RootViewController visible even though it's only awake for background task. To fix this, I have to move this line to applicationDidBecomeActive
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self.window makeKeyAndVisible];
}
As a result, the RootViewController's viewDidAppear now being call only when the application is in foreground (visible to user). But, my concern is, when application is in device memory (either active or inactive) applicationDidBecomeActive will be call several times.
user launch app
application become active again from springboard
device unlock
finish call
Is there any issue if I call UIWindow's makeKeyAndVisible several times during the application life cycle?
You can easily ensure that makeKeyAndVisible only happens once: wrap it.
if (!self.window.keyWindow) {
[self.window makeKeyAndVisible;
} else if (self.window.hidden) {
self.window.hidden = NO;
}

Resources