Apple Watch Device crashing with SandboxViolation

2019-09-18 02:46发布

问题:

Im having an issue on the Apple Watch when making network calls.

It works fine on the simulator but when deployed to the device I see this in the device logs:

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

The code for making the call is done using refit and works on the simulator so I would imagine it is not the cause but I will post it if necessary.

I have set these values in the Info.plist of the WatchExtensionApp (and not set them in the WatchApp as these keys )

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

I set the WatchApp and WatchExtensionApp to:

Just at a bit of loss as to what to try next. Any help would be greatly appreciated.

回答1:

Ok I got it working, Firstly I changed my Info.plist to:

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

I had to change all my network calls to us NSUrlSession rather than using Refit + HttpClient. So I assume there is something in the HttpClient that watchOS doesn't like or watchos only works with NSUrlSession. Anyway here is a sample of one of my calls:

    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; }
    }

Hope this helps.