I am trying to launch another application using its url handler right on my own app launch.
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSURL *actionURL = [NSURL URLWithString:#"fantastical2://"];
[[UIApplication sharedApplication] openURL:actionURL];
}
It basically works, however there is a significant delay of about 7 seconds from seeing my app appear to actually opening the URL.
How come the delay? How can I launch open a URL/app immediately when launching my own app or reduce this delay?
You can resolve using any of the examples below.
Using diapatch_async
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:urlString];
});
Using perfomSelector
- (void)applicationDidBecomeActive:(UIApplication *)application
{
...
//hangs for 10 seconds
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
//Fix: use threads!
[self performSelector:#selector(redirectToURL:)
withObject:url afterDelay:0.0];
...
}
- (void)redirectToURL:(NSString *)url
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
}
Using NSThread
- (void)applicationDidBecomeActive:(UIApplication *)application
{
...
//hangs for 10 seconds
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
//Fix: use threads!
[NSThread detachNewThreadSelector:#selector(openBrowserInBackground:)
toTarget:self withObject:url];
...
}
- (void)openBrowserInBackground:(NSString *)url
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
}
Try with the NSTimer with target ..
For just Reference :
[NSTimer scheduledTimerWithTimeInterval:7.0
target:self
selector:#selector(targetMethod:)
userInfo:nil
repeats:NO];
-(void) targetMethod{
// Call Here ...
NSURL *actionURL = [NSURL URLWithString:#"fantastical2://"];
[[UIApplication sharedApplication] openURL:actionURL];
//Invalidate the time
[myTimer invalidate];
myTimer = nil;
}
Add your code in - (void)applicationDidFinishLaunching:(UIApplication *)application because - (void)applicationDidBecomeActive:(UIApplication *)application will be called after applicationDidFinishLaunching is called.
Try calling the -openURL method within a block that runs on the main thread. This will cause it to execute once everything else is loaded:
-(void)applicationDidBecomeActive:(UIApplication *)application {
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *actionURL = [NSURL URLWithString:#"fantastical2://"];
[[UIApplication sharedApplication] openURL:actionURL];
});
}
Related
Whenever myapp is background state we need call webservices. Is it possible?
I'm using this delegate methods but not calling. If anyone knows this requirements please let me know
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
// We will add content here soon.
}
You can use NSURLSessionConfiguration for Background
-(NSURLSession *)backgroundSession
{
static NSURLSession *backgroundSession = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:#"com.shinobicontrols.BackgroundDownload.BackgroundSession"];
backgroundSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
});
return backgroundSession;
}
You can use the following way,
- (void)applicationDidEnterBackground:(UIApplication *)application
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundUpdateTask];
//Call the Web Services
[self endBackgroundUpdateTask];
});
}
- (void) beginBackgroundUpdateTask
{
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void) endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
}
I am working on a project where I fire a local notification frequently also repeat notification.
After fired notification, if I delete the application and install it again (without opening it), I get all the old notifications.
- (void)applicationWillTerminate:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
*and then set remaining all notification in*
- (void)applicationDidBecomeActive:(UIApplication *)application
{
UILocalNotification *notif = [[UILocalNotification alloc]init];
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
Try this:
-(void)clearNotifications
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
put your code in App did finish launching method.so, when app launch then it's cancel the all notification.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//cancel All notification when app is launch
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
Pls note that it cancel the all notification. if u want to cancel any particular notification then set it's tag and using tag cancel this notification.
I am trying to track's user location when the application moves to background. I just need to track once. So, I am not using background location services. In the code below this works when I uncomment 1 but it doesn't work when I uncomment 2.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// 1 works here [self startStandardUpdates];
self.bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
// 2 doesn't work here [self startStandardUpdates];
});
}
In second case this delegate function is not called.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"Updated\n%#",locations);
[self.manager stopUpdatingLocation];
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
Can someone tell me why this is not working in 2nd case. Also, is it fine using [self startStandardUpdates] in the first position ?
Try this
By default, this is YES for applications linked against iOS 6.0 or later.
if ([self.manager respondsToSelector:#selector(pausesLocationUpdatesAutomatically)]) {
self.manager.pausesLocationUpdatesAutomatically = NO;
}
I want to fetch service for every 15 mins,so i am using NSTimer.It is working fine.But how to call same service while app is in background state by using nstimer.Nstimer is not working in background state.Please suggest me.
//when the application is about to move from active to inactive state.
- (void)applicationWillResignActive:(UIApplication *)application
{
[self sendBackgroundLocationToServer];
}
- (void) sendBackgroundLocationToServer
{
UIBackgroundTaskIdentifier bgTask = UIBackgroundTaskInvalid;
bgTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
//Start Timer
[self startTimer];
//Close the task
if (bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}
}
-(void) closeUpShopBeforeBackgrounding {
// this listen to the UIApplicationDidEnterBackgroundNotification
[self beginBackgroundUpdateTask];
// do some closing stuff
[self endBackgroundUpdateTask]; }
- (void) beginBackgroundUpdateTask {
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}]; }
- (void) endBackgroundUpdateTask {
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid; }
This code is working fine for me, but when I foreground the app again, its dead in the water. Its as if the viewDidLoads are not firing, and nothing is happening.
Is there anything I need to do when the app foregrounds to kick it back into life?