关于通用链接的相关内容如下图所示:
通用链接.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;
}
网友评论