Apple Watch 设备因 SandboxViolation deny(1) 网络出站而崩溃

Apple Watch Device crashing with SandboxViolation deny(1) network-outbound

我在 Apple Watch 上进行网络通话时遇到问题。

它在模拟器上运行良好,但当部署到设备时,我在设备日志中看到:

MyAppleWatch kernel(Sandbox)[0] : SandboxViolation: MyWatchApp(203) deny(1) network-outbound /private/var/run/mDNSResponder

拨打电话的代码是使用改装完成的,并且可以在模拟器上运行,所以我想这不是原因,但如果需要我会 post 它。

我已经在 WatchExtensionApp 的 Info.plist 中设置了这些值(而不是在 WatchApp 中将它们设置为这些键)

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>my.domain.com</key>
        <dict>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

我将 WatchApp 和 WatchExtensionApp 设置为:

对于接下来要尝试什么有点不知所措。任何帮助将不胜感激。

好的,我成功了,首先我将 Info.plist 更改为:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>my.domain.com</key>
        <dict>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
    </dict>
</dict>

我不得不更改对我们的所有网络调用 NSUrlSession,而不是使用 Refit + HttpClient。所以我假设 HttpClient 中有 watchOS 不喜欢的东西,或者 watchos 只适用于 NSUrlSession。无论如何,这是我的一个电话的示例:

    public async Task<HttpResponse> MakeCall(NSMutableUrlRequest request)
    {
        var result = new HttpResponse();
        var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
        var session = NSUrlSession.FromConfiguration(config);

        var resultUser = await session.CreateDataTaskAsync(request);
        var response = resultUser.Response as NSHttpUrlResponse;
        if (response != null)
        {
            result.StatusCode = (int)response.StatusCode;
            result.Content = NSString.FromData(resultUser.Data, NSStringEncoding.UTF8).ToString();
        }
        return result;
    }

    public async Task<HttpResponse> GetStuffWithParameter(string parameter, string authorization)
    {
        var url = new NSUrl($"https://www.domain.com/api/stuff/{parameter}");

        var header = new NSMutableDictionary();
        header.SetValueForKey(new NSString(authorization), new NSString("Authorization"));
        header.SetValueForKey(new NSString("application/json"), new NSString("Content-Type"));

        var request = new NSMutableUrlRequest(url)
        {
            Headers = header,
            HttpMethod = "GET"
        };

        return await MakeCall(request);
    }

    public class HttpResponse
    {
        public int StatusCode { get; set; }
        public object Content { get; set; }
    }

希望这对您有所帮助。