0

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.

SampleApp-info.plist

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

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. I have to restart my app to load the first viewcontroller and suspending it on the background is the best way, I think...

Can anyone help me on this? or a better way so I can reload my app starting in the first viewcontroller? Newbie here.

I really appreciate any help. Thank you and regards.

Community
  • 1
  • 1
JeRa
  • 1
  • 4
  • Check the `launchOptions` dictionary in `application:didFinishLaunchingWithOptions:` for the key `UIApplicationLaunchOptionsURLKey`. – Avi Mar 03 '16 at 04:09
  • hi thanks for answering, how can I get the parameter value in there? – JeRa Mar 03 '16 at 04:17
  • Did you read the documentation? – Avi Mar 03 '16 at 04:19
  • I'm still not getting logs, Am I doing this correctly? `- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions UIApplicationLaunchOptionsURLKey:(NSString *)getURL { // Override point for customization after application launch. NSLog(@"test url: %@", getURL); return YES; }` – JeRa Mar 03 '16 at 05:06
  • That code makes no sense. Do you understand Objective-C? – Avi Mar 03 '16 at 06:44

1 Answers1

0

UIApplicationExitsOnSuspend Settings

You have to set UIApplicationExitsOnSuspend to false. URL Scheme Callback will not work with UIApplicationExitsOnSuspend set to true. If you want to have a simular behavior like UIApplicationExitsOnSuspend set to true just add the following code

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

First you have to make an entry in in Info.plist

e.g. entry your URL Scheme. In this case (in the picture) the callback will be blurry:// the identifier should be normally unique

Info Plist

Callback Implementation

For Pre iOS 9 swift

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

For Pre 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

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

IOS 9 - Swift

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

Kordi
  • 2,405
  • 1
  • 14
  • 13
  • wait did a copy mistake the callback should be other i will change it in one minute – Kordi Mar 03 '16 at 06:55
  • Did you make an entry in the plist and then with the code i wrote for ios9 the other should work, too but its deprecated. But you can test both methods. The plist is very important. – Kordi Mar 03 '16 at 07:01
  • I tried the pre ios 9 but unluckily didn't work still didn't created a log. I already set the url scheme on my app .plist and able to call it from safari before. Now, I'm having error on this part `options:(NSDictionary *)` saying `expected "<"`. What should I do here? – JeRa Mar 03 '16 at 07:09
  • @JeRa Is it a compile error. The code i posted for iOS9 should work (changed it). Just copy my code! – Kordi Mar 03 '16 at 07:30
  • @JeRa Now working. Tested again my code and its fine .. if you just copy it into your appdelegate – Kordi Mar 03 '16 at 07:38
  • @JeRa whats not working? Compile Error? Or just not called? Do you use Swift or objective-c App? – Kordi Mar 03 '16 at 07:50
  • on pre ios9, when I tried putting logs on it before, it didnt logged. on >= ios9 I'm still having 2 compile errors on this line `options:(NSDictionary'` and `expected ')'`. by the way, my iOs version is 7.0.3 and I'm using Objective-C. – JeRa Mar 03 '16 at 08:09
  • Its about the iOS Compile Target. Then use the pre IOS Version. And look if you perhaps have this function duplicated or so could you make a screenshot of your ide with the function and post it to imgur.com that i can look why something is not compiling ... – Kordi Mar 03 '16 at 08:10
  • @JeRa or try my newly update - openURL sourceApplication Variant. See Pre iOS9 - the second function – Kordi Mar 03 '16 at 08:17
  • I checked and there's no duplicated. tried your code and the url log works when I remove the `UIApplicationExitsOnSuspend ` from the .plist, but didn't work when there is... same scenario on my question up. – JeRa Mar 03 '16 at 08:22
  • @Jera So thats just the problem you have to set it to false. But is this a Problem? You could just exit by yourself if the application goes to background. Just make a exit in applicationDidEnterBackground: so then its nearly the same. So Updated my question for this case! – Kordi Mar 03 '16 at 08:49
  • @JeRa updated my post. Think thats a good Solution. Does with this Solution exit(0) in DidEnterBackground the callback working? – Kordi Mar 03 '16 at 08:56
  • got an error `No visible @interface for 'UIResponder' declares the selector 'applicationDidEnterBackground:' ? – JeRa Mar 03 '16 at 09:09
  • @Jera you added it to the ApllicationDelegate? – Kordi Mar 03 '16 at 09:13
  • yes, inside `- (void)applicationDidEnterBackground:(UIApplication*)application {}` – JeRa Mar 03 '16 at 09:14
  • but inside this just add exit(0) and thats all ... your using swift .. and then set UIApplicationExistOnSuspend to false – Kordi Mar 03 '16 at 09:17
  • still didn't got any logs. :/ even though I removed `UIApplicationExitsOnSuspend ` – JeRa Mar 03 '16 at 09:28
  • @JeRa But you said: I checked and there's no duplicated. tried your code and the url log works when I remove the UIApplicationExitsOnSuspend from the .plist, but didn't work when there is... same scenario on my question up. – Kordi Mar 03 '16 at 09:44
  • Yes I said that it worked and when I add `exit(0);` inside `- (void)applicationDidEnterBackground:(UIApplication*)application {}` on AppDelegate, it didn't got logs. – JeRa Mar 03 '16 at 09:55
  • It worked with empty applicationDidEnterBackground, too? I tested it myself there it works. But then just dont exit. Or do you have a reason, to not have your app in the background. – Kordi Mar 03 '16 at 09:57
  • yes it worked when I empty it. I am trying to restart my app so when safari calls the url scheme of my app it should loads the first viewcontroller, but if I dont suspend the app in background when I called back using the scheme it loads the previous viewcontroller not the first viewcontroller – JeRa Mar 03 '16 at 10:18