【问题标题】:Inter-app communication in iOS using URL schemeiOS 中使用 URL 方案的应用程序间通信
【发布时间】:2015-12-25 18:51:41
【问题描述】:

我有两个测试应用: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"

知道为什么会出现此错误吗?

谢谢...

【问题讨论】:

    标签: ios swift url fatal-error


    【解决方案1】:

    你的错误

    ViewController().labelToDisplayResult.text = query
    

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

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

    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 和 Swift 开发都很陌生,如果我想显示通过 URL 方案传递的值,如何从 AppDelegate 获取值到 ViewController?
    • 这会引发错误,但无论如何谢谢你,非常好的一点,我需要考虑这两种状态。
    • 对不起,我的错,它没有抛出错误,它实际上就像一个魅力......有我自己的错误。非常感谢!
    • 能否请您解释一下或在 NSNotificationCenter 部分添加一些 cmets?谢谢
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多