【问题标题】:Bring app to foreground from iPhone lock screen将应用程序从 iPhone 锁定屏幕带到前台
【发布时间】:2015-10-05 07:33:46
【问题描述】:

我有一个应用程序,我设法在this question on stack overflow 的帮助下制作了一个自定义遥控器。 它工作正常,但我想通过要求用户解锁手机来将应用程序带到前台,比如苹果音乐分享按钮操作。是否可以要求用户解锁手机并将应用程序置于前台以完成操作? 我设法使用本地通知使其工作,但我认为需要有一个警报视图或用户与按钮的交互。是否可以在不弹出任何窗口的情况下使其工作?

这是我用来更改锁屏控制器按钮的代码

//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];
}

【问题讨论】:

  • 我认为唯一的办法就是使用通知。
  • 您是否正确地遵循了来源?直到您没有展示您是如何完成您的问题的
  • @ThomasW 请您解释一下哪个通知,谢谢
  • @AjayPandya 我已经更新了我的问题

标签: ios iphone lockscreen


【解决方案1】:

我认为 appdelegate.h 中的这些委托方法会有所帮助。 我认为您可以使用最后一个,“应用程序确实已激活。”

- (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.
}

或

通过使用通知中心,您可以在特定类中执行任何操作,我使用过的将进入前台。根据用户要求,有不同的选项可供选择。

- (void)viewDidLoad {

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

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

【讨论】:

  • 谢谢,但这不是我要找的。我的应用程序在后台运行,因此它始终处于活动状态。我想要一些通知或委托人要求提供电话密码并启动我的应用程序。
猜你喜欢
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 2015-06-14
相关资源
最近更新 更多