Method 'application:openURL:options:' is n

2020-01-24 10:01发布

I'm trying to open my app from a web page using custom schemes. The app is opened but the following method is not called:

func application(_ app: UIApplication, open url: URL, options [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    // This is not called
}

My info.plist looks like the following:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>MyApp</string>
            </array>
            <key>CFBundleURLName</key>
            <string>url here</string>
        </dict>
    </array>

The project is created with Xcode 11.1, and I'm testing on iOS 13.

7条回答
你好瞎i
2楼-- · 2020-01-24 10:29

With the latest SDK, this does work fine if you are at NOT using SceneDelegate.

If you are using sceneDelegate the the following AppDelegate method is not called and therefore the login cannot be handled.

func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let handled = ApplicationDelegate.shared.application(
        application,
        open: url,
        sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
        annotation: options[UIApplication.OpenURLOptionsKey.annotation])
    return handled
}

This is because, this method is (understandably) deferred to the following method in the SceneDelegate:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    ...
}

The solution which I can confirm as working for iOS 13 applications implementing a SceneDelegate is:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url else {
        return
    }
    let _ = ApplicationDelegate.shared.application(
        UIApplication.shared,
        open: url,
        sourceApplication: nil,
        annotation: [UIApplication.OpenURLOptionsKey.annotation])        
}
查看更多
女痞
3楼-- · 2020-01-24 10:30

Thanks to @Matt there This is how I solve the problem.

on SceneDelegate.m

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {

    NSURL *url = connectionOptions.URLContexts.allObjects.firstObject.URL;
    NSLog(@"When app is not on memory  ::::: %@",url);

}

​
- (void)scene:(UIScene *)scene openURLContexts:(NSSet *)URLContexts {

    NSURL *url = URLContexts.allObjects.firstObject.URL;    
    NSLog(@"When app is on memory ::::: %@",url);

}
查看更多
▲ chillily
4楼-- · 2020-01-24 10:32

Refer this apple documentation

This method is not called if your implementations return NO from both the application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods. (If only one of the two methods is implemented, its return value determines whether this method is called.) If your app implements the applicationDidFinishLaunching: method instead of application:didFinishLaunchingWithOptions:, this method is called to open the specified URL after the app has been initialized. If a URL arrives while your app is suspended or running in the background, the system moves your app to the foreground prior to calling this method. There is no equivalent notification for this delegation method.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-01-24 10:38

I encountered a problem, using safari shortcut to start the application, openURLContexts callback multiple times.

2019-12-09 18:33:41.257088+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a71c0; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd4750; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
2019-12-09 18:33:42.022132+0800 OCTEST[5019:1468254] openURLContextsopenURLContexts {(
    <UIOpenURLContext: 0x2805a6d40; URL: appoctest://action=123123123; options: <UISceneOpenURLOptions: 0x280bd6f70; sourceApp: (null); annotation: (null); openInPlace: NO>>
)}
查看更多
狗以群分
6楼-- · 2020-01-24 10:40

Here is the method in SceneDelegate.swift. Swift version For iOS13

@available(iOS 13.0, *)
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url {
        // Handle URL
        WXApi.handleOpen(url, delegate: AppDelegate.shared)
    }
}
查看更多
Juvenile、少年°
7楼-- · 2020-01-24 10:44

Implement scene(_:openURLContexts:) in your scene delegate.

If the URL launches your app, you will get scene(_:willConnectTo:options:) instead and it’s in the options.

查看更多
登录 后发表回答