根据时间在iOS应用程序中拨打电话

时间:2016-06-14 14:06:01

标签: ios nsdate nscalendar

我目前正在开发自己的iOS应用程序,只需按一下按钮即可拨打电话。我想这样按钮根据一天中的时间调用不同的数字。我的想法是用UTC区分数字,因为本地时间取决于设备的位置。例如,如果它在00:01和08:00之间,则调用1234567.如果它在UTC时间08:01和16:00之间,则调用7654321。

这是我到目前为止所做的:

import UIKit

var str = "Hello, playground"

typealias WorkingTime = (start: NSDate, end: NSDate)
typealias TelephoneNumber = String

enum Regions {
    case Americas
    case EMEA
    case APAC

    func serviceDeskNumberForRegion(region: Regions) -> TelephoneNumber {
        switch self {
        case .Americas:
            return "00112341234"
        case .EMEA:
            return "12345678976543"
        case .APAC:
            return "3245678908"
        }
    }
}


//this is to create the dates from string
let dateformatter = NSDateFormatter()
dateformatter.dateFormat = "HH:mm"



//we're hardcoding....
struct WorkingTimesForRegion {
    static let AmericanWorkingTimes = (start: dateformatter.dateFromString("00:01")!, end: dateformatter.dateFromString("08:00")!)
    static let EMEAWorkingTimes = (start: dateformatter.dateFromString("08:01")!, end: dateformatter.dateFromString("16:00")!)
    static let APACWorkingTimes = (start: dateformatter.dateFromString("16:01")!, end: dateformatter.dateFromString("00:00")!)

    static func regionForTime(time: NSDate) -> Regions {
        //
        //here I should use the nscalendar to make sure all times are represented on the same timezones, like UTC. Not sure how to go about this.
        //


        //here I probably need to check both end and start 
        //a switch case would've been more elegant here but I don't see how to build it at the moment
        //an option I would explore is using NSRange as they make sense in principle and can be used in switch statements.
        //but I don't know if you can create ranges of dates ...
        if time.compare(WorkingTimesForRegion.AmericanWorkingTimes.end) == .OrderedAscending {
            return .Americas
        } else if time.compare(WorkingTimesForRegion.EMEAWorkingTimes.end) == .OrderedAscending {
            return .EMEA
        } else if time.compare(WorkingTimesForRegion.EMEAWorkingTimes.end) == .OrderedAscending {
            return .APAC
        }
        //just in case
        return .EMEA
    }
}


func serviceDeskForTime(time: NSDate) -> TelephoneNumber {
    let serviceDeskInCharge = WorkingTimesForRegion.regionForTime(time)
    return serviceDeskInCharge.serviceDeskNumberForRegion(serviceDeskInCharge)
}


//see if it works
serviceDeskForTime(dateformatter.dateFromString("05:00")!)


func call() -> Bool {
    guard let url = NSURL(string: "tel://123456765") else { return false }
    if UIApplication.sharedApplication().canOpenURL(url) {
        return UIApplication.sharedApplication().openURL(url)
    }
    return false
}

任何帮助将不胜感激。谢谢!

0 个答案:

没有答案