gpt4 book ai didi

ios - iOS 中如何保存 Web 服务调用并在有互联网连接时使用它?

转载 作者:行者123 更新时间:2023-11-30 12:25:00 24 4
gpt4 key购买 nike

我正在尝试实现Web服务调用,当用户截图时,我已经使用下面的方法成功实现了它。

  //this method get called when screenshot captured
func detectScreenShot() {
let mainQueue = OperationQueue.main
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationUserDidTakeScreenshot,
object: nil,
queue: mainQueue,
using: { notification in
WEBSERVICE_INTERFACE.webServiceWithPostJSONParameters(param: nil, methodName: Constants.URLs.screenshot, headers: Constants.Headers.urlEncoded, showProgress: false, completion: { (response) in

if let response = response{
let response = BaseResponse(JSONString : response)
if let message = response?.message{
LIMITUtils.showAlertMessage(message: message)
}
}

})

})
}

但现在我有这样的场景,假设用户通过关闭互联网连接来截取屏幕截图,那么不会发生任何 Web 服务调用,用户将从中受益。现在我想要某种解决方案,如果没有可用的连接,我可以保存网络服务调用,并在互联网连接可用时进行相同的调用。谁能建议我如何继续此操作?

最佳答案

使用Reachability要检查互联网是否可用,请编写以下代码来检查互联网

//MARK:- check internet connection
func checkInternetStatus() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(reachabilityChanged(_:)), name:ReachabilityChangedNotification, object: nil)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)) {
//Check for Internet Status
do {
self.reachability = try Reachability.reachabilityForInternetConnection()
try self.reachability?.startNotifier()
} catch {
jprint("Errore")
return
}
}
}

func reachabilityChanged(note:NSNotification) {
let Status = reachability?.currentReachabilityStatus //note.object as! Reachability
if Status != .NotReachable {
jprint("isNetworkAvailable = true")
isNetworkAvailable = true
} else {
jprint("isNetworkAvailable = false")
ShowFlinntAlert("No internet", description: "Your internet connection seems to be down")
isNetworkAvailable = false
}
}

在 API 调用之前只需检查

guard isInternetAvailable() else {
return
}

关于ios - iOS 中如何保存 Web 服务调用并在有互联网连接时使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44325489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com