fc2ブログ

記事一覧

アプリ間での呼び出しとパラメータの受け渡しについて(Deep Link / App Links)-iOS編- | Xamarin.Forms


前回の記事にてAndroidでのアプリ間の連携方法についてご紹介しました。今回はiOSでの連携方法についてご紹介いたします。具体的には呼び出し元のアプリAから呼び出し先のアプリBを呼び出し、パラメータを受け渡す方法についてご説明いたします。


前提条件
・Windows10 Pro 64Bit
・Visual Studio 2015 Community Update3
・Xamarin 4.3.0.795 (NuGet Xamarin.Forms 2.3.4.247)
・macOS Sierra 10.12.4 / Xcode8.3.1 / Xamarin.iOS 10.6.0.10



1.言葉の定義

まず始めに、説明させていただく言葉の定義について
・アプリA:呼び出し元のアプリ(通常のアプリ)
・アプリB:呼び出し先のアプリ(ファイルを開く為のエクスプローラ型のアプリ)



2.PCLの記述

前回と同じです。
それぞれのアプリにDependencyServiceから利用する為のインターフェースを用意します。

アプリAのIApplicationService.cs
namespace AppNameA.Services
{
    //DependencyServiceから利用する
    public interface IApplicationService
    {
        void Start(string param);
        event EventHandler ReturnParamSetted;
        string GetReturnParam();
    }
}

アプリBのIApplicationService.cs
namespace AppNameB.Services
{
    //DependencyServiceから利用する
    public interface IApplicationService
    {
        //Intentを戻す機能
        void ReturnApp(string param);
    }
}



3.iOSの実装方法

(1)アプリAからアプリBを呼び出し、パラメータを渡します。
(2)アプリBからアプリAを呼び出し、パラメータを渡します。

アプリAのInfo.plist
<plist version="1.0">
<dict>
  <key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>AppNameA</string>
      </array>
    </dict>
  </array>
  <key>LSApplicationQueriesSchemes</key>
  <array>
    <string>AppNameB</string>
  </array>
</dict>
</plist>


アプリAのApplicationService.cs
[assembly: Dependency(typeof(ApplicationService))]
namespace AppNameA.iOS.Services
{
    public class ApplicationService : IApplicationService
    {
        private const string LINK_URL = @"AppNameB://";

        public static ApplicationService Instance = null;
        public event EventHandler ReturnParamSetted;
        public static string ReturnParam = String.Empty;

        public void Start(string param)
        {
            //現在のインスタンスを保持しておく
            ApplicationService.Instance = this;

            Foundation.NSUrl url = Foundation.NSUrl.FromString(LINK_URL + param);  //パラメータの文字列をセットする

            if (!UIApplication.SharedApplication.CanOpenUrl(url))
            {
                //アプリBがインストールされていない場合はアプリBのAppStoreサイトを開く
                url = Foundation.NSUrl.FromString("https://itunes.apple.com/jp/app/AppNameB/id9999999999?l=ja&mt=8");
            }
            //アプリBを呼び出す
            UIApplication.SharedApplication.OpenUrl(url);
        }
        //戻りのパラメータを取得した際に呼び出す。
        public void OnReturnParamSetted()
        {
            if (ApplicationService.Instance != null)
            {
                ApplicationService.Instance.ReturnParamSetted(null, new EventArgs());
            }
        }
        //PCLからパラメータを取得する
        public string GetReturnParam()
        {
            return ReturnParam;
        }
    }
}


アプリAのAppDelegate.cs
private const string APP_URL = @"AppNameA://";
// For iOS 9 or newer
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    if (this.IsReturnedApp(url))
    {
        return true;
    }
}

// For iOS 8 and older
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
    if (this.IsReturnedApp(url))
    {
        return true;
    }
}

//アプリBからの戻りかどうかを判定する
private bool IsReturnedApp(NSUrl url)
{
    if (url != null &&
        !String.IsNullOrEmpty(url.ToString()))
    {
        //アプリBからの戻りの場合、urlにアプリAの文字列が含まれているはず
        if (url.ToString().Contains(APP_URL))
        {
            ApplicationService.ReturnParam = url.ToString().ToLower().Replace(APP_URL, "");
            ApplicationService.Instance.OnReturnParamSetted();
            return true;
        }
    }
    return false;
}


アプリBのInfo.plist
<plist version="1.0">
<dict>
  <key>CFBundleURLTypes</key>
  <array>
    <dict>
      <key>CFBundleURLSchemes</key>
      <array>
        <string>AppNameB</string>
      </array>
    </dict>
  </array>
  <key>LSApplicationQueriesSchemes</key>
  <array>
    <string>AppNameA</string>
  </array>
</dict>
</plist>


アプリBのApplicationService.cs
[assembly: Dependency(typeof(ApplicationService))]
namespace AppNameB.iOS.Model.Services
{
    public class ApplicationService : IApplicationService
    {
        private const string LINK_URL = @"AppNameA://";

        public void ReturnApp(string param)
        {
            Foundation.NSUrl url = Foundation.NSUrl.FromString(LINK_URL + param);

            if (!UIApplication.SharedApplication.CanOpenUrl(url))
            {
                //アプリAがインストールされていない場合はアプリAのAppStoreサイトを開く
                url = Foundation.NSUrl.FromString("https://itunes.apple.com/us/app/AppNameA/id9999999999?l=ja&ls=1&mt=8");
            }
//アプリAを呼び出す
            UIApplication.SharedApplication.OpenUrl(url);
        }     
    }
}


アプリBのAppDelegate.cs
// For iOS 9 or newer
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    //パラメータを取得する
    string param = url.ToString().ToLower().Replace("AppNameB://", "");
    return true;
}

// For iOS 8 and older
public override bool OpenUrl(UIApplication app, NSUrl url, string sourceApplication, NSObject annotation)
{
    //パラメータを取得する
    string param = url.ToString().ToLower().Replace("AppNameB://", "");
    return true;
}



4.使用方法

前回のAndroid編と同じようにPCLで記述可能です。
アプリAのTestPage.xaml.cs
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
OnButtonClick(object sender, EventArgs e)
{
    //イベントの紐づけ
    DependencyService.Get<IApplicationService>().ReturnParamSetted += this.OnParameterSetted;
//アプリBを起動してパラメータを受け渡す。
    string param = "test";
    DependencyService.Get<IApplicationService>().Start(param);
}
void OnParameterSetted(object sender, EventArgs e)
{
    //アプリBでセットしたパラメータを取得する
    string param = DependencyService.Get<IApplicationService>().GetReturnParam();
}
}

アプリBのTestPage.xaml.cs
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
OnButtonClick(object sender, EventArgs e)
{
//アプリBを終了してアプリAにパラメータを受け渡す。
    string param = "returnvalue";
    DependencyService.Get<IApplicationService>().ReturnApp(param);
}
}



5.Androidの実装方法

前回の記事にてご紹介しております。




当ブログの内容をまとめた Xamarin逆引きメニュー は以下のURLからご覧になれます。
https://itblog.dynaspo.com/blog-entry-81.html


関連記事

コメント

コメントの投稿

※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。

 入力されていないコメントには返信しませんのであらかじめご了承くださいませ。

※ニックネームでも良いので必ずご入力ください。

    

※必ずご入力ください。

    
    

※必ずご入力ください。

※技術的な質問には環境やエラーについて正確かつ詳細にお教えください。

・正確なエラーの内容

・Windowsのバージョン番号

・Visual Studioのバージョン

・機器の型番

・アプリやソフトのバージョン

    

カテゴリ別記事一覧

広告

プロフィール

石河 純


著者名 :石河 純
自己紹介:素人上がりのIT技術者。趣味は卓球・車・ボウリング

IT関連の知識はざっくりとこんな感じです。
【OS関連】
WindowsServer: 2012/2008R2/2003/2000/NT4
Windows: 10/8/7/XP/2000/me/NT4/98
Linux: CentOS RedHatLinux9
Mac: macOS Catalina 10.15 / Mojave 10.14 / High Sierra 10.13 / Sierra 10.12 / OSX Lion 10.7.5 / OSX Snow Leopard 10.6.8
【言語】
VB.net ASP.NET C#.net Java VBA
Xamarin.Forms
【データベース】
Oracle 10g/9i
SQLServer 2016/2008R2/2005/2000
SQLAnywhere 16/11/8
【BI/レポートツール】
Cognos ReportNet (IBM)
Microsoft PowerBI
ActiveReport (GrapeCity)
CrystalReport
【OCX関連】
GrapeCity InputMan SPREAD MultiRow GridView
【ネットワーク関連】
CCNP シスコ技術者認定
Cisco Catalyst シリーズ
Yamaha RTXシリーズ
FireWall関連
【WEB関連】
SEO SEM CSS jQuery IIS6/7 apache2

休みの日は卓球をやっています。
現在、卓球用品通販ショップは休業中です。