美文网首页
ios 通用链接

ios 通用链接

作者: 羊妞麻麻 | 来源:发表于2017-11-14 17:04 被阅读43次

关于通用链接的相关内容如下图所示:

通用链接.jpg

对应的相关操作及代码如下:
1.首先通过去开发者账号中申请一个证书带有Associated Domains 的证书,并开启这个服务。


屏幕快照 2017-11-14 下午5.09.58.png

2.关于AppDelegate里面的代码如下:

#pragma mark -通用链接
/**
 * 微信、qq屏蔽后通过Universal Link应用 获取到跳转链接的一些基本信息
 */
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
    if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
        NSURL *webUrl = userActivity.webpageURL;
        NSString *shareURL = [NSString stringWithFormat:@"%@",LocalCfg.API_SHARE_URL];
        if ([webUrl.host isEqualToString:shareURL]) {
            //打开对应页面
            DebugNSLog(@"通过链接成功");
            [[SQURLSchemeManager urlSchemeInstance] getCurrentUrlScheme:webUrl sourceApplication:nil];
        }else{
            [[UIApplication sharedApplication] openURL: webUrl];
        }
    }
    return true;
}

由于通用链接和URL Scheme同时需要进行判断页面跳转,所以创建了一个单例来处理。

单例SQURLSchemeManager代码如下:

#import <Foundation/Foundation.h>
#import "SQControllerPassValueDelegate.h"

@interface SQURLSchemeManager : NSObject<SQPassValueDelegate>

+ (instancetype)urlSchemeInstance;

/**
 *通过当前获取的URL 跳转到相应的页面
 */
- (void)getCurrentUrlScheme:(NSURL *)url sourceApplication:(NSString *)sourceApplication;
@end
#import "SQURLSchemeManager.h"
#import "SQShopViewController.h"
#import "AnnouncementListVC.h"
#import "SQPostListViewController.h"
#import "SQGoodDetialViewController.h"
#import "SQPostListViewController.h"
#import "LePhoneDetailViewController.h"
#import "SQTopicModel.h"
#import "SQPostDetailViewController.h"
#import "GrouponDetailVC.h"
#import "AnnouncementDetailVC.h"


/*
  sqbj://cn.shequbanjing.app/goodsDetail?goodsId=10011002
 sqbj:代表该scheme 协议名称
 cn.shequbanjing.app:代表 app唯一标识
 goodsDetail:代表scheme指定的页面
 goodsId:代表传递的参数
 */

static SQURLSchemeManager *urlScheme;

@implementation SQURLSchemeManager

+ (instancetype)urlSchemeInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        urlScheme = [[SQURLSchemeManager alloc] init];
    });
    return urlScheme;
}

/**
 *通过当前获取的URL 跳转到相应的页面
 * 页面跳转对应来源
 */
- (void)getCurrentUrlScheme:(NSURL *)url sourceApplication:(NSString *)sourceApplication{
    
    UINavigationController *navVC = [self getCurrentNav];
    JZContainerController *currentVC = (JZContainerController *)navVC.topViewController;
    
    NSDictionary *dict = [self getParamsWithURL:url];
     DebugNSLog(@"dict----%@",dict);
    NSString *pathString = dict[@"page"];
    if ([pathString isEqualToString:@"storeInfoShare"]) {
        if ([currentVC.contentViewController isKindOfClass:[SQShopViewController class]]) {
            return;
        }
        //跳转到商铺页面
        SQShopViewController *vc = [[SQShopViewController alloc] init];
        vc.merchantId = dict[@"merchantId"];
        vc.hidesBottomBarWhenPushed = YES;
        [currentVC.jz_navigationController pushViewController:vc animated:YES complete:^(BOOL finished) {
        }];
    }else if ([pathString isEqualToString:@"goodsInfoShare"]){
        if ([currentVC.contentViewController isKindOfClass:[SQGoodDetialViewController class]]) {
            return;
        }
        //跳转到商品详情页面
        SQGoodDetialViewController *gdmvc = [[SQGoodDetialViewController alloc] init];
        gdmvc.goodsId = [[NSString stringWithFormat:@"%@",dict[@"goodsId"]] integerValue];
        gdmvc.merchantId = dict[@"merchantId"];
        gdmvc.hidesBottomBarWhenPushed = YES;
        [currentVC.jz_navigationController pushViewController:gdmvc animated:YES];
        
    }else if([pathString isEqualToString:@"groupPurchaseeShare"]){
        if ([currentVC.contentViewController isKindOfClass:[GrouponDetailVC class]]) {
            return;
        }
        GrouponDetailVC *vc = [[GrouponDetailVC alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        vc.grouponId = [dict[@"grouponId"] integerValue];
        [currentVC.jz_navigationController pushViewController:vc animated:YES complete:nil];
        
    }else if([pathString isEqualToString:@"propertyNoticeShare"]){
        if ([currentVC.contentViewController isKindOfClass:[AnnouncementListVC class]]&&[currentVC.contentViewController isKindOfClass:[AnnouncementDetailVC class]]) {
            return;
        }
        //跳转到公告详情页面
        AnnouncementListVC* announcementListVC = [[AnnouncementListVC alloc] init];
        [announcementListVC setHidesBottomBarWhenPushed:YES];
        announcementListVC.automaticDetail = YES;
        announcementListVC.automaticAnnoucementId = dict[@"announcementId"];
        announcementListVC.otherCommunityId = [SafeObj2String([dict objectForKey:@"communityId"]) integerValue];
        [navVC pushViewController:announcementListVC animated:NO];
        
    }else if ([pathString isEqualToString:@"postInfoShare"]){
        if ([currentVC.contentViewController isKindOfClass:[SQPostDetailViewController class]]) {
            return;
        }
        SQPostListModel *currentModel = [[SQPostListModel alloc] init];
        currentModel.postId = dict[@"postId"];
        
        //跳转到话题帖子分享详情页面
        SQPostDetailViewController *postDetailVC = [[SQPostDetailViewController alloc]init];
        postDetailVC.postModel = currentModel;
        postDetailVC.postId = currentModel.postId;
        postDetailVC.hidesBottomBarWhenPushed = YES;
       
        [currentVC.jz_navigationController pushViewController:postDetailVC animated:YES complete:nil];
        
    }else if ([pathString isEqualToString:@"topicInfoShare"]){
        
        if ([currentVC.contentViewController isKindOfClass:[SQPostListViewController class]]) {
            return;
        }
        
        SQTopicModel *model = [[SQTopicModel alloc] init];
        model.eventId = dict[@"eventId"];
        
        //跳转到话题详情页面
        SQPostListViewController *postListVC = [[SQPostListViewController alloc] init];
        postListVC.hidesBottomBarWhenPushed = YES;
        postListVC.topicModel = model;
        [currentVC.jz_navigationController pushViewController:postListVC animated:YES complete:nil];
        
    }else if ([pathString isEqualToString:@"telphoneShare"]){
        if ([currentVC.contentViewController isKindOfClass:[LePhoneDetailViewController class]]) {
            return;
        }
        //跳转到便民电话详情页面
        LePhoneDetailViewController *phoneDetailVC = [[LePhoneDetailViewController alloc] init];
        phoneDetailVC.itemId =dict[@"areaContactId"];
        phoneDetailVC.isFromShareView = YES;
        phoneDetailVC.hidesBottomBarWhenPushed = YES;
        [currentVC.jz_navigationController pushViewController:phoneDetailVC animated:YES];
        
    }else if ([pathString isEqualToString:@"bannerShare"]){
        //跳转到首页
    }
   
}

//将url里面的参数转换成字典
- (NSDictionary *)getParamsWithURL:(NSURL *)url {
    
    //query是?后面的参数,在这个demo中,指的是title=hello&content=helloworld&urlschemes=shixueqian
    NSString *query = url.query;
    
    //进行字符串的拆分,通过&来拆分,把每个参数分开
    NSArray *subArray = [query componentsSeparatedByString:@"&"];
    //把subArray转换为字典
    //tempDic中存放一个URL中转换的键值对
    NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
    
    for (int i = 0 ; i < subArray.count ; i++) {
        //通过“=”拆分键和值
        NSArray *dicArray = [subArray[i] componentsSeparatedByString:@"="]
        ;
        //给字典加入元素,=前面为key,后面为value
        [tempDic setObject:dicArray[1] forKey:dicArray[0]];
    }
    //返回转换后的字典
    return tempDic ;
}

- (UINavigationController*)getCurrentNav {
    SQRootTabBarController *tabVC = [AppDelegate appDelegate].rootTabBarController;
    BaseNavigationController *selNavVC = (BaseNavigationController*)[tabVC selectedViewController];
    
    if (selNavVC.topViewController.presentedViewController && [selNavVC.topViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *nav = (UINavigationController*)selNavVC.topViewController.presentedViewController;
        return nav;
    }
    return selNavVC;
}

相关文章

  • iOS 通用链接(UniversalLinks)+ 分享功能的一

    iOS 通用链接(UniversalLinks)+ 分享功能的一些看法 iOS 通用链接(UniversalLin...

  • web跳转app

    iOS Universal Links(通用链接) 1.构建通用链接 2.上传通用链接 3.配置Xcode 4测试...

  • iOS 唤起APP之Universal Link(通用链接)

    什么是Universal Link(通用链接) Universal Link(通用链接)是Apple在iOS9推出...

  • iOS 唤起APP之Universal Link(通用链接)

    什么是Universal Link(通用链接) Universal Link(通用链接)是Apple在iOS9推出...

  • iOS Universal Link(通用链接)

    什么是通用链接:   通用链接是iOS9推出的一项新功能,如果你的应用(app)支持通用链接(Universal ...

  • iOS 通用链接

    最近集成了openinstall的sdk,其中有配置通用链接一项,不太了解,自己查了查资料,记录下自己的理解 Q:...

  • ios 通用链接

    关于通用链接的相关内容如下图所示: 对应的相关操作及代码如下:1.首先通过去开发者账号中申请一个证书带有Assoc...

  • iOS 通用链接

    Universal Links https://developer.apple.com/library/conte...

  • IOS - 通用链接

    1、通用链接使用系统。 IOS9及以上版本。可以在微信中,通过点击链接,直接跳转app可以在邮件,记事本等工具中,...

  • ios 通用链接

    1.通用链接验证:验证工具2.解释:通用链接说明

网友评论

      本文标题:ios 通用链接

      本文链接:https://www.haomeiwen.com/subject/bhefvxtx.html