我有两个测试应用:App1 和 App2。
App1 从文本字段中获取字符串并触发方法:
@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,它只有标签,应该更改为通过 url 发送的文本,代码在 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 结果传递给实际标签的行给我这个错误:
EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)
但是我可以肯定地在 App2 中拥有所有数据,因为我可以在调试器中看到它们的值:
url NSURL "app2://com.application.started?displayText=564315712437124375" 0x00007fa4e3426320
query String? "displayText=564315712437124375"
知道我为什么会收到此错误吗?
谢谢...
最佳答案
你的错误
ViewController().labelToDisplayResult.text = query
ViewController()
创建一个新的 ViewController
实例,而不是从 Storyboard加载的实例。我猜 labelToDisplayResult
是一个导出,所以它是零,所以你得到 EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)
这是我平时处理openURL scheme的方式,需要考虑两种状态:
- 目标应用已经启动,所以当打开url时,目标应用处于后台或非事件状态
- 目标应用没有启动,所以当打开url时,目标应用根本没有运行
在 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
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)
}
}
关于ios - iOS 中使用 URL 方案的应用程序间通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32824528/