美文网首页程序员
iOS应用间跳转UrlScheme

iOS应用间跳转UrlScheme

作者: 何景根 | 来源:发表于2021-01-15 11:10 被阅读0次

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

相关文章

  • iOS应用间跳转UrlScheme

    iOSURLScheme的配置 一、基本唤起 1、被唤起方要求配置URLScheme,这个例子使用YourApp。...

  • iOS内深度链接问题记录

    一、iOS app之间跳转方式1、通过URLScheme2、Universal Link(需iOS9.0以上支持)...

  • 跳转到设置WIFI

    在iOS10更新后,系统设置跳转被禁用。只能跳转App设置,但是最近发现苹果又更新了URLscheme,亲测可用,...

  • 解决iOS10不能跳转系统WiFi列表的问题

    在iOS10更新后,系统设置跳转被禁用,只能跳转App设置,但是最近发现苹果又更新了URLscheme,亲测可用,...

  • 跳转系统设置界面总结

    在iOS10更新后,系统设置跳转被禁用,只能跳转App设置,但是苹果又更新了URLscheme,亲测不可用。 步骤...

  • 获取plist中的URL Schemes

    最近在写的项目中涉及到了应用间的跳转实现,iOS实现跳转应用是通过 openURL:方法; 此时遇到多个应用跳转到...

  • iOS应用间跳转

    场景需求:一个应用A(以news应用为示例)跳转到另外一个应用B(以weChat为示例),常见需求如下1.应用推荐...

  • iOS应用间跳转

    1.应用通过配置URL schemes进行标识(可以配置多个URL schemes)。跳转到其他应用就必须知道这个...

  • ios 应用间跳转

    1.先来看看效果,这里做了三个功能 从MyApp跳转到YourApp 从MyApp跳转到YourApp的指定页面 ...

  • iOS - 应用间跳转

    在平常的开发中,会经常遇到从我们自己的App跳转到其他App,如微信分享,第三方支付,使用系统的内置程序等; 应用...

网友评论

    本文标题:iOS应用间跳转UrlScheme

    本文链接:https://www.haomeiwen.com/subject/ytiiaktx.html