5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Xamarin:Storyboard: 指定したビューに強制的に遷移させる

Last updated at Posted at 2014-07-01
  • カスタムURLスキームでサイトからアクティベーションコードをもらって端末アプリをアクティベートするユースケース

Activationを行うViewを追加

  • ビュー (ActivationController)追加
  • Storyboard ID を指定する

image

  • Activation Codeを表示させるラベル(ActivateLabel)

image

  • Activateさせるボタン(ActivateButton)

image

カスタムスキームを定義

  • Info.plist
  • Advancedタブ
  • "qrcode"という “URL Schemes”を定義

Activationのビューのラベルにcodeをセット#

  • 実際は ActivateButtonのタッチイベントのハンドラも定義するが省略

    using System;
    using MonoTouch.Foundation;
    using MonoTouch.UIKit;
    using System.CodeDom.Compiler;
    
    namespace QrCoder
    {
    	partial class ActivationController : UIViewController
    	{
    		string _activation_code = null;
    		
    		public string ActivationCode
    		{
    			set{ this._activation_code = value; }
    		}
    
    		public override void ViewDidAppear (bool animated)
    		{
    			base.ViewDidAppear (animated);
    			this.ActivateCode.Text = _activation_code ?? "" ;
    		}
    	}
    }

AppDelegateでカスタムスキームで起動するように設定

  • OpenUrlをオーバーライド

		ActivationController _activation_controller = null; 

		public override bool OpenUrl (
			UIApplication application, 
			NSUrl url, string sourceApplication, NSObject annotation)
		{
			// url :  qrcode://host/path?query#fragment 

            // ビューコントローラをStoryboardからロードしてクラス作成する
			if (_activation_controller == null) {
				_activation_controller = this.Window.RootViewController.Storyboard.InstantiateViewController (
					"ActivationController"
				) as ActivationController;
			}

            // URL Query
			var query = System.Web.HttpUtility.ParseQueryString (url.Query); // System.Web.WebServices

			if (string.IsNullOrEmpty (query ["code"]))
                // codeパラメータがなければActivateしない
				return false;

            // ビューにコードをセット
			_activation_controller.ActivationCode = query ["code"].ToString ();

            // ナビゲーションコントローラを取得し、ActivationControllerビューをそこにプッシュする
			var controller = (UINavigationController)this.Window.RootViewController;
			controller.PushViewController (_activation_controller, true);

            // OpenUrlが処理されてUIが変わります
			return true;
		}

Safariからテスト

  • qrcode://me/activate?code=activation_code でActivationビューが起動して、それにコードが設定せれている
  • "Activate Now"をクリックして、端末のアプリをアクティベートする
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?