Alamofire.request("https://example.com/writecomment.php", method: .post, parameters: parameters).validate().responseJSON { response in
switch response.result {
case .success:
if let json = response.result.value {
var success = 0
if let dictJSON = json as? [String: AnyObject] {
if let successInteger = dictJSON["success"] as? Int {
success = successInteger
if success == 1
{
//succes
}
}
}
}
case .failure(_):
return
}
}
This is connected to an UIButton action
It SOMETIMES triggers service calls multiple times when user tap button.
How to prevent multiple calls?
Example::
var isAPICalling = false
#IBAction func btnTapped(_ sender: UIButton) {
if isAPICalling == false
{
isAPICalling = true
self.APICalling() // Your API calling method
}
}
In APICalling() method set isAPICalling = false when you get response.
OTHER OPTION :
Use ActivityIndicator when you request that time start indicator and after response stop indicator. So you cannot able to tap on button.
You can do one thing without using flag, if you have button outlet then just make button state selected when button click action is performed.
then call API and once you get the response make button state normal. so it will call only once when user click on button.
#IBAction func btnApiAction(_ sender: Any) {
if btnAPI.isSelected == false {
btnAPI.isSelected = true
//Call API here
}else {
//Do nothing here
}
}
if API Call is get failed or if you are getting any error then also change button state to normal so user can click again on button.
Disable UIButton when you call API. And enable back when api call finished.
#IBAction func didTapButton(_ sender: UIButton) {
sender.isEnabled = false
Alamofire.request(/* Params */) { response in
sender.isEnabled = true
// parse response - response.result
}
}
I prefer to add ActivityIndicatorView to the button view, and remove when finished for better UX.
Related
I have a method with a completion that returns a bool. But I cant figure out how to get around this error.
Error
UISwitch.isOn must be used from main thread only
Button Action
#IBAction func notificationSwitch(_ sender: Any) {
LocalNotification().checkEnabled(completion: { (success) -> Void in
// When download completes,control flow goes here.
if success == false{
print("Cant Turn On")
self.notificationToggle.isOn = false
} else {
print("Can Turn On")
if self.notificationToggle.isOn == true {
self.notificationToggle.isOn = false
} else {
self.notificationToggle.isOn = true
}
}
})
}
also already tried wrapping the LocalNotifications().... in DispatchQueue.main.async but still get the same error
You're almost there. It is not the checkEnabled that needs to be wrapped in the call to get onto the main thread, but the stuff "inside" it:
LocalNotification().checkEnabled(completion: { (success) -> Void in
DispatchQueue.main.async {
if success == false {
On iOS app with GoogleInteractiveMediaAds integrated "Skip Ad" button doesn't work. Meanwhile manual call adsManager.skip() works perfectly. The button itself reacts to the tuches because it changes bounds and seems highlighted. Unfortunately, I haven't found anything according to handle tap manually, so maybe somebody has already been in this situation and could help with it.
guard
let adInformation = delegate?.latestAdInformation(), let url = adInformation.urlForIMA,
let adContainer = delegate?.videoAdDisplayContainerView()
else { return }
switch adInformation.adsType {
case .interstitials, .none:
self.play(ignoreAds: true)
return
case .prerolls, .all:
fallthrough
#unknown default:
break
}
let adDisplayContainer = IMAAdDisplayContainer(adContainer: adContainer, companionSlots: nil)
let request = IMAAdsRequest(
adTagUrl: url,
adDisplayContainer: adDisplayContainer,
contentPlayhead: contentPlayhead,
userContext: nil)
adsLoader.requestAds(with: request)
func adsLoader(_ loader: IMAAdsLoader!, adsLoadedWith adsLoadedData: IMAAdsLoadedData!) {
// Grab the instance of the IMAAdsManager and set ourselves as the delegate
adsManager = adsLoadedData.adsManager
adsManager?.delegate = self
// Create ads rendering settings and tell the SDK to use the in-app browser.
let adsRenderingSettings = IMAAdsRenderingSettings()
if let vc = delegate?.adWebControllerPreferredOpenViewController() {
adsRenderingSettings.webOpenerPresentingController = vc
}
// Initialize the ads manager.
adsManager?.initialize(with: adsRenderingSettings)
}
func adsLoader(_ loader: IMAAdsLoader!, failedWith adErrorData: IMAAdLoadingErrorData!) {
self.play(ignoreAds: true)
}
func adsManager(_ adsManager: IMAAdsManager!, didReceive event: IMAAdEvent!) {
if event.type == IMAAdEventType.LOADED {
adsManager.start()
}
}
func adsManager(_ adsManager: IMAAdsManager!, didReceive error: IMAAdError!) {
self.play(ignoreAds: true)
}
func adsManagerDidRequestContentPause(_ adsManager: IMAAdsManager!) {
self.pause(ignoreAds: true)
}
func adsManagerDidRequestContentResume(_ adsManager: IMAAdsManager!) {
self.play(ignoreAds: true)
}
I have just started using Digits - Twitter API for Phone Number verification, but it seems I'm unable to read the user's Phone number, I'm not sure if there is a function for that or so, but after reading a while I knew that I can do that with a Call back after successful phone verification but no explanation for that !
AuthConfig.Builder authConfigBuilder = new AuthConfig.Builder()
.withAuthCallBack(callback)
.withPhoneNumber(phoneNumberOrCountryCodeFromMyActivity)
found this snippet but again not sure where to implement it.
HERE is my Action for the login button with phone verification:
fileprivate func navigateToMainAppScreen() {
performSegue(withIdentifier: "signedIn", sender: self)
}
#IBAction func tapped(_ sender: Any) {
let configuration = DGTAuthenticationConfiguration(accountFields: .defaultOptionMask)
configuration?.appearance = DGTAppearance()
configuration?.appearance.backgroundColor = UIColor.white
configuration?.appearance.accentColor = UIColor.red
// Start the Digits authentication flow with the custom appearance.
Digits.sharedInstance().authenticate(with: nil, configuration:configuration!) { (session, error) in
if session != nil {
// Navigate to the main app screen to select a theme.
self.navigateToMainAppScreen()
} else {
print("Error")
}
}
}
So I found the answer after digging a lot more in Digits Documentations and it was pretty simple, I had to add:
print(session.phoneNumber)
print(session.userID)
In the didTap function, so the complete code will be:
#IBAction func tapped(_ sender: Any) {
let configuration = DGTAuthenticationConfiguration(accountFields: .defaultOptionMask)
configuration?.appearance = DGTAppearance()
configuration?.appearance.backgroundColor = UIColor.white
configuration?.appearance.accentColor = UIColor.red
// Start the Digits authentication flow with the custom appearance.
Digits.sharedInstance().authenticate(with: nil, configuration:configuration!) { (session, error) in
if session != nil {
//Print Data
print(session?.phoneNumber)
print(session?.userID)
// Navigate to the main app screen to select a theme.
self.navigateToMainAppScreen()
} else {
print("Error")
}
}
}
Here is the Reference I have used:
https://docs.fabric.io/apple/examples/cannonball/index.html#sign-in-with-digits
I am trying to perform segue to Home Screen after checking whether entered username and password are correct and if they are incorrect I am trying to show an alert message. I am not successful in performing either of those. Because as soon as I click the login button a white screen is displayed.
Below is my code:
Performing segue in the following method:
#IBAction func didClickLogin(_ sender: Any) {
self.progressIndicator.isHidden = false
self.progressIndicator.startAnimating()
let enteredUserName = mMobileNumber.text
let enteredPassword = mPassword.text
//performing validation for username and password
didUserLogin(username: enteredUserName!, password: enteredPassword!)
return true
}
My didLogin function:
func didUserLogin(username: String, password: String) {
let requestString = Constants.BASE_URL+"/mlogin"
let parameters: Parameters = ["username":username, "password":password]
let headers = ["Authorization": "Basic \(Constants.base64LoginString)"]
Alamofire.request(requestString, method:.post, parameters: parameters, encoding: URLEncoding.default, headers: headers).responseJSON { response in
//to get status code
switch response.result {
case .success(let data):
let json = JSON(data)
switch (json["status"].stringValue){
case "200":
DispatchQueue.main.async {
self.performSegue(withIdentifier: "loginSuccess", sender: self)
}
break
case "401":
self.progressIndicator.isHidden = true
self.progressIndicator.stopAnimating()
let alert = Constants()
alert.showAlert(fromController: self, alertMessage: "Entered username or password is incorrect")
break
case .failure(let error):
print("Request failed with error: \(error)")
}
}
}
}
Please let me know what mistake am I doing and please let me know what changes do I need to do. I am not bale to display the alert message also.
I am getting the below warning:
Warning: Attempt to present <UIAlertController: on whose view is not in the window hierarchy!
I think you just need to implement the following method, and it should work fine :
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "YOUR_SCENE_IDENTIFIER_TO_THE_NEXT_SCREEN") {
// pass some data to the destination vc, if you need
}
}
Code to preform segue:
performSegue(withIdentifier: "loginSuccess", sender: nil)
I've been getting an EXC_BAD_ACCESS error/crash when I engage in a specific action within my application. Figuring that this was a memory management issue, I enabled NSZombies to help me decipher the issue. Upon the crash, my console gave me the following message:
heres my stack trace:
and the new error highlighting my app delegate line:
Now being the debugger is referring to a UIActivityIndicatorRelease, and the only line of code highlighted in my stack trace is the 1st line in my delegate, is there an issue with my Activity Indicator UI Element? Here is the logic within my login action ( which forces the crash every time ):
#IBAction func Login(sender: AnyObject) {
activityIND.hidden = false
activityIND.startAnimating()
var userName = usernameText.text
var passWord = passwordText.text
PFUser.logInWithUsernameInBackground(userName, password: passWord) {
(user, error: NSError?) -> Void in
if user != nil {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("loginSuccess", sender: self)
}
} else {
self.activityIND.stopAnimating()
if let message: AnyObject = error!.userInfo!["error"] {
self.message.text = "\(message)"
}
}
}
}
is there an error within it?
All your code that manipulates UI objects absolutely, positively must be done from the main thread. (and so it should be in a call to dispatch_async(dispatch_get_main_queue()) as #JAL says in his comment.
That includes not just the self.activityIND.stopAnimating() line, but the code that sets label text as well (any code that manipulates a UIKit object like a UIView).
Your if...else clause should look something like this:
if user != nil
{
dispatch_async(dispatch_get_main_queue())
{
self.activityIND.stopAnimating()
self.performSegueWithIdentifier("loginSuccess", sender: self)
}
}
else
{
dispatch_async(dispatch_get_main_queue())
{
self.activityIND.stopAnimating()
if let message: AnyObject = error!.userInfo!["error"]
{
self.message.text = "\(message)"
}
}
}
So it turns out, in my viewDidLoad() I had the following code in attempt to hide the indicator on the load:
UIActivityIndicator.appearance().hidden = true
UIActivityIndicatorView.appearance().hidesWhenStopped = true
not knowing this would deallocate the indicator for the remainder of the application so when i called the following in my login logic:
activityIND.hidden = false
activityIND.startAnimating()
i was sending a message to an instance that was no longer available, causing the crashes. So all i did was adjust my code in viewDidLoad()
to :
activityIND.hidden = true
activityIND.hidesWhenStopped = true
using the name of the specific outlet I created rather than the general UIActivityIndicatorView
All UI related operation should execute in Main Thread, i.e., within
dispatch_async(dispatch_get_main_queue(){}
block
#IBAction func Login(sender: AnyObject) {
activityIND.hidden = false
activityIND.startAnimating()
var userName = usernameText.text
var passWord = passwordText.text
PFUser.logInWithUsernameInBackground(userName, password: passWord) {
(user, error: NSError?) -> Void in
if user != nil {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("loginSuccess", sender: self) //UI task
}
}
else {
dispatch_async(dispatch_get_main_queue())
{
self.activityIND.stopAnimating() //UI task
if let message: AnyObject = error!.userInfo!["error"]
{
self.message.text = "\(message)" //UI task
}
};
}
}
}
Refer some good articles on Concurrency here and here