iOS/macOS 用のブラウザーと WebView をカスタマイズする

対話型の認証には Web ブラウザーが必要です。 iOS および macOS 10.15 以降では、Microsoft Authentication Library (MSAL) は既定でシステム Web ブラウザー (アプリの上に表示される場合があります) を使用して、対話型認証を実行してユーザーをサインインします。 システム ブラウザーを使用すると、シングル サインオン (SSO) 状態を他のアプリケーションや Web アプリケーションと共有できるという利点があります。

次のような Web コンテンツを表示するための他のオプションに構成をカスタマイズすることで、エクスペリエンスを変更できます。

iOS の場合のみ:

iOS および macOS の場合:

macOS 用 MSAL では、古い OS バージョンでの WKWebView のみがサポートされます。 ASWebAuthenticationSession は macOS 10.15 以降でのみサポートされています。

システム ブラウザー

iOS の場合、 ASWebAuthenticationSessionSFAuthenticationSessionSFSafariViewController はシステム ブラウザーと見なされます。 macOS の場合、 ASWebAuthenticationSession のみを使用できます。 一般に、システム ブラウザーは、Cookie やその他の Web サイト データを Safari ブラウザー アプリケーションと共有します。

既定では、MSAL は iOS バージョンを動的に検出し、そのバージョンで使用可能な推奨システム ブラウザーを選択します。 iOS 12 以降では、 ASWebAuthenticationSessionされます。

iOS の既定の構成

バージョン Web ブラウザ
iOS 12 以降 ASWebAuthenticationSession
iOS 11 SFAuthenticationSession
iOS 10 SFSafariViewController

macOS の既定の構成

バージョン Web ブラウザ
macOS 10.15 以降 ASWebAuthenticationSession
その他のバージョン WKWebView

開発者は、MSAL アプリ用に別のシステム ブラウザーを選択することもできます。

  • SFAuthenticationSession は iOS 11 バージョンの ASWebAuthenticationSessionです。
  • SFSafariViewController は、より一般的な目的であり、Web を参照するためのインターフェイスを提供し、ログイン目的でも使用できます。 iOS 9 および 10 では、Cookie やその他の Web サイト データは Safari と共有されますが、iOS 11 以降では共有されません。

アプリ内ブラウザー

WKWebView は、Web コンテンツを表示するアプリ内ブラウザーです。 Cookie や Web サイトのデータは、他の WKWebView インスタンスや Safari ブラウザーと共有されません。 WKWebView は、iOS と macOS の両方で使用できるクロスプラットフォーム ブラウザーです。

使用するブラウザーは、Cookie の共有方法により SSO エクスペリエンスに影響します。 次の表は、ブラウザーごとの SSO エクスペリエンスの概要を示しています。

テクノロジ ブラウザーの種類 iOS の可用性 macOS の可用性 Cookie とその他のデータを共有する MSAL の可用性 SSO
ASWebAuthenticationSession システム iOS12 以降 macOS 10.15 以降 はい iOS および macOS 10.15 以降 Safari インスタンスを含む
SFAuthenticationSession システム iOS 11以降 N/a はい iOS のみ Safari インスタンスを含む
SFSafariViewController システム iOS 11以降 N/a No iOS のみ いいえ**
SFSafariViewController システム iOS10 N/a はい iOS のみ Safari インスタンスを含む
WKWebView アプリ内 iOS 8以降 macOS 10.10 以降 No iOS と macOS いいえ**

** SSO を機能させるには、トークンをアプリ間で共有する必要があります。 これには、iOS 用のMicrosoft Authenticatorなどのトークン キャッシュまたはブローカー アプリケーションが必要です。

要求の既定のブラウザーを変更する

MSALWebviewParametersで次のプロパティを変更することで、UX 要件に応じてアプリ内ブラウザーまたは特定のシステム ブラウザーを使用できます。

@property (nonatomic) MSALWebviewType webviewType;

対話型要求ごとの変更

各要求は、MSALInteractiveTokenParameters.webviewParameters.webviewType API に渡す前に acquireTokenWithParameters:completionBlock: プロパティを変更することで、既定のブラウザーをオーバーライドするように構成できます。

さらに、MSAL では、WKWebView プロパティを設定してカスタム MSALInteractiveTokenParameters.webviewParameters.customWebViewを渡すことがサポートされています。

例えば次が挙げられます。

Objective-C

UIViewController *myParentController = ...;
WKWebView *myCustomWebView = ...;
MSALWebviewParameters *webViewParameters = [[MSALWebviewParameters alloc] initWithAuthPresentationViewController:myParentController];
webViewParameters.webviewType = MSALWebviewTypeWKWebView;
webViewParameters.customWebview = myCustomWebView;
MSALInteractiveTokenParameters *interactiveParameters = [[MSALInteractiveTokenParameters alloc] initWithScopes:@[@"myscope"] webviewParameters:webViewParameters];

[app acquireTokenWithParameters:interactiveParameters completionBlock:completionBlock];

Swift

let myParentController: UIViewController = ...
let myCustomWebView: WKWebView = ...
let webViewParameters = MSALWebviewParameters(authPresentationViewController: myParentController)
webViewParameters.webviewType = MSALWebviewType.wkWebView
webViewParameters.customWebview = myCustomWebView
let interactiveParameters = MSALInteractiveTokenParameters(scopes: ["myscope"], webviewParameters: webViewParameters)

app.acquireToken(with: interactiveParameters, completionBlock: completionBlock)

カスタム Web ビューを使用する場合、通知は、表示されている Web コンテンツの状態を示すために使用されます。次に例を示します。

/*! Fired at the start of a resource load in the webview. The URL of the load, if available, will be in the @"url" key in the userInfo dictionary */
extern NSString *MSALWebAuthDidStartLoadNotification;

/*! Fired when a resource finishes loading in the webview. */
extern NSString *MSALWebAuthDidFinishLoadNotification;

/*! Fired when web authentication fails due to reasons originating from the network. Look at the @"error" key in the userInfo dictionary for more details.*/
extern NSString *MSALWebAuthDidFailNotification;

/*! Fired when authentication finishes */
extern NSString *MSALWebAuthDidCompleteNotification;

/*! Fired before ADAL invokes the broker app */
extern NSString *MSALWebAuthWillSwitchToBrokerApp;

オプション

MSAL でサポートされているすべての Web ブラウザーの種類は、MSALWebviewType 列挙型で宣言されています

typedef NS_ENUM(NSInteger, MSALWebviewType)
{
    /**
     For iOS 11 and up, uses AuthenticationSession (ASWebAuthenticationSession or SFAuthenticationSession).
     For older versions, with AuthenticationSession not being available, uses SafariViewController.
     For macOS 10.15 and above uses ASWebAuthenticationSession
     For older macOS versions uses WKWebView
     */
    MSALWebviewTypeDefault,

    /** Use ASWebAuthenticationSession where available.
     On older iOS versions uses SFAuthenticationSession
     Doesn't allow any other webview type, so if either of these are not present, fails the request*/
    MSALWebviewTypeAuthenticationSession,

#if TARGET_OS_IPHONE

    /** Use SFSafariViewController for all versions. */
    MSALWebviewTypeSafariViewController,

#endif
    /** Use WKWebView */
    MSALWebviewTypeWKWebView,
};

次のステップ

認証フローとアプリケーション シナリオの詳細を確認する