在我的’
Swift‘应用程序中,我有一个将照片上传到我的Amazon S3存储桶的功能.当用户连接到WiFi或LTE时,没有问题,但是当连接速度稍慢(例如3G)时,上传需要花费很多时间(最多一分钟),iphone可能会损失15-20%电池!我将照片调整到大约200-300kb,这应该不是问题.我用的代码是:
func awsS3PhotoUploader(_ ext: String,pathToFile: String,contentType: String,automaticUpload: Bool){ let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType,identityPoolId:CognitoIdentityPoolId) let configuration = AWSServiceConfiguration(region:CognitoRegionType,credentialsProvider:credentialsProvider) AWSServiceManager.default().defaultServiceConfiguration = configuration let uploadRequest = AWSS3TransferManagerUploadRequest() uploadRequest?.body = URL(string: "file://"+pathToFile) uploadRequest?.key = ProcessInfo.processInfo.globallyUniqueString + "." + ext uploadRequest?.bucket = S3BucketName uploadRequest?.contentType = contentType + ext uploadRequest?.uploadProgress = { (bytesSent,totalBytesSent,totalBytesExpectedToSend) -> Void in DispatchQueue.main.async(execute: { () -> Void in if totalBytesExpectedToSend > 1 { print(totalBytesSent) print(totalBytesExpectedToSend) } }) } let transferManager = AWSS3TransferManager.default() transferManager?.upload(uploadRequest).continue({ (task) -> AnyObject! in if (task.isCompleted) { print("task completed") } if let error = task.error { print("Upload Failed ❌ (\(error))") } if let exception = task.exception { print("Upload Failed ❌ (\(exception))") } if task.result != nil { let s3URL: String = "https://myAlias.cloudfront.net/\((uploadRequest?.key!)!)" print("Uploaded to:\n\(s3URL)") } else { print("Unexpected empty result.") } return nil } ) }
有什么事情让你想到我在这里做错了什么?如何避免这种巨大的电池消耗?
解决方法
以下答案的灵感来自
https://stackoverflow.com/a/20690088/3549695
我相信你需要做的是能够检测无线电网络的类型.无论是WiFi,LTE,3G,2G还是无网络.
然后,应用程序将需要根据结果做出决定.
我创建了一个测试Xcode项目来测试我的iPhone 6上的这个概念.
它似乎工作,但我只能测试’空中飞机模式’,WiFi和LTE.我不能让自己进入2G或3G网络.
如果是WiFi或LTE,我将获得价值:
‘CTRadioAccessTechnologyLTE’
在“Air Plane Mode”中,Optional值为零.因此,由我来取代它的文本取决于我.我选择输出’无法检测’
这是我的ViewController.swift的样子:
import UIKit import CoreTelephony class ViewController: UIViewController { @IBOutlet weak var currentRAN: UILabel! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. } @IBAction func detect(_ sender: UIButton) { if case let telephonyInfo = CTTelephonyNetworkInfo(),let currentRadioAccessTech = telephonyInfo.currentRadioAccessTechnology { currentRAN.text = currentRadioAccessTech print("Current Radio Access Technology: \(currentRadioAccessTech)") } else { currentRAN.text = "Not able to detect" print("Not able to detect") } } }
.currentRadioAccessTechnology的可能值为:
/* * Radio Access Technology values */ @available(iOS 7.0,*) public let CTRadioAccessTechnologyGPRS: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyEdge: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyWCDMA: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyHSDPA: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyHSUPA: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyCDMA1x: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyCDMAEVDORev0: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyCDMAEVDORevA: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyCDMAEVDORevB: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyeHRPD: String @available(iOS 7.0,*) public let CTRadioAccessTechnologyLTE: String