log*_*han 12 ios ios-app-extension today-extension share-extension
有没有人知道如何从应用扩展程序的视图控制器启动父应用程序?
我只想从其应用扩展程序启动主应用程序.
在WWDC会话中为iOS和OS X创建扩展,第1部分大约22分钟标记说使用openURL:completionHandler:UIViewController的extensionContext中的方法来打开自定义URL方案
[self.extensionContext openURL:[NSURL URLWithString:@"your-app-custom-url-scheme://your-internal-url"]
completionHandler:nil];
Run Code Online (Sandbox Code Playgroud)
它正在使用操作扩展在我当前的工作应用程序中工作
2-在扩展视图控制器中添加这两个功能
func openURL(url: NSURL) -> Bool {
do {
let application = try self.sharedApplication()
return application.performSelector(inBackground: "openURL:", with: 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?.next
}
throw NSError(domain: "UIInputViewController+sharedApplication.swift", code: 1, userInfo: nil)
}
Run Code Online (Sandbox Code Playgroud)
3-在您的按钮操作或您想要做的地方调用此函数
self.openURL(url: NSURL(string:"openPdf://HomeVC")!)
Run Code Online (Sandbox Code Playgroud)
这里 homevc 是应该显示的视图控制器的类名
4-在您的应用程序委托中实现类似的方法
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let urlPath : String = url.absoluteString
print(urlPath)
if urlPath.contains("HomeVC"){
//here go to firstViewController view controller
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "homeVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
Run Code Online (Sandbox Code Playgroud)
我希望能正常工作,还可以在扩展应用程序和父应用程序之间共享数据
Swift 3.1 中的工作解决方案(在 iOS10 中测试):
您需要为您的主机应用程序创建您自己的 URL Scheme,然后将此函数添加到您的 ViewController 并使用openURL("myScheme://myIdentifier")
// Function must be named exactly like this so a selector can be found by the compiler!
// Anyway - it's another selector in another instance that would be "performed" instead.
func openURL(_ url: URL) -> Bool {
var responder: UIResponder? = self
while responder != nil {
if let application = responder as? UIApplication {
return application.perform(#selector(openURL(_:)), with: url) != nil
}
responder = responder?.next
}
return false
}
Run Code Online (Sandbox Code Playgroud)
我在这里问了一个类似的问题:Communicating with/opening Containing app from Share extension。基本上,您可以做几件事。
\n\n使用 UIWebView 的 loadRequest webView 加载包含应用程序 URL 的 NSURL 请求。例如,
\n\nNSURL *url = [NSURL URLWithString:@"myurl"];\nNSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];\n[self.webView loadRequest:request];\nRun Code Online (Sandbox Code Playgroud)使用 UIDocumentInteractionController 和自定义文件扩展名类型提供指向您的包含应用程序的链接(并且仅包含您的包含应用程序)
启动“假”NSURL 会话以获得以下功能:在 iOS 中,如果您的扩展程序在后台任务完成时未运行\xe2\x80\x99,则系统会在后台启动您的包含应用程序并调用 application:handleEventsForBackgroundURLSession:completionHandler :应用程序委托方法。
第一个可能是您最好的选择。
\n| 归档时间: |
|
| 查看次数: |
8993 次 |
| 最近记录: |