How to get that application launch from spotlight search? - ios

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.

Related

How to remove application from recent apps in Objective C IOS [duplicate]

This question already has answers here:
Prevent iOS from taking screen capture of app before going into background
(10 answers)
Closed 3 years ago.
I am new to IOS development
I want to remove my application from recent apps which is developed in Objective C.
I tried UIApplicationExitsOnSuspend in
info.plist
, but no luck still application is showing in info.plist.
Can anyone help me on this.
Thanks in Advance !!!
You could use concept of the cover window.
When app will resign active state you show your cover, and system will take snapshot of that cover instead of last visible UIViewController.
When app will become active you hide and deallocate your cover window.
Here is example
#import "AppDelegate.h"
#interface AppDelegate ()
#property (nonatomic) UIWindow *coverWindow;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
self.coverWindow = UIWindow.new;
self.coverWindow.rootViewController = UIViewController.new;
[self.coverWindow makeKeyAndVisible];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self.coverWindow removeFromSuperview];
self.coverWindow = nil;
}
#end
UIApplicationExitsOnSuspend is deprecated. You shouldn't use it any more. There has been reports of apple rejecting apps with that key. As per apple:
Deprecated
The system now automatically suspends apps leaving the foreground when
they don’t require background execution. For more information, see
About the Background Execution Sequence.
So for now, you are stuck with letting apple handle the background state of apps. Forcefully trying to exit the app by any manner would lead to a rejection from App Store.
UPDATE
I just noticed your comment saying what you actually want. To prevent the Background Snapshot, you can add a custom view to the window. This is similar to the answer posted by Mark Agranal below, but the thing is you don't need to add a new Window or new ViewController. You can simply add a custom view to the window and remove the view when the app reenters active state. In your AppDelegate:
// The view to use as a mask
#property (nonatomic, weak) UIView* coverView;
// Add the view to window
-(void)applicationWillResignActive:(UIApplication *)application
{
coverView = [[UIView alloc]initWithFrame:[self.window frame]];
[self.window addSubview:coverView];
}
// Remove the view to window
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(coverView != nil) {
[coverView removeFromSuperview];
coverView = nil;
}
}
Note that you can add any view to the window using the above method. The system will take screenshot of the added view and hence the sensitive user data will be protected.

iOS: Is this an okay way to detect if the app goes from background to foreground completly?

I need to be able to detect when an App has fully transitioned from background to foreground. I tried using - (void)applicationDidBecomeActive:(UIApplication *)application delegate, but the delegate seems to be called when the application first launches. Then, I tried using - (void)applicationDidBecomeActive:(UIApplication *)application, but it is called before the app has fully transitioned. In the end, I combined the two delegates and it seems to work well for me.
- (void)applicationWillEnterForeground:(UIApplication *)application {
openFromBackground = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (openFromBackground) {
openFromBackground = NO;
// do stuff
}
}
However, I am not sure this is the best way to handle the situation. Any tips or suggestions are appreciated.

iOS Objective-c run JavaScript in WebView depending on 3D force-touch shortcut

I am trying to load different JavaScript into my UIWebView depending on the 3D-touch shortcut that the user choose. 3D-Touch Example:
I use following method to detect the different shortcuts:
AppDelegate.m
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
if([shortcutItem.type isEqualToString:#"MY_SHORTCUT_NAME"]){
ViewController * view = [[ViewController alloc]init];
[view loadWebViewFromAppDelegate]; // Method 'loadWebViewFromAppDelegate' is from my ViewController and runs as expected.
}
}
ViewController.m:
-(void)loadWebViewFromAppDelegate {
NSLog(#"INECTING JS");
[_webView stringByEvaluatingJavaScriptFromString:#"window.location = 'https://google.de/';"];
}
But however the NSLog statement in loadWebViewFromAppDelegate is executed but stringByEvaluatingJavaScriptFromString does not run probably. The JavaScript won't run. But If I call loadWebViewFromAppDelegate from the ViewController's viewDidLoad everything works fine. I assume that the WebView has not loaded completely when performActionForShortcutItem triggers, so the WebView is not able to receive the JavaScript yet. How to inject JavaScript, depending on the 3D shortcut, into a UIWebView correctly?

State Preservation and restoration issue

In my application, I need to make a call, So obviously my app go to background while making a call using native call feature.if the call goes long my app is getting killed by IOS itself. Now i need to restore the last view at the time of making a call. I have used Native restoration. What i did is
1. Set the restoration ID for all the views and view controllers.
2. Override the app delegate restoration methods.
My Issue is,
If my app go to background and come back to foreground, Last view is displayed using preservation and suddenly moved to main view(Default launch view). just like last view blinking while coming to fore ground.
Here is my setting:
app Delegate code :
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
return YES;
}
Main view settings :
Main storyboard contains the login view as a root. Please guide me to fix the restoration issue.
I managed to get rid of the blinking by making the window key and visible in application:willFinishLaunchingWithOptions:.
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
return YES;
}

Preserving state of UIViewController

I've seen many conflicting answers that seem to be situation dependent, but I haven't figured out one that works for me. I'm building a tabbed application in Storyboard, and I want to save the state of a view controller inside one tab that lives inside a navigation controller. It has dynamically created objects (labels and pictures) that I want to preserve when the app is shut down.
Thus far, I have the following in AppDelegate.m:
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder {
return YES; }
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
return YES; }
and in my viewController.m file:
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[coder encodeObject:self.view forKey:#"view"];
[super encodeRestorableStateWithCoder:coder]; }
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder {
self.view = [coder decodeObjectForKey:#"view"];
[super decodeRestorableStateWithCoder:coder]; }
I've also assigned RestorationID's to UITabBarController, the navigation controller, and the view controller in question.
Right now, if I minimize and restore, data is preserved, but if I stop and restart using xcode, it's lost. What am I missing?
Try to run an app, push the Home button twice to go to the app switcher, swipe your app away to close it and run it by clicking the icon on home screen (not from Xcode!). Is now everything ok?
I suppose when you run your app from Xcode it not just relaunches it, but reinstalls it, so that is not exactly what you need.

Resources