ios - iOS 中使用 URL 方案的应用程序间通信

标签 ios swift url fatal-error

我有两个测试应用: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的方式,需要考虑两种状态:

  1. 目标应用已经启动,所以当打开url时,目标应用处于后台或非事件状态
  2. 目标应用没有启动,所以当打开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/

相关文章:

javascript - jQuery 将参数附加到 url

ios - 使用自定义单元格时摆脱额外的 UITableViewCell 分隔符

iOS - 在 Facebook 墙上发布是否需要审核?

iOS - CLLocation Manager didUpdateLocations 在一个类中被调用但在另一个类中没有被调用?

ios - 如何刷新 UIStackView 中控件的内容?

php - 使用 PHP 重写 URL

linux - grep 和 curl 命令

ios - 如何诊断和解决 WCSession sendMessage(_ :replyHandler:errorHandler:)?

iphone - OpenGL ES View : how to orient it to landscape?

ios - 如何获取 Parse.com 查询中的键列表?