在我的“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
}
)
}
有什么事情让你想到我在这里做错了什么以及如何避免这种巨大的电池消耗?
答案 0 :(得分:5)
以下答案受https://stackoverflow.com/a/20690088/3549695
启发我相信你需要做的是能够检测无线电网络的类型。无论是WiFi,LTE,3G,2G还是无网络。 然后,应用程序将需要根据结果做出决定。
我创建了一个测试Xcode项目,以便在我的iPhone 6上测试这个概念。 它似乎有效,但我只能测试飞机模式' WiFi和LTE。我无法进入2G或3G网络。
如果是WiFi或LTE,我会得到价值: ' CTRadioAccessTechnologyLTE'
在“空中飞机模式”中,“可选”值为零。因此,由我来取代它的是什么文字取决于我。我选择输出'无法检测'
这是我的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