2015-10-05 144 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