在我的“Swift”应用程序中,我具有将照片上传到Amazon S3存储桶的功能。当用户连接到WiFiLTE时,没有问题,但是当连接速度稍慢时(例如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启发

我相信您需要做的就是检测 radio 网络类型的能力。无论是WiFi,LTE,3G,2G还是无网络。
然后,应用程序将需要根据结果做出决定。

我创建了一个测试Xcode项目,以在iPhone 6上测试此概念。
看来可行,但是我只能测试“飞机模式”,WiFi和LTE。我无法进入2G或3G网络。

如果是WiFi或LTE,我将获得以下值(value):
“CTRadioAccessTechnologyLTE”

在“飞机模式”下,“可选”值将为nil。因此,由我自己替换什么文字取决于我。我选择输出“无法检测”

这是我的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

关于ios - 我正在将数据从我的Swift应用程序上传到Amazon S3,它像其他东西一样消耗了电池。如何避免这种情况?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44097830/

10-12 14:43