首先构造出基本界面,使用interface Builder,
注意:如果使用的是ios8 的同学记得要在右边设置属性栏目中,取消use size classes用不好会出现view异常
一、什么是CocoaPods
1、为什么需要CocoaPods
在进行iOS开发的时候,总免不了使用第三方的开源库,比如SBJson、AFNetworking、Reachability等等。使用这些库的时候通常需要:
- 下载开源库的源代码并引入工程
- 向工程中添加开源库使用到的framework
- 解决开源库和开源库以及开源库和工程之间的依赖关系、检查重复添加的framework等问题
- 如果开源库有更新的时候,还需要将工程中使用的开源库删除,重新执行前面的三个步骤,顿时头都大了。。。
自从有了CocoaPods以后,这些繁杂的工作就不再需要我们亲力亲为了,只需要我们做好少量的配置工作,CocoaPods会为我们做好一切!
2、什么是CocoaPods
CocoaPods是一个用来帮助我们管理第三方依赖库的工具。它可以解决库与库之间的依赖关系,下载库的源代码,同时通过创建一个Xcode的workspace来将这些第三方库和我们的工程连接起来,供我们开发使用。
使用CocoaPods的目的是让我们能自动化的、集中的、直观的管理第三方开源库。
二、安装CocoaPods
1、安装
CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境。幸运的是OS X系统默认的已经可以运行Ruby了,因此我们只需要执行以下命令:
- $ sudo gem install cocoapods
CocoaPods是以Ruby gem包的形式被安装的。在安装执行的过程中,可能会问我们是不是更新rake,输入y即可。这是因为rake gem包会在安装的过程中检查更细,如果有可用的新版本就会出现刚才的选项。
在安装进程结束的时候,执行命令:
- $ pod setup
如果没有报错,就说明一切安装就成功了!
改写viewController.swift文件
代码如下
// // ViewController.swift // SwiftWeather // // Created by srain on 14-12-14. // Copyright (c) 2014年 srain. All rights reserved. // import UIKit import CoreLocation //实现地理位置的对象 class ViewController: UIViewController,CLLocationManagerDelegate { //初始化CLLocationManager let locationManager:CLLocationManager = CLLocationManager() @IBOutlet weak var location: UILabel! @IBOutlet weak var icon: UIImageView! @IBOutlet weak var temperature: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //副值给当前的类,这样就可以用回调函数了 locationManager.delegate = self //设置精度--最好的精度 locationManager.desiredAccuracy = kCLLocationAccuracyBest //增加判断分别支持iOS7 或者iOS8 if(ios8()){ locationManager.requestAlwaysAuthorization() } locationManager.startUpdatingLocation() } func ios8() -> Bool{ //获得设备系统版本信息 没有弄好版本!!! return UIDevice.currentDevice().systemVersion == "8.1" } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //地理位置信息改变会回调给我们信息,回传一个数组 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){ //获得CLLocation as 是强制转换 var location :CLLocation = locations[locations.count-1] as CLLocation if(location.horizontalAccuracy>0){ //取得天气信息 self.updateWeatherInfo(location.coordinate.latitude, longitude:location.coordinate.longitude) print(location.coordinate.latitude) print(location.coordinate.longitude) //得到一次就不再回调 locationManager.stopUpdatingLocation() } } //程序如果出错 func locationManager(manager: CLLocationManager!, didFinishDeferredUpdatesWithError error: NSError!){ print(error) } // func updateWeatherInfo(latitude:CLLocationDegrees,longitude:CLLocationDegrees) { let manager = AFHTTPRequestOperationManager() //免费的天气服务,支持全世界 let url = "http://api.openweathermap.org/data/2.5/weather" //需要一个字典类型传递数据给这个url let params = ["lat":latitude,"lon":longitude,"cnt":0] //提供的通信规则 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!){ //json 路径搜索 main 的属性 中temp 中的值 if let tempResult=jsonResult["main"]?["temp"]? as? Double{ var temperature:Double if(jsonResult["sys"]?["country"]? as String=="US"){ temperature = round(((tempResult-273.15)*1.8)+32) }else{ temperature = round(tempResult - 273.15) } //把获得的温度值放入显示中去 self.temperature.text="\(temperature)*" //把城市的名称付上去 var name = jsonResult["name"]? as String self.location.text = "\(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 } self.updateWeatherIcon(condition,nightTime:nightTime) }else{ } } //设置图片 func updateWeatherIcon(condition:Int,nightTime:Bool){ if(condition < 300){ self.icon.image = UIImage(named: "tstorm1_night") }else{ self.icon.image = UIImage(named: "tstorm1") } } }
问题2:
使用Storyboard时出现以下警告:
warning: Unsupported Configuration: Scene is unreachable due to lack of entry points and does not have an identifier for runtime access via -instantiateViewControllerWithIdentifier:
大意是为了在程序中动态访问Scene,需要给其设置一个Storyboard ID,
所以给出了警告,解决方法:设置一个Storyboard ID即可
问题3:
错误现象: XCODE 6.1 在使用storyboard时,
报错:Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard' - perhaps the designated entry point is not set?
原因分析:在StoryBoard中没有一个view controller设置了Initial Scene。
解决方案:在Storyboard中,选择一个view conroller作为story board的第一启动界面
如何调整模拟器的大小:
首先要做的是,如何调整这个模拟器的大小。
command +
R 模拟器运行后, 再按 command +1、 command+2、 command+3,
试试看。 你会发现
模拟器的大小发生了变化。