-->

iOS Safari does not recognize url schemes after us

2020-06-01 00:41发布

问题:

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

回答1:

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



回答2:

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);


回答3:

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



回答4:

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"



回答5:

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
}