菜鸟教程小白 发表于 2022-12-12 13:01:49

ios - 在 didFinishLaunchingWithOptions 之后几秒钟调用应用程序 openURL


                                            <p><p>我的应用程序中有一个深层链接功能,在一种情况下运行良好。
根据打开应用程序的网址,我有 3 个不同的入职页面。
因此,当应用程序启动时,我需要知道哪个链接(如果有)打开了应用程序,然后显示正确的入职页面。问题是我需要知道该方法中要显示的屏幕:</p>

<pre><code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
</code></pre>

<p>但我只能知道深层链接是否打开了应用程序</p>

<pre><code>- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
</code></pre>

<p>在 <code>didFinishLaunchingWithOptions</code> 被调用 5 秒后被调用(我计算了秒数)。所以我有 5 秒钟的时间看到一个错误的入职页面,直到调用 <code>openURL</code>(如果它会被调用)。</p>

<p>所以我的问题是:有没有办法知道应用程序是在 <code>didFinishLaunchingWithOptions</code> 之前还是期间从 url 启动的?</p>

<p>顺便说一下,当应用从深层链接打开时,<code>didFinishLaunchingWithOptions</code> 中的 <code>launchOptions</code> 为 nil</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您正在寻找的启动选项键是 <strong><code>UIApplicationLaunchOptionsURLKey</code></strong> (Objective-C)/<strong><code>UIApplicationLaunchOptionsKey.url</code></strong> ( swift )。<br/>
如果您的目标是 iOS 9 及更高版本,您只需拦截来自 </p> 的启动 URL

<ul>
<li><code>application:didFinishLaunchingWithOptions:</code>(以防应用尚未在内存中)</li>
<li><code>application:openURL:options:</code>(如果应用程序已经在后台)。 </li>
</ul>

<p>这是 <code>UIApplicationDelegate</code> 的简约实现,它应该涵盖这两种情况 - 请注意,为了清楚起见,已经省略了很多不相关的逻辑:</p>

<p> objective-C :
</p>

<pre><code>@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSURL *url = launchOptions;
    if (url) {
      // TODO: handle URL from here
    }

    return YES;
}

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary&lt;UIApplicationOpenURLOptionsKey,id&gt; *)options {

    // TODO: handle URL from here

    return YES;
}

@end
</code></pre>

<p> swift5:
</p>

<pre><code>@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: ?) -&gt; Bool {

      if let url = launchOptions?[.url] as? URL {
            // TODO: handle URL from here
      }

      return true
    }

    func application(_ app: UIApplication, open url: URL, options: = [:]) -&gt; Bool {

      // TODO: handle URL from here

      return true
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 didFinishLaunchingWithOptions 之后几秒钟调用应用程序 openURL,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42345586/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42345586/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 didFinishLaunchingWithOptions 之后几秒钟调用应用程序 openURL