0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

iOS18 SDK で一部バージョンの Unity の Application.OpenURL が機能しない

Posted at

経緯

ビルド環境:
Xcode16.0
Unity2021.3.36f1

Xcode16(iOS18 SDK)でビルドした iOS のバイナリで、外部ブラウザが開けない問題が発生。
アプリ強制アップデート時にストアに飛ばすリンクや、問い合わせのためにメーラーを起動するリンクが軒並み動作しない状態となった。
この条件下では iOS では Application.OpenURL が正しく動作しないことが判明。

調べたところ、Unity のバージョン 2021.3 などで発生しているとのこと。(2022 以降の LTS では発生しないという情報が多かった。)
「2021 の最新の LTS で直る予定」という記事も見たが、この時点で最新付近のバージョンのリリースノートにはそれらしい記述が見当たらず…
運営の都合上、一週間以内に対応しないとマズいということになったので、コミュニティでの記事を参考に iOS の場合のみネイティブの処理を呼び出すように対応しました。

対応の備忘録で記しておきます。

追加ファイル

CustomOpenURL.mm
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

extern "C" void _CustomOpenURL(const char* url) {
    NSString* urlString = [NSString stringWithUTF8String:url];
    NSURL* nsUrl = [NSURL URLWithString:urlString];

    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:nsUrl options:@{} completionHandler:^(BOOL success) {
            if (!success) {
                // 失敗時にすることがあればここに
            }
        }];
    } else {
        BOOL success = [[UIApplication sharedApplication] openURL:nsUrl];
        if (!success) {
            // 失敗時にすることがあればここに
        }
    }
}

これを Assets->Plugins->iOS に追加してビルド。

OpenURLControl.cs
using System.Runtime.InteropServices;

...

#if UNITY_IOS
        [DllImport("__Internal")]
        private static extern void _CustomOpenURL(string url);
#endif

...

#if UNITY_IOS
        _CustomOpenURL(url);
#else
        Application.OpenURL(url);
#endif

呼び出し側でこのように実装しました。

今のところ動作的には問題なさそうなので、Unity のバージョンアップを行うまでは間に合わせで、この対応で様子を見ようと思います。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?