Article From:https://www.cnblogs.com/CoderHong/p/9062275.html

overview

A common scenario involves jump scenarios between applications.

  • Social sharing
  • Alipay, WeChat to pay
  • Link jump to application

The essence of jump is applied in iOS: to open an application, you only need to get the URL of the corresponding application.

What is URL?

URL(The unified resource locator is composed of protocol header (scheme) + resource path.

  • Protocol header: determine the way to find resources, such as http://, https://, file://
  • Resource path: path path

Note: an effective URL must have a protocol header and no resource path. That is to say, as long as we know that an application protocol header can jump to the application.

The default application of iOS is that there is no URL and we need to configure it.

iOS to achieve application jump”

The first step is to configure the URL for the jump application. Other applications can jump to the application through the URL. In TARGETS-> Info-> URL Types configuring URL, you can configure only protocol heads.

If other applications jump to the application, you can jump through the openURL: method of UIApplication.

UIApplication *application = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:@"wechat://"];
if ([application canOpenURL:url]) {
    [application openURL:url];
}else{
    NSLog(@"Can't open WeChat application ");}

At the same time, we can also open the application by clicking the link, for example, entering wechat:// in Safari browser can also jump.

It is not enough to jump through the above configuration and code, the console will output when jumping.

Reason: iOS 9 system policy updates, restricted access to the HTTP protocol, in addition to the application of the "Info.plist" to use the URL Schemes column as white list, can the normal check whether other applications are installed.
For example, if you want to jump to B application by applying A application, you need to list URL Schemes of B application as white list in A application Info.plist.

        <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
        <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>wechat</string>
        </array>

This enables the jump between applications.

iOS” to achieve the application jump to the specified page.

We share scenes in App and share WeChat friends with WeChat friends. This requires the design of the jump application specified page. There is a need to use the proxy method of UIApplication.
At present, there are three proxy methods. When the application is turned back to the callback method by other applications through the openURL method, the first two have expired.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options

Judging by the URL resource path carrying parameters

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    NSString *urlString = url.absoluteString;
    NSLog(@"urlStr=%@", urlString);
    
    UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
    // 1.First, let the controller in the stack of the navigation controller pop solve multiple jumps. The last push page has no pop. Once again, push appears and needs to return multiple times.[rootNav popToRootViewControllerAniMated:NO];UIViewController *rootVC = [rootNav childViewControllers].firstObject;If ([urlString containsString:@ "timeline")) {[rootVC performSegueWithIdentifier:@ "HomeToTimEline "sender:nil];NSLog (@ "jump to the circle of friends");}else if ([urlString containsString:@ "session"]) {[rootVC performSegueWithIdentifier:@ "HomeToSession" sender:nil];NSLog (@ "jump to WeChat friends");}Return YES;}

Leave a Reply

Your email address will not be published. Required fields are marked *