`
haoningabc
  • 浏览: 1444474 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ios的helloworld 2

    博客分类:
  • ios
ios 
阅读更多
参考视频:
www.imooc.com/learn/149
Swift Weather app
使用xcode6_beta
1.create a new xcode project
single View Application
language选swift
选下面匹配iphone w为compact
如果删掉viewController新建的时候一定要右边第三个
CostumClass 的class选ViewController

拖拽两个label和一个ImageView
关掉xcode
2. 到项目目录下创建Podfile
➜  first git:(master) ✗ cat ../hello/Podfile
platform :ios,'7.0'
pod 'AFNetworking'

pod install
如果pod没安装
sudo gem install cocoapods

open .
发现多了一个工程文件
用xcode打开,pod就引用AFNetworking了

3 . supporting files
下面的info.plist加两个变量
NSLocationAlwaysUsageDescription  我们需要使用你的地理位置
NSLocationUsageDescription 我们需要使用你的地理位置

4. 新建头文件
为了swift调用objective-c的代码
SwiftWeather-Bridging-Header.h
内容为
#import <AFNetWorking/AFNetWorking.h>

并点项目
build Setting中
搜bridging
Objective-C Bridging Header
设置为
first/SwiftWeather-Bridging-Header.h

5 .ViewController.swift
内容为
//
//  ViewController.swift
//  first
//
//  Created by xiao7 on 14-9-9.
//  Copyright (c) 2014年 killinux. All rights reserved.
//

import UIKit
import CoreLocation

class ViewController: UIViewController ,
    CLLocationManagerDelegate{
    let locationManager:CLLocationManager = CLLocationManager()
    override func viewDidLoad() {
        println("aa");
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        
        self.loadingIndicatorView.startAnimating()
        
        let background = UIImage(named: "tubiao2.png")
        self.view.backgroundColor = UIColor(patternImage:background)
        
        if(ios8()){
            locationManager.requestAlwaysAuthorization()
        }
        locationManager.startUpdatingLocation()

    }
    
    @IBOutlet var location : UILabel
    
    @IBOutlet var temperature : UILabel
    @IBOutlet var icon : UIImageView
    @IBOutlet var loadingIndicatorView : UIActivityIndicatorView
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
  
    func ios8() ->Bool {
        return UIDevice.currentDevice().systemVersion == "8.0"
    }
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!){
        var location:CLLocation = locations[locations.count-1] as CLLocation
        if(location.horizontalAccuracy > 0){
            println(location.coordinate.latitude)
            println(location.coordinate.longitude)
            self.updateWeatherInfo(location.coordinate.latitude,longitude:location.coordinate.longitude)
            locationManager.stopUpdatingLocation()
        }
    }
    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){
        println("haoning print error")
        println(error)
    }
    func updateWeatherInfo(latitude:CLLocationDegrees,longitude:CLLocationDegrees){
        let manager = AFHTTPRequestOperationManager()
        let url = "http://api.openweathermap.org/data/2.5/weather"
        //http://api.openweathermap.org/data/2.5/weather?lat=37.8136&lon=144.96&cnt=0
        let params = ["lat":latitude,"lon":longitude,"cnt":0]
        println("-----")
        println(latitude)
        manager.GET(url,
            parameters:params,
            success:{
                (operation:AFHTTPRequestOperation!,responseObject: AnyObject!) in println("JSON: "+responseObject.description!)
                self.updateUISuccess(responseObject as NSDictionary!)
            },
            failure:{
                (operation:AFHTTPRequestOperation!,error:NSError!) in println("Error:" + error.localizedDescription)
            }
            
        )
    }
    func updateUISuccess(jsonResult:NSDictionary!){
        self.loadingIndicatorView.stopAnimating()
        self.loadingIndicatorView.hidden = true
        if let tempResult = jsonResult["main"]?["temp"]? as? Double{
            var thistemperature: Double
            if (jsonResult["sys"]?["country"]? as String == "US"){
                thistemperature = round(((tempResult - 273.15) * 1.8) + 32)
            }else{
                thistemperature = round(tempResult - 273.15)
            }
            println("temperature----->")
            println(thistemperature)
            self.temperature.text = "\(thistemperature)."
            self.temperature.font = UIFont.boldSystemFontOfSize(60)
            
            var name = jsonResult["name"]? as String
            self.location.font = UIFont.boldSystemFontOfSize(25)
            self.location.text = "\(name)"
            println("name----->")
            println(name)
            var condition = (jsonResult["weather"]? as NSArray)[0]?["id"]? as Int
            var sunrise = jsonResult["sys"]?["sunrise"]? as Double
            var sunset = jsonResult["sys"]?["sunset"]? as Double
            
            var nightTime = false
            var now = NSDate().timeIntervalSince1970
            if(now < sunrise || now > sunset){
                nightTime = true
            }
            println("nightTime----->")
            println(nightTime)
            self.updateWeatherIcon(condition,nightTime: nightTime)
        }else{
            
        }
    }
    func updateWeatherIcon(condition: Int, nightTime: Bool){
        
        if nightTime{
            self.icon.image = UIImage(named:"tubiao1")
        }else{
            self.icon.image = UIImage(named:"tubiao2")
        }
    }



}






6. 其中@IBOutlet var location : UILabel
需要
点main.storyboard
按住option 再点ViewController.swift
同时打开两个页面后再
点住control 拖拽 main.storyboard中点标识到ViewController中
  • 大小: 26.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics