iOS Users

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
shartfield
Posts: 3
Joined: Thu Oct 11, 2012 10:32 pm

iOS Users

Post by shartfield » Thu Oct 11, 2012 10:43 pm

I am very new to LiveCode and iOS. I am wondering if someone would consider posting an example of how to manage users in iOS. Specifically, create a user, manage data associated with that user, and then delete the user when no longer needed. I have reviewed the files and folder entries in the dictionary, but am just not putting it together.

endernafi
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 296
Joined: Wed May 02, 2012 12:23 pm
Contact:

Re: iOS Users

Post by endernafi » Fri Oct 12, 2012 7:35 am

Hello shartfield,

Try these commands...
Image


Best,

~ Ender Nafi
~... together, we're smarter ...~
__________________________________________

macOS Sierra • LiveCode 7 & xCode 8

endernafi
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 296
Joined: Wed May 02, 2012 12:23 pm
Contact:

Re: iOS Users

Post by endernafi » Fri Oct 12, 2012 8:14 am

And here is a very simple implementation of that 8 commands.
mobileContactSample.zip
(1.74 KiB) Downloaded 350 times
Hope it helps...


Best,

~ Ender Nafi
~... together, we're smarter ...~
__________________________________________

macOS Sierra • LiveCode 7 & xCode 8

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: iOS Users

Post by Klaus » Fri Oct 12, 2012 12:20 pm

Hi shartfield,

as you can see, this is a VERY lively forum, so you better look here more often :D

But first of all, please tell us if you really mean USERS (iOS is a single user OS)
or ENTRIES in the current users "Contacts" (Address Book) 8)

Or if you want to provide a way to let more than one user use your app on the
same device, which is surely possible!


Best

Klaus

endernafi
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 296
Joined: Wed May 02, 2012 12:23 pm
Contact:

Re: iOS Users

Post by endernafi » Fri Oct 12, 2012 12:32 pm

Did I misunderstood shartfield's question?
Well, sorry then :oops:


~ Ender Nafi
~... together, we're smarter ...~
__________________________________________

macOS Sierra • LiveCode 7 & xCode 8

shartfield
Posts: 3
Joined: Thu Oct 11, 2012 10:32 pm

Re: iOS Users

Post by shartfield » Sat Oct 13, 2012 1:26 am

Thanks for all of your responses. This is what I am asking…. Imagine you have a game on a single iPad and you want to be able to allow several people to play it, and save the game state for each person. Only one person plays at a time, but any of the “users” can pick up where they left off to continue playing. How would you create each of the users? I think saving the game state will be easy – just write to a data file, right? But then how would you associate each preference file to a user?

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: iOS Users

Post by Klaus » Sat Oct 13, 2012 12:54 pm

Hi shartfield,

AHA :D

On the first card of your app you should provide a list (field) for all users and a button to create a new user.
User clicks his name (forcing unique name is mandatory!) and you create a folder with this name in
-> specialfolderpath("documents")
and save the highscore etc. in that folder.

Use a global variable of a custom property to store like this:

Code: Select all

...
put specialfolderpath("documents") & "/" & the_selected_user_name_in_user_list & "/" into tCurrentUserFolder
if there is not a folder tCurrentUserFolder then
  create folder tCurrentUserFolder
end if
set the cCurrentUserFolder of this stack to tCurrentUserFolder
...
Then save your stuff like this:

Code: Select all

...
put the cCurrentUserFolder of this stack into tCurrentUserFolder
put tHighScore into url("file:" & tCurrentUserFolder & "highscore")
## And/or whatever you need to store for a user...
...
Hint: To delete a folder you first have to delete its contents = all files, before you can delete that folder

Well, that's it basically 8)


Best

Klaus

shartfield
Posts: 3
Joined: Thu Oct 11, 2012 10:32 pm

Re: iOS Users

Post by shartfield » Mon Oct 15, 2012 3:34 pm

Thank you very much! This is very helpful.

chelling
Posts: 100
Joined: Tue Mar 06, 2007 3:14 am

Re: iOS Users

Post by chelling » Mon Nov 26, 2012 1:33 am

I have been playing around with this topic am hoping someone will be willing to take a look at the sample stack I have attached. I have broken it down into 5 steps to help me think it through better. I am certain there is a better way to accomplish this. Also, I really am not understanding the custom properties - I can see the changes in the property inspector but how are they referencing to the user? Are the custom properties a global variable that has to be declared in each script?

Create User - Creates a new user; Checks to make sure the new user doesn't exist, Creates the user folder
Delete User - Deletes the user
Save User - Saves user data
Open User Data - Switches to display the current user data
Change User - Changes the user by switching the folder
Delete User - I can delete the user in the display field but the folders and file(s) aren't deleting

Thanks in advance for your help.
Attachments
User Management.livecode.zip
(2.82 KiB) Downloaded 288 times

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: iOS Users

Post by sturgis » Mon Nov 26, 2012 3:10 am

Just had time to look at one of your scripts (create user) follows is some commented replacement code.
For your usercheck you can do it with 2 lines. (as indicated in script below)
set the wholematches to true (will force a check of the whole line. Whole line must match or its a non-match)
then lineoffset
get lineoffset(valueToLookFor,linesToCheckAgainst)
if it > 0 then a match was found, the user already exists.

As always I am sure there are cleaner ways to do this. If so, others will most likely chime in. (Will look at it more tomorrow probably after I crash for a while)

Code: Select all

on mouseUp
   set the wholematches to true -- used to force lineoffset below to match only whole lines
   Ask "Create New User" 
   if it is not empty then  -- if it is empty nothing is done. Outermost if.
      put it into sNewUser -- otherwise dump the value into sNewUser. Doesn't really need to be a scriptlocal, its a single use var
      if lineoffset(sNewUser,field "field") > 0 then -- check lineoffset to see if user already exists
         answer error merge("User [[sNewUser]] already exists!") -- if lineoffset is > 0 there was a match, show the error
         exit mouseup -- and exit the mouseup doing nothing further
      else -- if the user is new, add it to the field, make sure the last line is hilited
         adduser sNewUser -- could put the adduser routine inline here. Either works.
         get createUserDir(sNewUser) -- called as a function passing the username variable. 
         if it is not "true" then answer error "There was an error: " & it -- checks for error returned by createUserDir
      end if
   end if
end mouseUp

command adduser pUser
   put the number of lines of field "Field" into tLineCount
   put pUser into line tLineCount + 1 of card field "Field"
   set the hilitedlines of field "field" to (tLineCount + 1) -- make sure the last line added is hilited
end adduser

function createUserDir pUser
   put specialfolderpath("documents") & "/" & pUser & "/" into tCurrentUserFolder 
   if there is not a folder tCurrentUserFolder then
      create folder tCurrentUserFolder
      if the result is not empty then return the result --if the command returns some unknown error, return it
      return true -- otherwise return true
   else
      return "Folder already exists!" -- return custom error if folder is already there. 
   end if
end createUserDir

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: iOS Users

Post by sturgis » Mon Nov 26, 2012 3:23 am

Just noticed a few issues with the delete user. First, you "put the files" into a variable so that you can iterate through them and delete, but you don't set the defaultfolder to point to the right place, so you're risking deletion of all kinds of files in a potentially unspecified location.

Then your repeat loop repeats with x = 1 to the number of lines..
and you delete file x. In this case x contains a number not the name of a file.

You would have to do "delete file (line x of myVariable) to get it to work that way. That way (line x of my variable) evaluates to a file name.

Its probably easier to use a repeat for each type of loop in this case.

Code: Select all

repeat for each line tLIne in myVariable
delete file tLine
end repeat
However, make DARN sure you're deleting from the right place.

Follows is an adjusted (but untested) script for the user deletion button.

Code: Select all

on mouseUp
   put the hilitedLine of fld "Field" into lineToDelete 
   put  the selectedText of field "Field" into userToDelete
   if userToDelete is not empty 
   then 
      answer "Delete" && userTodelete & "?" with "Yes" or "No"
      if it is "Yes" then 
         deleteUser userToDelete
         delete line lineToDelete of fld "Field" 
      end if 
   end if
end mouseUp


on deleteUser pUser
   put the defaultfolder into tOldFolder -- save the current default folder
   put specialfolderpath("documents") & "/" & pUser & "/" into tCurrentUserFolder
   if there is a folder tCurrentUserFolder then 
      set the defaultfolder to tCurrentUserFolder -- point to the right place!
      put the files into tFileList -- get the file list from the defaultfolder set from above. 
      repeat for each line tLine in tFileList
               delete file tLine
   end repeat
   set the defaultfolder to tOldFolder -- revert to wherever we were pointing before
   delete folder tCurrentUserFolder -- delete the user folder
   put "" into field "FieldData" 
end if
end deleteUser

Post Reply