繁体   English   中英

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

[英]Bring app to foreground from iPhone lock screen

我有一个应用程序,通过此问题,我设法制作了一个自定义遥控器。 它工作正常,但在我想通过要求用户解锁手机来使应用程序出现在前台时,例如apple Musics共享按钮操作。 是否可以要求用户解锁手机并将应用程序带到前台以完成操作? 我设法使用本地通知使其工作,但我认为需要有一个警报视图或用户与按钮的交互。 是否可以使它正常工作而没有弹出窗口?

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

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

我认为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.
}

在此处输入图片说明

暂无
暂无

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

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