ios 通用鏈接

關(guān)于通用鏈接的相關(guān)內(nèi)容如下圖所示:

通用鏈接.jpg

對應(yīng)的相關(guān)操作及代碼如下:
1.首先通過去開發(fā)者賬號中申請一個證書帶有Associated Domains 的證書,并開啟這個服務(wù)。


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

2.關(guān)于AppDelegate里面的代碼如下:

#pragma mark -通用鏈接
/**
 * 微信、qq屏蔽后通過Universal Link應(yīng)用 獲取到跳轉(zhuǎn)鏈接的一些基本信息
 */
- (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]) {
            //打開對應(yīng)頁面
            DebugNSLog(@"通過鏈接成功");
            [[SQURLSchemeManager urlSchemeInstance] getCurrentUrlScheme:webUrl sourceApplication:nil];
        }else{
            [[UIApplication sharedApplication] openURL: webUrl];
        }
    }
    return true;
}

由于通用鏈接和URL Scheme同時需要進(jìn)行判斷頁面跳轉(zhuǎn),所以創(chuàng)建了一個單例來處理。

單例SQURLSchemeManager代碼如下:

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

@interface SQURLSchemeManager : NSObject<SQPassValueDelegate>

+ (instancetype)urlSchemeInstance;

/**
 *通過當(dāng)前獲取的URL 跳轉(zhuǎn)到相應(yīng)的頁面
 */
- (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 協(xié)議名稱
 cn.shequbanjing.app:代表 app唯一標(biāo)識
 goodsDetail:代表scheme指定的頁面
 goodsId:代表傳遞的參數(shù)
 */

static SQURLSchemeManager *urlScheme;

@implementation SQURLSchemeManager

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

/**
 *通過當(dāng)前獲取的URL 跳轉(zhuǎn)到相應(yīng)的頁面
 * 頁面跳轉(zhuǎn)對應(yīng)來源
 */
- (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;
        }
        //跳轉(zhuǎn)到商鋪頁面
        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;
        }
        //跳轉(zhuǎn)到商品詳情頁面
        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;
        }
        //跳轉(zhuǎn)到公告詳情頁面
        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"];
        
        //跳轉(zhuǎn)到話題帖子分享詳情頁面
        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"];
        
        //跳轉(zhuǎn)到話題詳情頁面
        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;
        }
        //跳轉(zhuǎn)到便民電話詳情頁面
        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"]){
        //跳轉(zhuǎn)到首頁
    }
   
}

//將url里面的參數(shù)轉(zhuǎn)換成字典
- (NSDictionary *)getParamsWithURL:(NSURL *)url {
    
    //query是?后面的參數(shù),在這個demo中,指的是title=hello&content=helloworld&urlschemes=shixueqian
    NSString *query = url.query;
    
    //進(jìn)行字符串的拆分,通過&來拆分,把每個參數(shù)分開
    NSArray *subArray = [query componentsSeparatedByString:@"&"];
    //把subArray轉(zhuǎn)換為字典
    //tempDic中存放一個URL中轉(zhuǎn)換的鍵值對
    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]];
    }
    //返回轉(zhuǎn)換后的字典
    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;
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容