[英]How to display a specific UIViewController when a push notification is received and tapped
我想在收到推送通知並且用戶點擊該通知時顯示特定的UIViewController。 我進行了一些研究,並嘗試了一些成功的嘗試。
我嘗試了以下代碼,它可以在UIAlertController上運行,但沒有顯示我想要的特定UIViewController。 請幫幫我。 我正在使用Xcode 6.3。
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if(apsInfo) {
UIStoryboard *myStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
notificationViewController *nvc = [myStoryBoard instantiateViewControllerWithIdentifier:@"notificationViewController"];
[self.window.rootViewController presentViewController:nvc animated:YES completion:nil];
}
return YES;
}
提前致謝
您可以創建一個用於處理通知有效負載的“ Router”對象,然后將該對象從ViewController傳遞到ViewController,從而逐步構建導航堆棧。
這是我為說明該概念而進行的示例項目的鏈接 。 為了演示起見,在對application:application handleOpenURL:
buildNavigationStack
進行調用之后,將構建“ Router”,但是您可以輕松地將其更改為使用application:didReceiveRemoteNotification:fetchCompletionHandler:
收到的userInfo字典。
為此,我在AppDelegate類或Singleton類(如果項目中存在)中創建一個名為currentViewController
的UIViewController
變量。
對於創建的每個VC,我在viewWillAppear
和viewDidAppear
添加以下行:
[(myAppDelegate *)[[UIApplication sharedApplication] delegate] setCurrentViewController:self];
這樣,屏幕上顯示的VC始終保存在ivar中。
因此,在- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
或在您呈現alertViewController
任何位置,您都可以輕松呈現或推送新的VC。
[self.currentViewController presentViewController:vc animated:YES completion:nil];
詳細解決方案:在這種方法中,關鍵和棘手的部分是將currentViewController
設置為單例類。 休息會很簡單。
創建單例類:
//CapsHelper.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CAPSHelper : NSObject
{
}
@property (nonatomic, strong) UIViewController * currentViewController;
+ (instancetype)sharedHelpers;
//CapsHelper.m
#import "CAPSHelper.h"
@interface CAPSHelper ()
@end
@implementation CAPSHelper
@synthesize currentViewController;
#pragma mark Singleton Methods
+ (instancetype) sharedHelpers
{
static CAPSHelper *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
假設您的應用中大約有10個視圖控制器,分別稱為VC1
, VC2
,.., VC10
等。在每個VC的viewWillAppear:animated:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[CAPSHelper sharedHelpers] setCurrentViewController:self]
}
或者,如果這看起來像很多重復,您可以創建一個新類並覆蓋viewWillAppear:animated:
您將不會想起要在將來創建的新VC上添加內容的麻煩。
現在,我們已經准備好了,每次將VC顯示到屏幕上時,我們都將其保存在一個名為currentViewController
的變量中,並且可以通過導入#import "CAPSHelper.h"
從任何位置進行訪問。
//AppDelegate.m
#import "AppDelegate.h"
#import "CAPSHelper.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if(apsInfo)
{
if([[CAPSHelper sharedHelpers] currentViewController])
{
//UIViewController * pushNotificationViewController = [[UIViewController alloc] ..... This is your specific VC which you want to present when a PN is recieved..
[[[CAPSHelper sharedHelpers] currentViewController] presentViewController:pushNotificationViewController animated:YES completion:nil];
}
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if(apsInfo)
{
if([[CAPSHelper sharedHelpers] currentViewController])
{
//UIViewController * pushNotificationViewController = [[UIViewController alloc] ..... This is your specific VC which you want to present when a PN is recieved..
[[[CAPSHelper sharedHelpers] currentViewController] presentViewController:pushNotificationViewController animated:YES completion:nil];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.