简体   繁体   English

当我的应用设置为不在后台运行时,是否可以在URL方案中获取参数?

[英]Can I possibly get the parameter in URL Scheme when my app set to not run in background?

I am trying to open my app using URL Scheme with a parameter on it, my problem is, is it possible to get the parameter when the app is set to not run in the background. 我正在尝试使用带有参数的URL方案打开我的应用程序,我的问题是,当应用程序设置为不在后台运行时,是否可以获取参数。

SampleApp-info.plist SampleApp-info.plist

  <key>UIApplicationExitsOnSuspend</key>
        <true/>

AppDelegate.m AppDelegate.m

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
{

    NSLog(@"url recieved: %@", url);
    NSLog(@"scheme: %@", [url scheme]);
    NSLog(@"query string: %@", [url query]);
    NSLog(@"host: %@", [url host]);

    return YES;
}

When UIApplicationExitsOnSuspend is set to true and try to open my app from Safari, it's not creating any logs but when I remove the key it is working well. 当UIApplicationExitsOnSuspend设置为true并尝试从Safari打开我的应用程序时,它没有创建任何日志,但是当我删除密钥时,它运行良好。 I have to restart my app to load the first viewcontroller and suspending it on the background is the best way, I think... 我认为我必须重新启动应用程序才能加载第一个ViewController,并将其挂起在后台是最好的方法。

Can anyone help me on this? 谁可以帮我这个事? or a better way so I can reload my app starting in the first viewcontroller? 还是更好的方法,以便我可以从第一个ViewController开始重新加载我的应用程序? Newbie here. 新手在这里。

I really appreciate any help. 我非常感谢您的帮助。 Thank you and regards. 谢谢和问候。

UIApplicationExitsOnSuspend Settings UIApplicationExitsOnSuspend设置

You have to set UIApplicationExitsOnSuspend to false . 您必须将UIApplicationExitsOnSuspend设置为false URL Scheme Callback will not work with UIApplicationExitsOnSuspend set to true . URL方案回调在UIApplicationExitsOnSuspend设置为true时不起作用。 If you want to have a simular behavior like UIApplicationExitsOnSuspend set to true just add the following code 如果要将UIApplicationExitsOnSuspend这样的模拟行为设置为true,只需添加以下代码

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [super applicationDidEnterBackground:application];
    exit(0);
}

First you have to make an entry in in Info.plist 首先,您必须在Info.plist中进行输入

eg entry your URL Scheme. 例如,输入您的URL方案。 In this case (in the picture) the callback will be blurry:// the identifier should be normally unique 在这种情况下(如图),回调将是模糊的://标识符通常应该是唯一的

信息清单

Callback Implementation 回调实现

For Pre iOS 9 swift 对于iOS 9之前的版本

optional func application(_ application: UIApplication,
            handleOpenURL url: NSURL) -> Bool {
    NSLog("URL is %@", url)
    return true
}

For Pre iOS 9 objective-c 对于iOS 9之前的Objective-c

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    // handler code here
    NSLog(@"Url is %@, url);
    return YES;
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    // handler code here
    NSLog(@"Url is %@, url);
    return YES;
}

IOS 9 objective-c iOS 9 Objective-C

- (BOOL)application:(UIApplication *)app
        openURL:(NSURL *)url
        options:(NSDictionary<NSString *,
                         id> *)options {
    NSLog(@"Url is %@", url);
    return YES;
}

IOS 9 - Swift iOS 9-迅捷

optional func application(_ app: UIApplication,
              openURL url: NSURL,
              options options: [String : AnyObject]) -> Bool {
   NSLog("URL is", NSURL)
   return true
}

Test the callback 测试回调

Just open the weburl in the example case blurry://test with the mobile safari on your Handy 只需在您的Handy上使用移动Safari的示例案例blurry:// test中打开weburl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以通过发短信给我的应用程序发送URL方案吗? - Can I send a URL Scheme to my App by texting someone? 当应用程序使用URL方案在iOS中打开我的应用程序时,如何获取呼叫者的信息 - How to get the caller's info When a app using URL Scheme to open my App in iOS 我的应用程序正在由 iOS 中的 URL 方案打开,如何获取完整的 URL? - My app is being opened by a URL scheme in iOS, how do I get the complete url? 当应用未在后台运行时,自定义URL方案不起作用 - custom URL scheme does not work when app is not running in background 如果当 iOS 应用程序在后台时 NSTimer 不起作用,我怎样才能让我的应用程序定期唤醒? - If NSTimer does not work when an iOS app is in the background, how can I get my app to wake up periodically? 如何启动打开自定义URL方案的应用程序? - How can I launch back the app that opened my custom URL scheme? 我可以运行将我的应用程序置于后台的KIF测试吗? - Can I run a KIF test that puts my app in the background? 我的应用会回复网址方案吗? - will my app respond to a url scheme? 如何使用URL方案向特定视图打开我的应用程序? - How can my app be opened to a specific view with URL scheme? 另一个应用程序可以注册我的自定义URL方案 - Can another app register my custom url scheme
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM