2015-10-05 148 views
2

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

这里是我用于改变锁屏控制器按钮

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

我认为要做到这一点的唯一方法是使用通知。 – ThomasW

+0

你是否正确地遵循了源代码?直到你不显示你如何完成你的问题的答案应该是或不是,所以请显示你对代码做了什么。 –

+0

@ThomasW请你解释一下哪个通知,谢谢 – Niloufar

回答

0

我想在这些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. 
} 

OR

通过使用通知中心,您可以执行特定类别的任何行动,我已经使用将进入前景。根据用户需求有不同的选项可用。

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

+0

谢谢,但这不是我要找的。我的应用程序在后台工作,所以它始终处于活动状态我想要一些通知或委托来要求电话密码并将我的应用程序启动。 – Niloufar