iOS应用间跳转UrlScheme

iOSURLScheme的配置

一、基本唤起

1、被唤起方要求配置URLScheme,这个例子使用YourApp。


image.png

这时唤起方能通过YourApp://做基本唤起,下面 NSLog(@"%@",strUrl);是打印URL,实际应和场景你是需要根据url里的具体内容跳到相应的页面。

如:

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

二、带参数唤起

1、被唤起方按(一、基本唤起)的步骤配置

2、在AppDelegate中添加以下方法接收参数。

-(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实现如下方法,其中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"调用openUrl你就会收到这个这个URL,实现内部的跳转的功能;值得注意的是SceneDelegate中的- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions;里面是直接保存URL而不能直接跳转,因为唤起方唤起的时候你的程序根本没有启动,这时候就会进到这个方法里,而这个方法使用StoryBoard的话是没有创建窗口 ViewController的,可以在主页中读取这个URL并实现启动目标页。

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

推荐阅读更多精彩内容

  • 什么是 URL Scheme? android中的scheme是一种页面内跳转协议,是一种非常好的实现机制,通过定...
    一笑小先生阅读 14,676评论 0 7
  • 应用间跳转 •app应用的跳转的原理 •如何实现两个app应用之间的跳转 •如何实现两个app之间跳转到指定的页面...
    花椒不麻牙阅读 3,873评论 0 0
  • 关于跳转应用的方式 https://www.jianshu.com/p/862885bd8ea2 URL Sche...
    未子涵阅读 4,886评论 0 0
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 11,006评论 0 5
  • 昨天,在回家的路上,坐在车里悠哉悠哉地看着三毛的《撒哈拉沙漠的故事》,我被里面的内容深深吸引住了,尽管上学时...
    夜阑晓语阅读 9,195评论 2 9