LoginSignup
3
1

More than 5 years have passed since last update.

Web Share APIはHTTPSかlocalhostでしか動作しない

Posted at

最近iOSに対応したばかりのWeb Share APIですが、実装する際に嵌ったのでメモ。

デモ

こんな感じで、navigator.share を使って起動させます。

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Web Share API</title>
  </head>
  <body>
    <button onclick="share()">シェアする</button>
    <script type="text/javascript">
      function share() {
        navigator.share({
         title: 'Example',
         text: 'テスト用です',
         url: 'https://example.com',
       });
      }
    </script>
  </body>
</html>

localhostで実行した場合

うまく動作します。
スクリーンショット 2019-04-30 23.26.22.png

IP指定かつHTTPで動作させた場合

TypeError: navigator.share is not a function. エラーが出て、起動しません。
スクリーンショット 2019-04-30 23.27.43.png

まとめ

Web Share APIをテストする際は、localhostでテストしましょう。
localhostでも動作しない仕様だったらテストが困難でしたが、動作して良かった……

3
1
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
3
1