27

I'm noticing strange behavior in Safari recently.

I register a url scheme for the my app, and enter myapp:// into Safari. This launches my app immediately.

Then I go back to Safari, and enter myapp:// into Safari again, this time it prompts me "Open this page in "myapp"?" Cancel or Open.

My app will launch if I tap on open, and subsequent attempts the same alert shows. If I try tapping on cancel, my app will not launch. which is expected.

However, if I enter myapp:// into the URL bar again, I'm prompted "Cannot Open Page" "Safari cannot open the page because the address is invalid."

This will fail in the same way for all subsequent attempts, until I kill Safari and re-start it, or open another tab.

This is the same behavior with Youtube and Evernote. my guess is that Safari cached the URL as an invalid URL when the User taps on cancel. Is there official documentation on this behavior?

Bbserved in iOS 8.1.2 and iOS 6.1.3

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
tzl
  • 1,540
  • 2
  • 20
  • 31
  • Any update on this? I can't seem to find any decent way to re-enable my app URL scheme.I had to reset the device settings to make it work again :-( – momo Apr 20 '15 at 07:57
  • i had a same problem, make sure in the info.plist you should have written "appurl" not "appurll://" – Amitg2k12 Apr 30 '15 at 11:52

6 Answers6

13

In 9.1 the issue still exists. The solution for me is just restarting safari (swipe up to clear it from background).

Jinghan Wang
  • 1,139
  • 12
  • 21
  • Restarting Safari didn't work for me. After restarting the problem comes again if user presses cancel. Did it get fixed for you after restarting ? – Chirag Bhutani Jun 23 '16 at 08:01
  • @ChiragBhutani It's a consistent bug. What I meant was the prompt would come out if you restart Safari. But if the user keep pressing cancel, of course it would fail again. – Jinghan Wang Jun 23 '16 at 08:22
  • 2
    I have noticed one more thing. If I host a simple html page on my server with a button redirecting to url scheme then it works fine even if I press cancel but if i use javascript in my webpage then after pressing cancel the problem occurs. Any idea regarding this? – Chirag Bhutani Jun 23 '16 at 11:38
  • Still happens on iOS 12.0. – Jonny Dec 11 '18 at 02:06
  • Still happens on iOS 15.5. :-( – Giel Berkers Aug 30 '22 at 13:54
4

I had the same problem. Once cancelled, it would give that error.

What I did was sending an extra parameter with a timestamp, so Safari would not cache it. So after the last param, I added a foo param with the number of milliseconds since midnight January 1, 1970. I use as3, but this should be readable for all developers:

var foo:Number = new Date().time; //The number of milliseconds since midnight January 1, 1970
var urlRequest:URLRequest = new URLRequest(url+"&foo="+foo);
fantaghirocco
  • 4,761
  • 6
  • 38
  • 48
APvG
  • 53
  • 7
  • nice idea. maybe I'll try this some time – tzl Jul 20 '16 at 06:50
  • 1
    The OAuth2 `state` parameter can also serve this purpose, since it should normally be constructed with a unique value that can help prevent forgery attacks. – Mark Larter Aug 30 '17 at 01:52
2

relaunching the safari app, or opening a new tab solved this problem

tzl
  • 1,540
  • 2
  • 20
  • 31
0

When you call your url add a unique value such as timeStamp to your url call

double currentt = [[NSDate new] timeIntervalSince1970];
NSTimeInterval differ= [[NSDate dateWithTimeIntervalSince1970:currentt] timeIntervalSinceDate:[NSDate dateWithTimeIntervalSince1970:1296748524]];
NSLog(@"differ: %f", differ);
NSString *url =[NSString stringWithFormat: @"https://thisisawebsite&timestamp=%f", differ];

Will always then see the popup until you click "Okay"

HannahCarney
  • 3,441
  • 2
  • 26
  • 32
0

Adding following code in AppDelegate solved my problem, hope it works for you too.

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    let notification = Notification(name: Notification.Name(rawValue: "AppNotificationLaunchString"), object: nil, userInfo: [UIApplicationLaunchOptionsKey.url:url])
    NotificationCenter.default.post(notification)
    return true
}
Himani C.
  • 84
  • 6
0

My observation is that it seems to be a setting associated with the Safari tab. If you open a new tab, the url scheme works.

Cancel disables the url scheme and saves the setting into the tab. If you swipe away Safari or even restart the phone, Safari will restore the tab and it still won't handle the scheme. However the url scheme works if you open a new tab.

I suppose that for consistent behavior, you would want to somehow get a new tab opened before the url scheme is used.

UrbanMetro
  • 123
  • 1
  • 9