Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 方法';应用程序:openURL:options:';不叫_Ios_Swift_Ios13_Custom Scheme Url - Fatal编程技术网

Ios 方法';应用程序:openURL:options:';不叫

Ios 方法';应用程序:openURL:options:';不叫,ios,swift,ios13,custom-scheme-url,Ios,Swift,Ios13,Custom Scheme Url,我正在尝试使用自定义方案从网页打开我的应用程序。应用程序已打开,但未调用以下方法: func application(_ app: UIApplication, open url: URL, options [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { // This is not called } 我的info.plist如下所示: <key>CFBundleURLTypes</key

我正在尝试使用自定义方案从网页打开我的应用程序。应用程序已打开,但未调用以下方法:

func application(_ app: UIApplication, open url: URL, options [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // This is not called
}
我的
info.plist
如下所示:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>MyApp</string>
            </array>
            <key>CFBundleURLName</key>
            <string>url here</string>
        </dict>
    </array>
CbundleUrlTypes
循环流化床锅炉方案
MyApp
CFBundleURLName
此处的url
该项目是使用Xcode 11.1创建的,我正在iOS 13上进行测试。

在场景代理中实现
场景(uU2;:openURLContexts:)

如果URL启动了你的应用程序,你将得到
场景(uwwillconnectto:options:)
,它位于场景代理中的
选项

实现
场景(wwillconnectto:options:)


如果URL启动了你的应用程序,你会看到
场景(u:willConnectTo:options:)
,它位于
选项中,多亏了@Matt,这就是我解决问题的方法

关于SceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { NSURL *url = connectionOptions.URLContexts.allObjects.firstObject.URL; NSLog(@"When app is not on memory ::::: %@",url); } ​ - (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts { NSURL *url = URLContexts.allObjects.firstObject.URL; NSLog(@"When app is on memory ::::: %@",url); } -(void)场景:(UIScene*)场景将连接到会话:(UISceneSession*)会话选项:(UISceneConnectionOptions*)连接选项{ NSURL*url=connectionOptions.URLContexts.allObjects.firstObject.url; NSLog(@“当应用程序不在内存中时:::%@”,url); } ​ -(void)场景:(UIScene*)场景openURLContexts:(NSSet*)URLContexts{ NSURL*url=URLContexts.allObjects.firstObject.url; NSLog(@“当应用程序在内存中时:::%@”,url);
} 多亏了@Matt,这就是我解决问题的方法

关于SceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { NSURL *url = connectionOptions.URLContexts.allObjects.firstObject.URL; NSLog(@"When app is not on memory ::::: %@",url); } ​ - (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts { NSURL *url = URLContexts.allObjects.firstObject.URL; NSLog(@"When app is on memory ::::: %@",url); } -(void)场景:(UIScene*)场景将连接到会话:(UISceneSession*)会话选项:(UISceneConnectionOptions*)连接选项{ NSURL*url=connectionOptions.URLContexts.allObjects.firstObject.url; NSLog(@“当应用程序不在内存中时:::%@”,url); } ​ -(void)场景:(UIScene*)场景openURLContexts:(NSSet*)URLContexts{ NSURL*url=URLContexts.allObjects.firstObject.url; NSLog(@“当应用程序在内存中时:::%@”,url); } 下面是SceneDelegate.swift中的方法。iOS13的Swift版本

@可用(iOS 13.0,*)
func场景(u场景:UIScene,openURLContexts:Set){
如果让url=URLContexts.first?.url{
//句柄URL
WXApi.handleOpen(url,委托:AppDelegate.shared)
}
}
下面是SceneDelegate.swift中的方法。iOS13的Swift版本

@可用(iOS 13.0,*)
func场景(u场景:UIScene,openURLContexts:Set){
如果让url=URLContexts.first?.url{
//句柄URL
WXApi.handleOpen(url,委托:AppDelegate.shared)
}
}

对于最新的SDK,如果您不使用ScenedLegate,这项功能确实可以正常工作

如果您使用的是sceneDelegate,则不会调用以下AppDelegate方法,因此无法处理登录

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let handled = ApplicationDelegate.shared.application(
        application,
        open: url,
        sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
        annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    return handled
}
这是因为,该方法(可以理解)被推迟到SceneDelegate中的以下方法:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    ...
}
func场景(scene:UIScene,openURLContexts:Set){
...
}
我可以确认,用于实现ScenedLegate的iOS 13应用程序的解决方案是:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }
    let _ = ApplicationDelegate.shared.application(
        UIApplication.shared,
        open: url,
        sourceApplication: nil,
        annotation: [UIApplication.OpenURLOptionsKey.annotation])        
}
func场景(scene:UIScene,openURLContexts:Set){
guard let url=URLContexts.first?url else{
返回
}
让u=applicationelegate.shared.application(
UIApplication.shared,
打开:url,
sourceApplication:nil,
注释:[UIApplication.OpenURLOptionsKey.annotation])
}

对于最新的SDK,如果您不使用ScenedLegate,这项功能确实可以正常工作

如果您使用的是sceneDelegate,则不会调用以下AppDelegate方法,因此无法处理登录

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let handled = ApplicationDelegate.shared.application(
        application,
        open: url,
        sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
        annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    return handled
}
这是因为,该方法(可以理解)被推迟到SceneDelegate中的以下方法:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    ...
}
func场景(scene:UIScene,openURLContexts:Set){
...
}
我可以确认,用于实现ScenedLegate的iOS 13应用程序的解决方案是:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }
    let _ = ApplicationDelegate.shared.application(
        UIApplication.shared,
        open: url,
        sourceApplication: nil,
        annotation: [UIApplication.OpenURLOptionsKey.annotation])        
}
func场景(scene:UIScene,openURLContexts:Set){
guard let url=URLContexts.first?url else{
返回
}
让u=applicationelegate.shared.application(
UIApplication.shared,
打开:url,
sourceApplication:nil,
注释:[UIApplication.OpenURLOptionsKey.annotation])
}

我遇到了一个问题,使用safari快捷方式启动应用程序,多次调用openURLContexts

2019-12-09 18:33:41.257088+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a71c0; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd4750; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
2019-12-09 18:33:42.022132+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a6d40; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd6f70; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
2019-12-09 18:33:41.257088+0800八重奏[5019:1468254]openURLContextsopenURLContexts{(
)}
2019-12-09 18:33:42.022132+0800八重奏[5019:1468254]openURLContextsopenURLContexts{(
)}

我遇到了一个问题,使用safari快捷方式启动应用程序,多次调用openURLContexts

2019-12-09 18:33:41.257088+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a71c0; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd4750; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
2019-12-09 18:33:42.022132+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a6d40; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd6f70; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
2019-12-09 18:33:41.257088+0800八重奏[5019:1468254]openURLContextsopenURLContexts{(
)}
2019-12-09 18:33:42.022132+0800八重奏[5019:1468254]openURLContextsopenURLContexts{(
)}

我遇到了同样的问题,没有调用
场景(\uscene:,continue userActivity:)
也没有调用
场景(\uscene:,openurlcontents:)

当我选中传递给方法
场景的
connectionOptions
时(\uscene:,willConnectTo session,options connectionOptions:)
它有一个具有预期URL的用户活动。因此,我最终手动调用了该方法:

func场景(u场景:UIScene,
将连接到会话:UISceneSession,
选项连接选项:UIScene.connectionOptions){
如果让userActivity=connectionOptions.userActivities.first{
self.scene(场景,continue:userActivity)
}
}

我遇到了同样的问题,没有调用
场景(\uscene:,continue userActivity:)
也没有调用
场景(\uscene:,openurlcontents:)

当我