使用URL方案在iOS中进行应用程序间通信 [英] Inter-app communication in iOS using URL scheme

查看:142
本文介绍了使用URL方案在iOS中进行应用程序间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个测试应用程序:App1& App2。

I have two test apps: App1 & App2.

App1从文本字段中获取字符串并触发方法:

App1 takes String from the text field and triggers method:

@IBAction func openApp(sender: AnyObject) { 
    let url1 = ("app2://com.application.started?displayText="+textToSend.text!)
    let url2 = url1.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
    UIApplication.sharedApplication().openURL(NSURL(string: url2!)!)
}

实际打开的App2只有标签,应该更改为通过网址发送的文本,代码在AppDelegate.swift中:

Which actually opens App2 which has only label which should change to the text sent through the url, code is within AppDelegate.swift:

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    let url = url.standardizedURL
    let query = url?.query
    ViewController().labelToDisplayResult.text = query
    return true;
}

不幸的是,我试图将URL的结果传递给实际标签给我这个错误:

Unfortunately the line where I am trying to pass result of the URL to the actual label is giving me this error:

EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)

然而,我确实拥有App2中的所有数据,因为我可以在调试器中看到它们的值:

However I have all the data in the App2 for sure as I can see their values in debugger:

url NSURL   "app2://com.application.started?displayText=564315712437124375" 0x00007fa4e3426320
query   String? "displayText=564315712437124375"

我知道为什么会收到此错误?

Any idea why I am getting this error?

谢谢......

推荐答案

您的错误

ViewController().labelToDisplayResult.text = query

ViewController()创建 ViewController 的新实例,而不是从storyboard加载的实例。我猜 labelToDisplayResult 是一个插座,所以它是零,所以你得到 EXC_BAD_INSTRUCTION(CODE = EXC_I386_INVOP SUBCODE = 0x0)

ViewController() Create a new instance of ViewController,not the one loaded from storyboard.I guess labelToDisplayResult is an outlet, so it is nil,so you get EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)

这是我通常做的处理openURL方案,需要考虑两种状态:

This is what I usually do to handle openURL scheme,need to think about two states:


  1. 目标应用程序之前启动,因此当打开URL时,目标应用程序处于后台或非活动状态

  2. 目标应用程序未启动,因此当打开URL时,目标应用程序未运行所有

在Appdelegate中

In Appdelegate

class AppDelegate: UIResponder, UIApplicationDelegate {
var openUrl:NSURL? //This is used when to save state when App is not running before the url trigered
var window: UIWindow?


func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    let url = url.standardizedURL
    NSNotificationCenter.defaultCenter().postNotificationName("HANDLEOPENURL", object:url!)
    self.openUrl = url
    return true;
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    return true
}
}

然后在ViewController句柄中openURL

Then in the ViewController handle openURL

class ViewController: UIViewController {

@IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
    super.viewDidLoad()
     NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleOpenURL:", name:"HANDLEOPENURL", object: nil)
    let delegate = UIApplication.sharedApplication().delegate as? AppDelegate
    if let url = delegate?.openUrl{
       testLabel.text = url.description 
        delegate?.openUrl = nil 
    }
}
func handleOpenURL(notification:NSNotification){
    if let url = notification.object as? NSURL{
        testLabel.text = url.description
    }
}
deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self, name: "HANDLEOPENURL", object:nil)
}

}

这篇关于使用URL方案在iOS中进行应用程序间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆