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 *)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 

@interface SceneDelegate : UIResponder 

@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 *)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并实现启动目标页。

你可能感兴趣的:(iOS应用间跳转UrlScheme)