之前的写的“iPhone 手机控制linux服务器程序源码”(http://www.simonzhang.net/?p=2861 )。开启时间使用textfidld,太麻烦了。就保留常用的几个时间即可。所以改为使用pickerview控件。控制没有使用故事板,直接代码控制位置。源码如下,其他部分使用之前的代码。iphone6调试正常。
//
// door.swift
// mydoor
//
// Created by zhangzimeng on 16/1/6.
// Copyright © 2016年 com. All rights reserved.
//
import UIKit
import Alamofire
import Crypto
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
var timePicker:UIPickerView!
var buildString = ["3秒", "30分钟" , "1小时", "2小时", "3小时", "4小时"]
var buildInt = ["3", "1800" , "7200", "7200", "10800", "14400"]
let kk = "09999999999999"
let url = "http://www.simonzhang.net/door?"
@IBAction func dangdang(sender: AnyObject) {
// 获取要开发的时间
let row:Int = self.timePicker.selectedRowInComponent(0)
let getSec=buildInt[row]
// 获取当前两位小时和分
let Hour:NSDate = NSDate()
let Min:NSDate = NSDate()
let dateForm=NSDateFormatter()
dateForm.dateFormat="HH"
let getHour=dateForm.stringFromDate(Hour)
dateForm.dateFormat="mm"
let getMin=dateForm.stringFromDate(Min)
// 拼接要加密的穿
let ss=kk+getHour+getMin
let authMd5 = ss.MD5
// 拼接要访问的字符串
let doorUrl = url+"se="+getSec+"&au="+authMd5!
// 开始访问
var returnText = ""
Alamofire.request(.GET, doorUrl)
.responseString{ openStatus in
if openStatus.result.value == "成功"{
returnText="成功"
}else{
returnText="失败"
}
UIAlertView(title: "敲门结果", message: returnText,
delegate: nil, cancelButtonTitle: "确定").show()
}
}
override func viewDidLoad() {
super.viewDidLoad()
timePicker=UIPickerView()
// 定义位置
self.timePicker.frame = CGRectMake(120, 100, 150, 200)
// 绑定数据
self.timePicker.delegate=self
self.timePicker.dataSource=self
//设置选择框的默认值
timePicker.selectRow(3,inComponent:0,animated:true)
//添加到界面中
self.view.addSubview(timePicker)
}
//设置选择框的列数为1列,继承于UIPickerViewDataSource协议
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// 返回要显示文字的列表
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 1){
return buildString.count
}else{
return buildString.count
}
}
// 返回显示内容
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 1){
return "\(buildString[row])"
}else{
return "\(buildString[row])"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}