iOS
Swiftでのサインイン/サインアップ実装ガイド
12 分
swiftを使用したログインとユーザー登録チュートリアル はじめに このセクションでは、back4appを使用して、シンプルなユーザー登録を持つアプリを作成する方法を説明します。 parse serverのコア機能 いつでも、このチュートリアルで構築された完全なプロジェクトにアクセスできます。 githubリポジトリ このクイックスタートを完了するには、次のものが必要です: xcode back4appで作成されたアプリ。 follow the 新しいparseアプリチュートリアル を参照して、back4appでparseアプリを作成する方法を学んでください。 back4appに接続されたiosアプリ。 注意: follow the parse sdk (swift)のインストールチュートリアル を参照して、back4appに接続されたxcodeプロジェクトを作成してください。 有料のapple developerアカウント。 始めましょう! 次のステップに従うことで、back4appデータベースでサインイン、サインアップ、ログアウトを行うアプリを構築できるようになります。 1 サインアップとログインのuiを設定して作成する xcodeに移動し、プロジェクトのメインフォルダにアクセスしてから、viewcontroller swiftファイルを開いて編集します。 viewcontroller swiftでは、ファイルの先頭にparseモジュールを含めることを確認してください。 1 import parse 3\ main storyboardに移動し、メインストーリーボードのviewcontrollerに4つのuitextfieldをドラッグします。テキストフィールドを中央に配置し、上部に2つ、下部に2つ配置します。さらに2つのuibuttonをビューにドラッグし、テキストフィールドの下に配置します。各ボタンにloader indicatorを1つずつドラッグします。 上のボタンのテキストを「サインイン」と設定します。下のボタンのテキストを「サインアップ」と設定します。テキストフィールドには「ユーザー名」と「パスワード」と表示させます。 4\ 次に、ストーリーボードのuitextfieldをviewcontrollerのプロパティに接続します。viewcontroller swiftの先頭に以下のプロパティを追加します。次に、ストーリーボードに移動し、各uitextfieldを右クリックしてリファレンスアウトレットをクリックし、viewcontrollerアイコンに線をドラッグして適切なフィールドに設定します。signinusernamefieldはサインインユーザー名フィールドに接続されます。など… このように見えるはずです 1 import uikit 2 import parse 3 4 class viewcontroller uiviewcontroller { 5 6 @iboutlet weak var txtusernamesignin uitextfield! 7 @iboutlet weak var txtpasswordsignin uitextfield! 8 @iboutlet weak var indicatorlogin uiactivityindicatorview! 9 10 @iboutlet weak var txtusernamesignup uitextfield! 11 @iboutlet weak var txtpasswordsignup uitextfield! 12 @iboutlet weak var indicatorsignup uiactivityindicatorview! 13 14 override func viewdidload() { 15 super viewdidload() 16 // do any additional setup after loading the view 17 } 18 19 @ibaction func signin( sender any) { 20 //todo 21 } 22 23 @ibaction func signup( sender any) { 24 //todo 25 } 26 27 } 2 サインイン機能を作成する サインイン機能内に以下のコードを追加します 1 @ibaction func signin( sender any) { 2 pfuser loginwithusername(inbackground self txtusernamesignin text!, password self txtpasswordsignin text!) { 3 (user pfuser?, error error?) > void in 4 if user != nil { 5 self displayalert(withtitle "login successful", message "") 6 } else { 7 self displayalert(withtitle "error", message error! localizeddescription) 8 } 9 } 10 } 3 サインアップ機能を作成する サインアップ関数内に次のコードを追加してください 1 @ibaction func signup( sender any) { 2 let user = pfuser() 3 user username = self txtusernamesignup text 4 user password = self txtpasswordsignup text 5 6 self indicatorsignup startanimating() 7 user signupinbackground {(succeeded bool, error error?) > void in 8 self indicatorsignup stopanimating() 9 if let error = error { 10 self displayalert(withtitle "error", message error localizeddescription) 11 } else { 12 self displayalert(withtitle "success", message "account has been successfully created") 13 } 14 } 15 } 4 ログアウト ログインすると、ログインしたユーザーを指すセッションオブジェクトが作成されます。ログインが成功すると、parseuser currentuser()はユーザーオブジェクトを返し、ダッシュボードにセッションオブジェクトが作成されます。そうでない場合、ターゲットユーザー名が存在しないか、パスワードが間違っていると、nullが返されます。 ログアウトするには、以下の手順に従ってください main storyboardに移動し、「logout」というuibuttonをドラッグし、このuibuttonとviewcontroller swiftの間にアクションを追加します。 この関数に次のコードを追加します 1 @ibaction func logout( sender any) { 2 pfuser logout() 3 } 5 アプリケーションコード 1 import uikit 2 import parse 3 4 class viewcontroller uiviewcontroller { 5 6 @iboutlet weak var txtusernamesignin uitextfield! 7 @iboutlet weak var txtpasswordsignin uitextfield! 8 @iboutlet weak var indicatorsignin uiactivityindicatorview! 9 10 @iboutlet weak var txtusernamesignup uitextfield! 11 @iboutlet weak var txtpasswordsignup uitextfield! 12 @iboutlet weak var indicatorsignup uiactivityindicatorview! 13 14 @iboutlet weak var btnlogout uibutton! 15 16 override func viewdidload() { 17 super viewdidload() 18 } 19 20 @ibaction func signin( sender any) { 21 pfuser loginwithusername(inbackground self txtusernamesignin text!, password self txtpasswordsignin text!) { 22 (user pfuser?, error error?) > void in 23 if user != nil { 24 self displayalert(withtitle "login successful", message "") 25 } else { 26 self displayalert(withtitle "error", message error! localizeddescription) 27 } 28 } 29 } 30 31 @ibaction func signup( sender any) { 32 let user = pfuser() 33 user username = self txtusernamesignup text 34 user password = self txtpasswordsignup text 35 36 self indicatorsignup startanimating() 37 user signupinbackground {(succeeded bool, error error?) > void in 38 self indicatorsignup stopanimating() 39 if let error = error { 40 self displayalert(withtitle "error", message error localizeddescription) 41 } else { 42 self displayalert(withtitle "success", message "account has been successfully created") 43 } 44 } 45 } 46 47 @ibaction func logout( sender any) { 48 pfuser logout() 49 } 50 51 func displayalert(withtitle title string, message string) { 52 let alert = uialertcontroller(title title, message message, preferredstyle alert) 53 let okaction = uialertaction(title "ok", style default) 54 alert addaction(okaction) 55 self present(alert, animated true) 56 } 57 58 } アプリケーションのuiは次のようになります 6 アプリをテストする アプリを実行し、いくつかのユーザーを作成し、登録後に再度ログインしてみてください。 次のurlでログインします back4appウェブサイト https //www back4app com/ アプリを見つけて、次をクリックします ダッシュボード ダッシュボード > コア コア > ブラウザ ブラウザ > ユーザー ユーザー 同じユーザーでログインとログアウトを試み、再度サインインしてください。 この時点で、以下のようにユーザーが表示されるはずです 注意 上記に表示されたコードを使用すると、ユーザーでログインするたびに、あなたの セッション セッション があなたの ダッシュボード ダッシュボード , しかし、ユーザーがログアウトすると、その特定の セッション セッション は終了します。また、ログインまたはサインアップの試行が失敗した場合、parse serverで開かれた セッション セッション は ダッシュボード ダッシュボード から削除されます。 完了しました! この段階で、parse serverのコア機能を使用して、back4appを通じてアプリにログイン、登録、またはログアウトできます!