0

私は天気アプリケーションを構築しています。ユーザーが (ホ​​ーム画面の 3D Touch を介して) ショートカット メニューをアクティブにしたときに天気データ (気温など) を表示できるようにしたいと考えています。ユーザーが温度を確認するためにアプリケーションに入る必要がないように、気象データがショートカットに表示されるようにしたいと思います。気象データを取得するために使用されるコードは次のとおりです。必要に応じてさらにコードを投稿します。

struct ForecastService {
  let forecastAPIKey: String
  let forecastBaseURL: NSURL?
  init(apiKey: String) {
    forecastAPIKey = apiKey
    forecastBaseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")
  }

  func getForecast(lat: Double, long: Double, completion: (CurrentWeather? -> Void)) {
    if let forecastURL = NSURL(string: "\(lat),\(long)", relativeToURL: forecastBaseURL) {
        let networkOperation = NetworkOperation(url: forecastURL)

        networkOperation.downloadJSONFromURL {
            (let JSONDictionary) in
            let currentWeather = self.currentWeatherFromJSON(JSONDictionary)
            completion(currentWeather)
        }
    } else {
        print("Could not construct a valid URL")
    }
  }

  func currentWeatherFromJSON(jsonDictionary: [String: AnyObject]?) -> CurrentWeather? {
    if let currentWeatherDictionary = jsonDictionary?["currently"] as? [String: AnyObject] {
        return CurrentWeather(weatherDictionary: currentWeatherDictionary)
    } else {
        print("JSON Dictionary returned nil for 'currently' key")
        return nil
    }
  }
}//end struct
4

1 に答える 1

3

タイトルを表示したい気象条件に設定してUIApplicationShortcutItemを作成し、アプリケーションのShortcutItemsをその項目を含む配列に設定する必要があります。例えば:

let item = UIApplicationShortcutItem(type:"showCurrentConditions", localizedTitle:"64° Sunny")
UIApplication.sharedApplication().shortcutItems = [item]

「type」パラメーターは任意の文字列であることに注意してください。アプリのデリゲートは、ユーザーがショートカットを選択したときにそれを認識できる必要があります。

于 2015-12-08T23:31:25.533 に答える