2016-10-14 46 views
0

進行網絡調用時,我有Apple Watch上的問題。蘋果手錶設備崩潰與SandboxViolation拒絕(1)網絡出境

它正常工作在模擬器上,但是當部署到設備我看到這在設備中的日誌:

MyAppleWatch內核(沙盒)[0]:SandboxViolation:MyWatchApp(203)拒絕(1)網絡-outbound /私營/ var/run中/ mDNSResponder

用於進行調用的代碼使用改裝完成,工程在模擬器上,所以我會想象它是不是原因,但如果有必要,我將它張貼。

我在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到:

enter image description here

只是在接下來要嘗試的一些損失。任何幫助將不勝感激。

回答

1

好吧,我得到它的工作,首先,我改變了我的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,而不是使用改裝+ 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; } 
    } 

希望這有助於。