Switch Save user preference












0















I am trying to send a tag with OneSignal when the switch is turned on, and send request to delete the tag when it is turned off again.



@IBAction func tagGeneral(_ sender: UISwitch) {

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")
}
else {
OneSignal.deleteTag("General")
print("deletetag")
}
}


This is the code i use for it. Seems to be working but when the user goes to another page the switch is automatically turned off...



How can i fix this?










share|improve this question

























  • Store the state somewhere. IE> UserDefaults

    – Ryan
    Nov 25 '18 at 18:18
















0















I am trying to send a tag with OneSignal when the switch is turned on, and send request to delete the tag when it is turned off again.



@IBAction func tagGeneral(_ sender: UISwitch) {

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")
}
else {
OneSignal.deleteTag("General")
print("deletetag")
}
}


This is the code i use for it. Seems to be working but when the user goes to another page the switch is automatically turned off...



How can i fix this?










share|improve this question

























  • Store the state somewhere. IE> UserDefaults

    – Ryan
    Nov 25 '18 at 18:18














0












0








0








I am trying to send a tag with OneSignal when the switch is turned on, and send request to delete the tag when it is turned off again.



@IBAction func tagGeneral(_ sender: UISwitch) {

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")
}
else {
OneSignal.deleteTag("General")
print("deletetag")
}
}


This is the code i use for it. Seems to be working but when the user goes to another page the switch is automatically turned off...



How can i fix this?










share|improve this question
















I am trying to send a tag with OneSignal when the switch is turned on, and send request to delete the tag when it is turned off again.



@IBAction func tagGeneral(_ sender: UISwitch) {

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")
}
else {
OneSignal.deleteTag("General")
print("deletetag")
}
}


This is the code i use for it. Seems to be working but when the user goes to another page the switch is automatically turned off...



How can i fix this?







ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 18:27









rmaddy

241k27316380




241k27316380










asked Nov 25 '18 at 18:07









JobJob

74




74













  • Store the state somewhere. IE> UserDefaults

    – Ryan
    Nov 25 '18 at 18:18



















  • Store the state somewhere. IE> UserDefaults

    – Ryan
    Nov 25 '18 at 18:18

















Store the state somewhere. IE> UserDefaults

– Ryan
Nov 25 '18 at 18:18





Store the state somewhere. IE> UserDefaults

– Ryan
Nov 25 '18 at 18:18












1 Answer
1






active

oldest

votes


















0














Regarding @Ryan's comment, Here's an answer:



First. there are many ways to save the user preference, i'll be doing it with UserDefaults() | Edit your button action code:



@IBAction func tagGeneral(_ sender: UISwitch) {

let userdef = UserDefaults.standard

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")

// user made the choice
userdef.set(true, forKey: "sw_set")
} else {
OneSignal.deleteTag("General")
print("deletetag")

// reset
userdef.set(false, forKey: "sw_set")
}
}


Normally this wouldn't work without this little function, make sure you call this function in your viewDidAppear():



private func init_switch() {
// Thanks @Vadian for the tip

let userdef = UserDefaults.standard
self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")
}


Call it in viewDidAppear():



override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

self.init_switch()
}


And let me know if it helped.






share|improve this answer


























  • Why not directly self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")?

    – vadian
    Nov 25 '18 at 19:13











  • Needs to be unwrapped and complicates the code. I'm sorry, If you don't like it i could edit it

    – excitedmicrobe
    Nov 25 '18 at 19:14











  • No, there's nothing to be unwrapped. And it simplifies the code.

    – vadian
    Nov 25 '18 at 19:15













  • Well the bool does not exist in the first place it would either return false or needs a ?, Thanks for the tip

    – excitedmicrobe
    Nov 25 '18 at 19:16











  • If the bool does not exist it returns false, this method returns always a non-optional.

    – vadian
    Nov 25 '18 at 19:18













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470388%2fswitch-save-user-preference%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Regarding @Ryan's comment, Here's an answer:



First. there are many ways to save the user preference, i'll be doing it with UserDefaults() | Edit your button action code:



@IBAction func tagGeneral(_ sender: UISwitch) {

let userdef = UserDefaults.standard

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")

// user made the choice
userdef.set(true, forKey: "sw_set")
} else {
OneSignal.deleteTag("General")
print("deletetag")

// reset
userdef.set(false, forKey: "sw_set")
}
}


Normally this wouldn't work without this little function, make sure you call this function in your viewDidAppear():



private func init_switch() {
// Thanks @Vadian for the tip

let userdef = UserDefaults.standard
self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")
}


Call it in viewDidAppear():



override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

self.init_switch()
}


And let me know if it helped.






share|improve this answer


























  • Why not directly self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")?

    – vadian
    Nov 25 '18 at 19:13











  • Needs to be unwrapped and complicates the code. I'm sorry, If you don't like it i could edit it

    – excitedmicrobe
    Nov 25 '18 at 19:14











  • No, there's nothing to be unwrapped. And it simplifies the code.

    – vadian
    Nov 25 '18 at 19:15













  • Well the bool does not exist in the first place it would either return false or needs a ?, Thanks for the tip

    – excitedmicrobe
    Nov 25 '18 at 19:16











  • If the bool does not exist it returns false, this method returns always a non-optional.

    – vadian
    Nov 25 '18 at 19:18


















0














Regarding @Ryan's comment, Here's an answer:



First. there are many ways to save the user preference, i'll be doing it with UserDefaults() | Edit your button action code:



@IBAction func tagGeneral(_ sender: UISwitch) {

let userdef = UserDefaults.standard

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")

// user made the choice
userdef.set(true, forKey: "sw_set")
} else {
OneSignal.deleteTag("General")
print("deletetag")

// reset
userdef.set(false, forKey: "sw_set")
}
}


Normally this wouldn't work without this little function, make sure you call this function in your viewDidAppear():



private func init_switch() {
// Thanks @Vadian for the tip

let userdef = UserDefaults.standard
self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")
}


Call it in viewDidAppear():



override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

self.init_switch()
}


And let me know if it helped.






share|improve this answer


























  • Why not directly self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")?

    – vadian
    Nov 25 '18 at 19:13











  • Needs to be unwrapped and complicates the code. I'm sorry, If you don't like it i could edit it

    – excitedmicrobe
    Nov 25 '18 at 19:14











  • No, there's nothing to be unwrapped. And it simplifies the code.

    – vadian
    Nov 25 '18 at 19:15













  • Well the bool does not exist in the first place it would either return false or needs a ?, Thanks for the tip

    – excitedmicrobe
    Nov 25 '18 at 19:16











  • If the bool does not exist it returns false, this method returns always a non-optional.

    – vadian
    Nov 25 '18 at 19:18
















0












0








0







Regarding @Ryan's comment, Here's an answer:



First. there are many ways to save the user preference, i'll be doing it with UserDefaults() | Edit your button action code:



@IBAction func tagGeneral(_ sender: UISwitch) {

let userdef = UserDefaults.standard

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")

// user made the choice
userdef.set(true, forKey: "sw_set")
} else {
OneSignal.deleteTag("General")
print("deletetag")

// reset
userdef.set(false, forKey: "sw_set")
}
}


Normally this wouldn't work without this little function, make sure you call this function in your viewDidAppear():



private func init_switch() {
// Thanks @Vadian for the tip

let userdef = UserDefaults.standard
self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")
}


Call it in viewDidAppear():



override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

self.init_switch()
}


And let me know if it helped.






share|improve this answer















Regarding @Ryan's comment, Here's an answer:



First. there are many ways to save the user preference, i'll be doing it with UserDefaults() | Edit your button action code:



@IBAction func tagGeneral(_ sender: UISwitch) {

let userdef = UserDefaults.standard

if (sender.isOn == true) {
OneSignal.sendTag("General", value: "value")
print("sendtag")

// user made the choice
userdef.set(true, forKey: "sw_set")
} else {
OneSignal.deleteTag("General")
print("deletetag")

// reset
userdef.set(false, forKey: "sw_set")
}
}


Normally this wouldn't work without this little function, make sure you call this function in your viewDidAppear():



private func init_switch() {
// Thanks @Vadian for the tip

let userdef = UserDefaults.standard
self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")
}


Call it in viewDidAppear():



override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

self.init_switch()
}


And let me know if it helped.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 19:18

























answered Nov 25 '18 at 19:11









excitedmicrobeexcitedmicrobe

9671518




9671518













  • Why not directly self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")?

    – vadian
    Nov 25 '18 at 19:13











  • Needs to be unwrapped and complicates the code. I'm sorry, If you don't like it i could edit it

    – excitedmicrobe
    Nov 25 '18 at 19:14











  • No, there's nothing to be unwrapped. And it simplifies the code.

    – vadian
    Nov 25 '18 at 19:15













  • Well the bool does not exist in the first place it would either return false or needs a ?, Thanks for the tip

    – excitedmicrobe
    Nov 25 '18 at 19:16











  • If the bool does not exist it returns false, this method returns always a non-optional.

    – vadian
    Nov 25 '18 at 19:18





















  • Why not directly self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")?

    – vadian
    Nov 25 '18 at 19:13











  • Needs to be unwrapped and complicates the code. I'm sorry, If you don't like it i could edit it

    – excitedmicrobe
    Nov 25 '18 at 19:14











  • No, there's nothing to be unwrapped. And it simplifies the code.

    – vadian
    Nov 25 '18 at 19:15













  • Well the bool does not exist in the first place it would either return false or needs a ?, Thanks for the tip

    – excitedmicrobe
    Nov 25 '18 at 19:16











  • If the bool does not exist it returns false, this method returns always a non-optional.

    – vadian
    Nov 25 '18 at 19:18



















Why not directly self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")?

– vadian
Nov 25 '18 at 19:13





Why not directly self.yourSwitch.isOn = userdef.bool(forKey: "sw_set")?

– vadian
Nov 25 '18 at 19:13













Needs to be unwrapped and complicates the code. I'm sorry, If you don't like it i could edit it

– excitedmicrobe
Nov 25 '18 at 19:14





Needs to be unwrapped and complicates the code. I'm sorry, If you don't like it i could edit it

– excitedmicrobe
Nov 25 '18 at 19:14













No, there's nothing to be unwrapped. And it simplifies the code.

– vadian
Nov 25 '18 at 19:15







No, there's nothing to be unwrapped. And it simplifies the code.

– vadian
Nov 25 '18 at 19:15















Well the bool does not exist in the first place it would either return false or needs a ?, Thanks for the tip

– excitedmicrobe
Nov 25 '18 at 19:16





Well the bool does not exist in the first place it would either return false or needs a ?, Thanks for the tip

– excitedmicrobe
Nov 25 '18 at 19:16













If the bool does not exist it returns false, this method returns always a non-optional.

– vadian
Nov 25 '18 at 19:18







If the bool does not exist it returns false, this method returns always a non-optional.

– vadian
Nov 25 '18 at 19:18




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470388%2fswitch-save-user-preference%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks