根据推送参数实现跳转

项目结构:UITabBarController->UINavigationController->UIViewController
实现结果:根据推送参数跳转对应的页面

首先先分析一番:
我们会在AppDelegate里收到推送的参数,一般是个JSON,我们可以在AppDelegate定义一个字典来接受这个JSON,然后需要根据这个字典来进行跳转。我们可以定义一个类来处理跳转,在这个类里,先获取到用户点到的tabBarItem,获取这个tabBarItem对应的UINavigationController,然后使用导航器进行跳转。

接下来开始写代码:
在AppDelegate定义一个字典接受推送的参数:

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSDictionary *pushDic;//推送参数
@end

在收到推送的方法接受推送来的参数
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
_pushDic = userInfo;
}

参数是这样:

_pushDic = @{@"type":@"1",
              @"text":@"miss",
              @"url":@""};

type:跳转的页面,1.普通页面 2.H5 将会打开一个链接
text:当type等于1时需要
url:    当type等于2时需要

新建一个ReceivePush类,来处理跳转:
.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];
    //根据参数跳转页面
    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

然后我们在需要跳转的时候调用方法就可以了

 [MSReceivePush reveivePush];

Git:根据模拟推送参数进行跳转

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,232评论 4 61
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,469评论 2 4
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 喜欢收听电台节目的小伙伴和喜欢阅读文章的小伙伴是有一些区别的,我也是一个电台订阅和收听的重度用户,哪一天自己一直收...
    大宝频道阅读 422评论 1 3