偏好不工作ios手表

时间:2017-09-26 11:45:33

标签: ios iphone swift apple-watch

我开发了一款ios手表应用程序。我想将值传递给ios手表。我尝试了几种方法。在ios app的元素中存储值时。我使用的是xcode 8,代码是用swift编写的。

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var webview: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("webview")
        let url = URL (string: "http://www.google.com/")
        let request = URLRequest(url: url!)
        webview.loadRequest(request)

        let preferences = UserDefaults.standard
        let currentLevel = "cl"
        let currentLevelKey = "currentLevel"
        preferences.set(currentLevel, forKey: currentLevelKey)
        preferences.synchronize()
        print("pref")
    }

}

然后我在ios watch app中调用了首选项代码 的 InterfaceController.swift

class InterfaceController: WKInterfaceController {

    @IBAction func clickingTheButton() {
        print("clicked")
        let preferences = UserDefaults.standard
        let currentLevelKey = "currentLevel"

        if preferences.object(forKey: currentLevelKey) == nil {
            // Doesn't exist
            print("Doesn't exist")
        } else {
            let currentLevel = preferences.integer(forKey: currentLevelKey)
            print("preference : ",currentLevel)
        }
    }

}

如何从ios传递价值观看应用。请帮帮我。

0 个答案:

没有答案