項目結(jié)構(gòu):UITabBarController->UINavigationController->UIViewController
實現(xiàn)結(jié)果:根據(jù)推送參數(shù)跳轉(zhuǎn)對應(yīng)的頁面
首先先分析一番:
我們會在AppDelegate里收到推送的參數(shù),一般是個JSON,我們可以在AppDelegate定義一個字典來接受這個JSON,然后需要根據(jù)這個字典來進行跳轉(zhuǎn)。我們可以定義一個類來處理跳轉(zhuǎn),在這個類里,先獲取到用戶點到的tabBarItem,獲取這個tabBarItem對應(yīng)的UINavigationController,然后使用導(dǎo)航器進行跳轉(zhuǎn)。
接下來開始寫代碼:
在AppDelegate定義一個字典接受推送的參數(shù):
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSDictionary *pushDic;//推送參數(shù)
@end
在收到推送的方法接受推送來的參數(shù)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
_pushDic = userInfo;
}
參數(shù)是這樣:
_pushDic = @{@"type":@"1",
@"text":@"miss",
@"url":@""};
type:跳轉(zhuǎn)的頁面,1.普通頁面 2.H5 將會打開一個鏈接
text:當(dāng)type等于1時需要
url: ???當(dāng)type等于2時需要
新建一個ReceivePush類,來處理跳轉(zhuǎn):
.h
#import <Foundation/Foundation.h>
@interface MSReceivePush : NSObject
+ (void)reveivePush;
@end
.m
#import "MSReceivePush.h"
#import "AppDelegate.h"
#import "PushOneViewController.h"
#import "PushTwoViewController.h"
@implementation MSReceivePush
+ (void)reveivePush{
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (app.pushDic.count == 0)return;
//獲取TabBarController
UITabBarController *tabVC = (UITabBarController *)[[[UIApplication sharedApplication] delegate] window].rootViewController;
//獲取用戶選擇的tabBarItem的NavigationController
UINavigationController *nav = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//根據(jù)參數(shù)跳轉(zhuǎn)頁面
if ([app.pushDic[@"type"] integerValue] == 1) {
PushOneViewController *one = [mainStoryboard instantiateViewControllerWithIdentifier:@"pushOne"];
one.pushString = app.pushDic[@"text"];
one.hidesBottomBarWhenPushed = YES;
[nav pushViewController:one animated:YES];
}else if([app.pushDic[@"type"] integerValue] == 2){
PushTwoViewController *two = [mainStoryboard instantiateViewControllerWithIdentifier:@"pushTwo"];
two.url = app.pushDic[@"url"];
two.hidesBottomBarWhenPushed = YES;
[nav pushViewController:two animated:YES];
}
}
@end
然后我們在需要跳轉(zhuǎn)的時候調(diào)用方法就可以了
[MSReceivePush reveivePush];