2

I have an application an I managed to make a custom remote controller with help of this question on stack overflow. it works fine but in I want to bring app to foreground by asking the user to unlock the phone, something like apple Musics share button action. Is it possible to ask user to unlock the phone and bring app to foreground to complete an action? I managed to make it work using local notification, but I think there need to be an alert view or a user interaction with button. Is it possible to make it work without any pop-up?

Here is the code I used to change lock screen controllers button

//App delegate   
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){

    [application registerUserNotificationSettings:[UIUserNotificationSettings
                                                   settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|
                                                   UIUserNotificationTypeSound categories:nil]];
   }
}

// inside viewDidLoad
MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];
MPFeedbackCommand *likeCommand = [rcc likeCommand];
[likeCommand setEnabled:YES];
[likeCommand setLocalizedTitle:@"I love it"];  // can leave this out for default
[likeCommand addTarget:self action:@selector(likeEvent:)];

MPFeedbackCommand *dislikeCommand = [rcc dislikeCommand];
[dislikeCommand setEnabled:YES];
[dislikeCommand setActive:YES];
[dislikeCommand setLocalizedTitle:@"I hate it"]; // can leave this out for default
[dislikeCommand addTarget:self action:@selector(dislikeEvent:)];

BOOL userPreviouslyIndicatedThatTheyDislikedThisItemAndIStoredThat = YES;

if (userPreviouslyIndicatedThatTheyDislikedThisItemAndIStoredThat) {
       [dislikeCommand setActive:YES];
    }

//Selectors:
 -(void)dislikeEvent: (MPFeedbackCommandEvent *)feedbackEvent
{
//I need to ask user to unlock the phone and bring app to foreground
NSLog(@"Mark the item disliked");
}
-(void)likeEvent: (MPFeedbackCommandEvent *)feedbackEvent
{
    //I need to ask user to unlock the phone and bring app to foreground
    NSLog(@"Mark the item liked");

   UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
notification.alertBody = @"This is local notification!";
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
Community
  • 1
  • 1
Niloufar
  • 512
  • 1
  • 5
  • 24

1 Answers1

0

I suppose these delegate methods in the appdelegate.h would be helpful. I think you could use the last one, "application did become active."

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

OR

By using notification center you can perform any actions in particular classes, i have used will enter foreground. There are different options available as per users requirements.

- (void)viewDidLoad {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appReturnToForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)appReturnToForeground {
    // Your code...What you want to perform.
}

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arpit Dhamane
  • 503
  • 7
  • 19
  • Thanks, but this is not what I'm looking for. My app works in background so it is always active. I want some notification or delegate to ask for phone pin and bring my app up. – Niloufar Oct 05 '15 at 08:10