代码之家  ›  专栏  ›  技术社区  ›  LoneWanderer

应用程序似乎通过URL方案正确调用,但什么也没发生

  •  0
  • LoneWanderer  · 技术社区  · 5 年前

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

    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>
    

    这个项目是用Xcode 11.1创建的,我正在iOS13上测试。

    0 回复  |  直到 5 年前
        1
  •  34
  •   matt    5 年前

    实施 scene(_:openURLContexts:)

    scene(_:willConnectTo:options:) 而是在 options

        2
  •  13
  •   Jitendra    5 年前

    这是中的方法斯内德雷特。斯威夫特. Swift版本 损失13

    @available(iOS 13.0, *)
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        if let url = URLContexts.first?.url {
            // Handle URL
            WXApi.handleOpen(url, delegate: AppDelegate.shared)
        }
    }
    
        3
  •  11
  •   SagarScript    5 年前

    在最新的SDK中,如果您不使用SceneDelegate,那么它就可以正常工作了。

    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(_ 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])        
    }
    
        4
  •  1
  •   Peterworks    5 年前

    在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);
    
    }
        5
  •  1
  •   Hejazi    5 年前

    scene(_ scene:, continue userActivity:) 也没有 scene(_ scene:, openURLContexts URLContexts:) 被呼叫。

    connectionOptions 传递给方法 scene(_ scene:, willConnectTo session, options connectionOptions:)

    func scene(_ scene: UIScene,
               willConnectTo session: UISceneSession,
               options connectionOptions: UIScene.ConnectionOptions) {
    
        if let userActivity = connectionOptions.userActivities.first {
            self.scene(scene, continue: userActivity)
        }
    }
    
        6
  •  0
  •   Sheldon    5 年前

    我遇到一个问题,使用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>>
    )}
    
        7
  •  -4
  •   Rahul K Rajan    5 年前

    Refer this apple documentation

    如果实现从两个ap返回NO,则不调用此方法复制:将使用选项完成启动:和ap复制:使用选项完成启动:方法。(如果只实现了这两个方法中的一个,则其返回值将确定是否调用此方法。)如果应用程序实现的是applicationIDfinishLaunching:方法而不是ap复制:使用选项完成启动:,则在应用程序初始化后调用此方法以打开指定的URL。