Problem accessing network from Quick Look Preview Extension

自作多情 提交于 2020-02-06 19:06:56

问题


I have created an iOS Quick Look Preview Extension which gets called by iOS whenever the user does a Spotlight search and previews a result from my app. The preview loads fine, but when I try to access a cloud file from within the extension it fails with DNS error, saying Operation not permitted.

I have found that on MacOS I need to disable sandboxing or, if using sandbox, give permission for network access, but this is iOS. There are no such settings here. How can I make it work?

Here is my code for the extension

#import "PreviewViewController.h"
#import <QuickLook/QuickLook.h>

@interface PreviewViewController () <QLPreviewingController>

@end

@implementation PreviewViewController

- (void)preparePreviewOfSearchableItemWithIdentifier:(NSString *)identifier queryString:(NSString * _Nullable)queryString completionHandler:(void (^)(NSError * _Nullable))handler {
    NSURL *url = [NSURL URLWithString:@"https://myclouddomain.com"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"GET";
    // [request setValue:headerFields[key] forHTTPHeaderField:key];
    // request.HTTPBody = (NSData *)bodyData;

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

    NSURLSessionDataTask *task = [urlSession dataTaskWithRequest:request completionHandler:^(NSData* data, NSURLResponse* response, NSError* error) {
        if (error == nil) {
            NSInteger responseCode = [(NSHTTPURLResponse*)response statusCode];
            NSLog(@"OK (%d)", (int)responseCode);
        } else {
            NSLog(@"Failed");
        }
        handler (nil);
    }];

    [task resume];
}

Here is the log output

2019-04-03 10:42:20.809768+0200 QuickLookPreview[7655:3650321] dnssd_clientstub ConnectToServer: connect()-> No of tries: 1

2019-04-03 10:42:21.815704+0200 QuickLookPreview[7655:3650321] dnssd_clientstub ConnectToServer: connect()-> No of tries: 2

2019-04-03 10:42:22.821499+0200 QuickLookPreview[7655:3650321] dnssd_clientstub ConnectToServer: connect()-> No of tries: 3

2019-04-03 10:42:23.827993+0200 QuickLookPreview[7655:3650321] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:4 Err:-1 Errno:1 Operation not permitted

2019-04-03 10:42:23.828797+0200 QuickLookPreview[7655:3650321] [] nw_resolver_create_dns_service_locked [C1] DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563)

2019-04-03 10:42:23.831035+0200 QuickLookPreview[7655:3650321] TIC TCP Conn Failed [1:0x283aea040]: 10:-72000 Err(-65563)

2019-04-03 10:42:23.833948+0200 QuickLookPreview[7655:3650323] Task .<1> HTTP load failed (error code: -1003 [10:-72000])

2019-04-03 10:42:23.834811+0200 QuickLookPreview[7655:3650323] Task .<1> finished with error - code: -1003

Failed

来源:https://stackoverflow.com/questions/55493267/problem-accessing-network-from-quick-look-preview-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!