简体   繁体   English

通过iPhone锁定屏幕将应用程序置于前台

[英]Bring app to foreground from iPhone lock screen

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. 它工作正常,但在我想通过要求用户解锁手机来使应用程序出现在前台时,例如apple Musics共享按钮操作。 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];
}

I suppose these delegate methods in the appdelegate.h would be helpful. 我认为appdelegate.h中的这些委托方法会有所帮助。 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.
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM