iOS應(yīng)用間跳轉(zhuǎn)UrlScheme

iOSURLScheme的配置

一擂错、基本喚起

1、被喚起方要求配置URLScheme,這個例子使用YourApp。


image.png

這時喚起方能通過YourApp://做基本喚起毕莱,下面 NSLog(@"%@",strUrl);是打印URL片任,實際應(yīng)和場景你是需要根據(jù)url里的具體內(nèi)容跳到相應(yīng)的頁面偏友。

如:

NSURL *url = [NSURL URLWithString:@"YourApp://"];
[app openURL:url options:@{} completionHandler:^(BOOL success) {
      if(success){
          NSLog(@"success");
      }else{
          NSLog(@"error");
      }
  }];

二、帶參數(shù)喚起

1对供、被喚起方按(一位他、基本喚起)的步驟配置

2、在AppDelegate中添加以下方法接收參數(shù)产场。

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url API_DEPRECATED_WITH_REPLACEMENT("application:openURL:options:", ios(2.0, 9.0)) API_UNAVAILABLE(tvos)
{
    NSString *strUrl=url.absoluteString;
    NSLog(@"%@",strUrl);
    return true;
}
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation API_DEPRECATED_WITH_REPLACEMENT("application:openURL:options:", ios(4.2, 9.0)) API_UNAVAILABLE(tvos);
{
   NSString *strUrl=url.absoluteString;
    NSLog(@"%@",strUrl);
    return true;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options API_AVAILABLE(ios(9.0)){
    NSString *strUrl=url.absoluteString;
     NSLog(@"%@",strUrl);
     return true;
}

如果你的項目使用****UIScene則在SceneDelegate實現(xiàn)如下方法鹅髓,其中TargetVC為目標VC;

//
//  SceneDelegate.h
//  YourApp
//
//  Created by 何景根 on 2021/1/12.
//  Copyright ? 2021 header. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;
@property (class,copy,nonatomic)NSString *strUrl;

@end


//
//  SceneDelegate.m
//  YourApp
//
//  Created by 何景根 on 2021/1/12.
//  Copyright ? 2021 header. All rights reserved.
//

#import "SceneDelegate.h"
#import "TargetVC.h"
static NSString *_strUrl=nil;
@interface SceneDelegate ()

@end

@implementation SceneDelegate
+ (void)setStrUrl:(NSString *)strUrl{
    _strUrl=strUrl;
}
+ (NSString *)strUrl{
    return _strUrl;
}

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    UIOpenURLContext *ctx = connectionOptions.URLContexts.anyObject;
    NSString *strUrl=ctx.URL.absoluteString;
    SceneDelegate.strUrl=strUrl;

    
}

- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts{
    UIOpenURLContext *ctx = URLContexts.anyObject;
    NSString *strUrl=ctx.URL.absoluteString;
        if (@available(iOS 13.0, *)) {
            UIWindowScene *scene = [UIApplication sharedApplication].openSessions.allObjects.lastObject.scene;
            UINavigationController *navi=((SceneDelegate *)scene.delegate).window.rootViewController;
            if([navi.topViewController isKindOfClass:TargetVC.class]){
                return;
            }
            TargetVC *vc = TargetVC.new;
            [navi pushViewController:vc animated:YES];
        }
    NSLog(@"%@",strUrl);
    
    
}
- (void)sceneDidDisconnect:(UIScene *)scene {
    // Called as the scene is being released by the system.
    // This occurs shortly after the scene enters the background, or when its session is discarded.
    // Release any resources associated with this scene that can be re-created the next time the scene connects.
    // The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}


- (void)sceneDidBecomeActive:(UIScene *)scene {
    // Called when the scene has moved from an inactive state to an active state.
    // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}


- (void)sceneWillResignActive:(UIScene *)scene {
    // Called when the scene will move from an active state to an inactive state.
    // This may occur due to temporary interruptions (ex. an incoming phone call).
}


- (void)sceneWillEnterForeground:(UIScene *)scene {
    // Called as the scene transitions from the background to the foreground.
    // Use this method to undo the changes made on entering the background.
}


- (void)sceneDidEnterBackground:(UIScene *)scene {
    // Called as the scene transitions from the foreground to the background.
    // Use this method to save data, release shared resources, and store enough scene-specific state information
    // to restore the scene back to its current state.
}


@end

例如喚起方通過**"YourApp://sampleOperator?param1=130"調(diào)用openUrl你就會收到這個這個URL,實現(xiàn)內(nèi)部的跳轉(zhuǎn)的功能;值得注意的是SceneDelegate中的- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions;里面是直接保存URL而不能直接跳轉(zhuǎn)京景,因為喚起方喚起的時候你的程序根本沒有啟動窿冯,這時候就會進到這個方法里,而這個方法使用StoryBoard的話是沒有創(chuàng)建窗口 ViewController的确徙,可以在主頁中讀取這個URL并實現(xiàn)啟動目標頁靡菇。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市米愿,隨后出現(xiàn)的幾起案子厦凤,更是在濱河造成了極大的恐慌,老刑警劉巖育苟,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件较鼓,死亡現(xiàn)場離奇詭異,居然都是意外死亡违柏,警方通過查閱死者的電腦和手機博烂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來漱竖,“玉大人禽篱,你說我怎么就攤上這事♀扇牵” “怎么了躺率?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵玛界,是天一觀的道長。 經(jīng)常有香客問我悼吱,道長慎框,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任后添,我火速辦了婚禮笨枯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘遇西。我一直安慰自己馅精,他們只是感情好,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布粱檀。 她就那樣靜靜地躺著洲敢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪梧税。 梳的紋絲不亂的頭發(fā)上沦疾,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機與錄音第队,去河邊找鬼哮塞。 笑死,一個胖子當著我的面吹牛凳谦,可吹牛的內(nèi)容都是我干的忆畅。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼尸执,長吁一口氣:“原來是場噩夢啊……” “哼家凯!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起如失,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤绊诲,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后褪贵,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體掂之,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年脆丁,在試婚紗的時候發(fā)現(xiàn)自己被綠了世舰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡槽卫,死狀恐怖跟压,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情歼培,我是刑警寧澤震蒋,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布茸塞,位于F島的核電站,受9級特大地震影響喷好,放射性物質(zhì)發(fā)生泄漏翔横。R本人自食惡果不足惜读跷,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一梗搅、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧效览,春花似錦无切、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至瘦锹,卻和暖如春籍嘹,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背弯院。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工辱士, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人听绳。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓颂碘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親椅挣。 傳聞我的和親對象是個殘疾皇子头岔,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容

  • 什么是 URL Scheme? android中的scheme是一種頁面內(nèi)跳轉(zhuǎn)協(xié)議鼠证,是一種非常好的實現(xiàn)機制峡竣,通過定...
    一笑小先生閱讀 13,645評論 0 7
  • 應(yīng)用間跳轉(zhuǎn) ?app應(yīng)用的跳轉(zhuǎn)的原理 ?如何實現(xiàn)兩個app應(yīng)用之間的跳轉(zhuǎn) ?如何實現(xiàn)兩個app之間跳轉(zhuǎn)到指定的頁面...
    花椒不麻牙閱讀 573評論 0 0
  • 關(guān)于跳轉(zhuǎn)應(yīng)用的方式 http://www.reibang.com/p/862885bd8ea2 URL Sche...
    未子涵閱讀 1,154評論 0 0
  • 推薦指數(shù): 6.0 書籍主旨關(guān)鍵詞:特權(quán)适掰、焦點、注意力娩鹉、語言聯(lián)想攻谁、情景聯(lián)想 觀點: 1.統(tǒng)計學現(xiàn)在叫數(shù)據(jù)分析,社會...
    Jenaral閱讀 5,722評論 0 5
  • 昨天弯予,在回家的路上戚宦,坐在車里悠哉悠哉地看著三毛的《撒哈拉沙漠的故事》,我被里面的內(nèi)容深深吸引住了锈嫩,盡管上學時...
    夜闌曉語閱讀 3,788評論 2 9