Can imessage extension open host app?

扶醉桌前 提交于 2020-01-14 19:08:00

问题


I've tried [self.extensionContext openURL:completion], but my app crashed. I heard some extensions can't use this method, can iMessage extensions?

By the way, can host app activate its iMessage extension?


回答1:


Share Extensions and Action Extensions are not designed to function as app launchers.

App Extension Programming Guide

There is no direct communication between an app extension and its containing app; typically, the containing app isn’t even running while a contained extension is running. An app extension’s containing app and the host app don’t communicate at all.

The extension displays a user interface, performs some work, and, if appropriate for the extension’s purpose, returns data to the host.

There is limited interaction available between an app extension and its containing app. A Today widget (and no other app extension type) can ask the system to open its containing app by calling the openURL:completionHandler: method of the NSExtensionContext class. "

A work around derived from this SO question.

Working solution (tested on iOS 9.2) for Keyboard Extension.

This category adds special method for access to hidden sharedApplication object and then call openURL: on it. (Of course then you have to use openURL: method with your app scheme.)

// Valentin Shergin

extension UIInputViewController {

func openURL(url: NSURL) -> Bool {
    do {
        let application = try self.sharedApplication()
        return application.performSelector("openURL:", withObject: url) != nil
    }
    catch {
        return false
    }
}

func sharedApplication() throws -> UIApplication {
    var responder: UIResponder? = self
    while responder != nil {
        if let application = responder as? UIApplication {
            return application
        }

        responder = responder?.nextResponder()
    }

    throw NSError(domain: "UIInputViewController+sharedApplication.swift", code: 1, userInfo: nil)
}

}



回答2:


self.extensionContext?.open(url, completionHandler: nil) doesn't work in my iMessage extension.

tymac answer is correct, however openURL has been moved to open in Swift 3.

I found a simpler solution in this thread : openURL not work in Action Extension

extension UIViewController {

  func openURL(url: URL) {
    var responder = self as UIResponder?

    while (responder != nil){
      if responder?.responds(to: Selector("openURL:")) == true{
        responder?.perform(Selector("openURL:"), with: url)
      }
      responder = responder!.next
    }
  }

}



回答3:


This is apparently not working in Seed 2 but should be fixed in Seed 3.

https://forums.developer.apple.com/message/153441#153441




回答4:


This works fine for me:

NSString *customURL = @"myHostAppName://";

    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
    } else {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"URL error" message:[NSString stringWithFormat:@"No custom URL defined for %@", customURL] preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:ok];

        [self presentViewController:alertController animated:YES completion:nil];
    }

Remember re-install the host app again.

Take in consideration How to open our app from iMessage




回答5:


[self.extensionContext openURL:YOUR_NSURL completionHandler:nil];

it would be nice if you can pass deeplinks to open specific screen in app



来源:https://stackoverflow.com/questions/38319433/can-imessage-extension-open-host-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!