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)啟動目標頁靡菇。