私はコロナsdkでソーシャルアプリを作成しています。ユーザがクリックできるタブは2つあります。 1つはニュースフィードであり、もう1つはプロフィールです。彼らがプロフィールタブに行くと、私は彼らに写真とユーザー名を見せたい。問題は、ユーザーのユーザー名ではなく、入力した内容を示すコードがあることです。例えば、local userName = tostring(userName)
の場合、これはnil
を示します。このlocal userName = tostring("user's username")
はuser's username
です。ログイン時にユーザーのユーザー名を表示するにはどうすればよいですか?コロナsdkアプリでユーザ名を表示するには
profile.lua:
local composer = require("composer")
local scene = composer.newScene()
local widget = require("widget")
-- forward declare the text fields
local json = require("json")
local userName
local userNameText
local userName = tostring(userName)
display.newText(userName, display.contentCenterX, display.contentCenterY,
native.systemFont, 20)
local function networkListener(event)
if (event.isError) then
local alert = native.showAlert("Error Loading .", "Check your internet
connection .", { "Try again" } )
end
end
function scene:create(event)
local screenGroup = self.view
local background =
display.newImageRect("insta.jpg",display.contentWidth,display.contentHeight)
background.x = display.contentCenterX
background.y = display.contentCenterY
screenGroup:insert(background)
local userNameText = display.newText(userName, 200, 200, native.systemFont, 30
)
userNameText:setFillColor(1, 0, 0)
screenGroup:insert(userNameText)
end
local tabButtons = {
{
label = "#NewsFeed",
width = 52, height = 10,
id = "newsfeed",
size = 16,
onPress = function() composer.gotoScene("newsfeed"); end,
selected = true
},
{
label = "#Profile",
size = 16,
id = "profile",
onPress = function() composer.gotoScene("profile"); end,
selected = true
}
}
-- Create the widget
local tabBar = widget.newTabBar(
{
top = display.contentHeight -52,
width = display.contentWidth,
buttons = tabButtons,
}
)
function scene:show(event)
local phase = event.phase
if (phase == "will") then
print("Phase started")
elseif (phase == "did") then
print("phase on login")
end
composer.removeScene("login")
end
scene:addEventListener("show")
function scene:hide(event)
end
function scene:destroy(event)
end
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)
return scene
profile.php:
include("auth_login.php");
echo $_SESSION['username'];
auth_login.php:
if(!isset($_SESSION['username'])){
echo"Download the app";
die();
}
login.lua:
local composer = require("composer")
local scene = composer.newScene()
local widget = require("widget")
-- forward declare the text fields
local json = require("json")
local username
local pw
local function emptyFields(event)
if (username.text == "" or pw.text == "") then
local alert = native.showAlert("Empty fields", "Fill in all fields .", { "Try again" } )
return true
else
return false
end
end
local function networkListener(event)
if (event.isError) then
local alert = native.showAlert("Error Logging In", "Check your internet connection .", { "Try again" } )
else
if event.response == "success" then
-- put the code here to go to where the user needs to be
-- after a successful registration
composer.gotoScene("newsfeed")
else
-- put code here to notify the user of the problem, perhaps
-- a native.alert() dialog that shows them the value of event.response
-- and take them back to the registration screen to let them try again
local json = require("json")
print(json.prettify(event))
local alert = native.showAlert("Error Logging In", event.response , { "Try again" } )
end
end
end
local function userLogin(event)
if ("ended" == event.phase) then
if emptyFields() == true then
else
local parameters = {}
parameters.body = "Login=1&username=" .. username.text .. "&pw=" .. pw.text
local URL = "http://hash.host22.com/login.php"
network.request(URL, "POST", networkListener, parameters)
local headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Accept-Language"] = "en-US"
parameters.headers = headers
end
end
end
local function registerLink(event)
if ("ended" == event.phase) then
composer.gotoScene("register")
end
end
function scene:create(event)
local screenGroup = self.view
local background =
display.newImageRect("bg4.jpg",display.contentWidth,display.contentHeight)
background.x = display.contentCenterX
background.y = display.contentCenterY
screenGroup:insert(background)
myImage = display.newImage("hash.png")
-- position the image
myImage:translate(160, 70)
myText = display.newText("#Hash", 160, 140, native.systemFontBold, 40)
myText:setFillColor(1, 1, .5)
username = native.newTextField(160, 200, 180, 30) -- take the local off
since it's forward declared
username.placeholder = "Username"
screenGroup:insert(username)
pw = native.newTextField(160, 270,180, 30) -- take the local off since it's
forward declared
pw.isSecure = true
pw.placeholder = "Password"
screenGroup:insert(pw)
local Button3 = widget.newButton(
{
shape = "roundedRect",
left = 70,
top = 320,
id = "Login",
label = "Login",
onEvent = userLogin,
fillColor = { default={ 0, 1, 4, 0.7 }, over={ 1, 0.5, 0.8, 4 } },
labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } }
}
)
screenGroup:insert(Button3)
local Button4 = widget.newButton(
{
shape = "roundedRect",
left = 70,
top = 410,
id = "register",
label = "Register Here",
onEvent = registerLink,
fillColor = { default={ 2, 4, 0, 0.7 }, over={ 1, 3, 8, 4 } },
labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } }
}
)
screenGroup:insert(Button4)
end
function scene:show(event)
local phase = event.phase
if (phase == "will") then
print("Phase started")
elseif (phase == "did") then
print("phase on login")
end
composer.removeScene("register")
end
scene:addEventListener("show")
function scene:hide(event)
myText:removeSelf()
username:removeSelf()
pw:removeSelf()
myImage:removeSelf()
end
function scene:destroy(event)
end
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)
return scene
(私は私のウェブサイト上のPHPコードのすべてを行うと、それは、ユーザー名が表示されます。)で
は、あなたのコロナアプリで任意のログインシーンはありますか?アプリからあなたのサイトにログインする方法を説明してください。 –
はい。私はコロナとPHP側にログインコードを持っています。コードが必要ですか? – user6855714
私はルアlogiinコードが役に立つと思います。 –