我正在将数据从我的Swift应用程序上传到Amazon S3,它就像其他任何东西一样耗尽电池.如何避免这种情况?

use*_*930 7 amazon-s3 amazon-web-services ios swift

在我的'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
        }
        )

}
Run Code Online (Sandbox Code Playgroud)

有什么事情让你想到我在这里做错了什么以及如何避免这种巨大的电池消耗?

Wis*_*min 5

以下答案的灵感来自/sf/answers/1448306191/

我相信你需要做的是能够检测无线电网络的类型.无论是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")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)