最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]NSInternalInconsistencyException’, reason: ‘UIDocumentInteractionController: invalid scheme (null). Only the file scheme is supported

Swift crifan 4423浏览 0评论

代码:

                self.fileItem.downloadedFilename = subDirectory + "/" + self.fileItem.name
                print("self.fileItem.downloadedFilename=\(self.fileItem.downloadedFilename)")
               
                let docDirUrl = FileDirectory.applicationDirectory(NSSearchPathDirectory.DocumentDirectory)
                print("docDirUrl=\(docDirUrl)")
                ///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … ocuments
                let fileFullUrl = docDirUrl?.URLByAppendingPathComponent(self.fileItem.downloadedFilename, isDirectory: false)
                print("fileFullUrl=\(fileFullUrl)")
                ///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … %A1.docx
//                let openFileVC = OpenFileViewController(fileUrl: NSURL(fileURLWithPath: self.fileItem.downloadedFilename, relativeToURL: docDirUrl))
                let openFileVC = OpenFileViewController(fileUrl:fileFullUrl!)
    init(fileUrl:NSURL){
        self.fileUrl = fileUrl
        self.dic = UIDocumentInteractionController(URL: self.fileUrl)
        super.init(nibName: nil, bundle: nil)
    }

中的:

self.dic = UIDocumentInteractionController(URL: self.fileUrl)

出错:

Printing description of docDirUrl:
▿ Optional(/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … ocuments)
  – Some : /Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … ocuments
fileFullUrl=Optional(/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … %A1.docx)
2016-01-19 10:45:39.267 JianDao[21652:1222643] *** Assertion failure in -[UIDocumentInteractionController setURL:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UIDocumentInteractionController.m:1106
2016-01-19 10:45:39.345 JianDao[21652:1222643] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘UIDocumentInteractionController: invalid scheme (null).  Only the file scheme is supported.’
*** First throw call stack:
(
    0   CoreFoundation                      0x013f9a14 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x036eae02 objc_exception_throw + 50
    2   CoreFoundation                      0x013f98aa +[NSException raise:format:arguments:] + 138
    3   Foundation                          0x01a3bd26 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
    4   UIKit                               0x02959fad -[UIDocumentInteractionController setURL:] + 282
    5   UIKit                               0x029597e5 -[UIDocumentInteractionController initWithURL:] + 88
    6   UIKit                               0x02959773 +[UIDocumentInteractionController interactionControllerWithURL:] + 71
    7   JianDao                             0x0028ed31 _TTOFCSo31UIDocumentInteractionControllerCfMS_FT3URLCSo5NSURL_S_ + 81
    8   JianDao                             0x0028d86b _TFC7JianDao22OpenFileViewControllercfMS0_FT7fileUrlCSo5NSURL_S0_ + 123
    9   JianDao                             0x0028d9fb _TFC7JianDao22OpenFileViewControllerCfMS0_FT7fileUrlCSo5NSURL_S0_ + 91
    10  JianDao                             0x000babec _TFC7JianDao24FileManageViewController27downloadFileCompleteHandlerfS0_FTSS10downloadOkSb8respDataGSqCSo6NSData_8errorStrGSqSS__T_ + 5884
    11  JianDao                             0x000bc810 _TPA + 96
    12  JianDao                             0x001fc2f9 _TFF7JianDao21downloadFromUrl_asyncFTSS23downloadCompleteHandlerFTSSSbGSqCSo6NSData_GSqSS__T__T_U_FCS_8ResponseT_ + 1545
    13  JianDao                             0x001f8f44 _TPA__TFF7JianDao21downloadFromUrl_asyncFTSS23downloadCompleteHandlerFTSSSbGSqCSo6NSData_GSqSS__T__T_U_FCS_8ResponseT_ + 260
    14  JianDao                             0x0019a3cb _TTRXFo_oC7JianDao8Response_dT__XFo_iS0__iT__ + 27
    15  JianDao                             0x00198e8a _TPA__TTRXFo_oC7JianDao8Response_dT__XFo_iS0__iT__ + 90
    16  JianDao                             0x0019a114 _TTRXFo_iC7JianDao8Response_iT__XFo_oS0__dT__ + 36
    17  JianDao                             0x001903da _TPA__TTRXFo_iC7JianDao8Response_iT__XFo_oS0__dT__ + 74
    18  JianDao                             0x0019a17c _TFFC7JianDao4HTTPW8onFinishGSqFCS_8ResponseT__U_FS1_T_ + 92
    19  JianDao                             0x00190545 _TPA__TFFC7JianDao4HTTPW8onFinishGSqFCS_8ResponseT__U_FS1_T_ + 101
    20  JianDao                             0x0019a3cb _TTRXFo_oC7JianDao8Response_dT__XFo_iS0__iT__ + 27
    21  JianDao                             0x001996ca _TPA__TTRXFo_oC7JianDao8Response_dT__XFo_iS0__iT__55 + 90
    22  JianDao                             0x001975b7 _TFC7JianDao15DelegateManager10URLSessionfS0_FTCSo12NSURLSession4taskCSo16NSURLSessionTask20didCompleteWithErrorGSqCSo7NSError__T_ + 1559
    23  JianDao                             0x001976b2 _TToFC7JianDao15DelegateManager10URLSessionfS0_FTCSo12NSURLSession4taskCSo16NSURLSessionTask20didCompleteWithErrorGSqCSo7NSError__T_ + 114
    24  CFNetwork                           0x009393e4 __51-[NSURLSession delegate_task:didCompleteWithError:]_block_invoke170 + 110
    25  Foundation                          0x01a6996f __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    26  Foundation                          0x0198d85f -[NSBlockOperation main] + 108
    27  Foundation                          0x0196dce4 -[__NSOperationInternal _start:] + 697
    28  Foundation                          0x0196da24 -[NSOperation start] + 83
    29  Foundation                          0x0196d862 __NSOQSchedule_f + 245
    30  libdispatch.dylib                   0x044129cd _dispatch_client_callout + 14
    31  libdispatch.dylib                   0x043f6650 _dispatch_queue_drain + 2227
    32  libdispatch.dylib                   0x043f5b04 _dispatch_queue_invoke + 570
    33  libdispatch.dylib                   0x043f87bb _dispatch_root_queue_drain + 550
    34  libdispatch.dylib                   0x043f858e _dispatch_worker_thread3 + 115
    35  libsystem_pthread.dylib             0x0473943e _pthread_wqthread + 1050
    36  libsystem_pthread.dylib             0x04736f72 start_wqthread + 34
)
libc++abi.dylib: terminating with uncaught exception of type NSException

如图:

搜:

UIDocumentInteractionController Only the file scheme is supported

ios – UIDocumentInteractionController "invalid scheme (null)" – Stack Overflow

去改为:

                let fullUrl = docDirUrl?.URLByAppendingPathComponent(self.fileItem.downloadedFilename, isDirectory: false)
                print("fullUrl=\(fullUrl)")
                ///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … %A1.docx
                let fileUrl = NSURL.fileURLWithPath(fullUrl!.path!)
                print("fileUrl=\(fileUrl)")
                //fileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/061FE00D-2CAC-4A8A-A2A5-E5EA58344B9B/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx
               
//                let openFileVC = OpenFileViewController(fileUrl: NSURL(fileURLWithPath: self.fileItem.downloadedFilename, relativeToURL: docDirUrl))
                let openFileVC = OpenFileViewController(fileUrl:fullUrl!)

结果问题依旧。。

搜:

Assertion failure UIDocumentInteractionController setURL  UIKit_Sim  UIDocumentInteractionController.m

iPhone- application crashes on click of menu action button in QLPreviewController ios 4.2 – Stack Overflow

How to print PDF file already opened in QLPreviewController using print button? [如何打印PDF文件中使用的打印按钮qlpreviewcontroller已经打开了?] – 问题-字节技术

我此处已经是:

file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/061FE00D-2CAC-4A8A-A2A5-E5EA58344B9B/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx

了啊。

Open PDF link to PDF Reader in iOS [在iOS打开PDF链接PDF阅读器] – 问题-字节技术

iOS SDK: Previewing and Opening Documents – Envato Tuts+ Code Tutorial

objective c – Open PDF link to PDF Reader in iOS – Stack Overflow

刚刚无语了,发现:

fileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/1848E9EA-416B-4DD5-BFB0-BA4C5FA7DB44/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx
self.fileUrl=/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … %A1.docx

即:

之前通过:

let fileUrl = NSURL.fileURLWithPath(fullUrl!.path!)

得到的文件的URL,结果通过同样的NSURL传入后,结果变成了:

没有file:开头的那种普通的文件路径了

所以改为:

                let fullUrl = docDirUrl?.URLByAppendingPathComponent(self.fileItem.downloadedFilename, isDirectory: false)
                print("fullUrl=\(fullUrl)")
                ///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Applicat … %A1.docx
//                let fileUrl = NSURL.fileURLWithPath(fullUrl!.path!)
//                print("fileUrl=\(fileUrl)")
                //fileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/061FE00D-2CAC-4A8A-A2A5-E5EA58344B9B/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx
               
                let fullFilePath = fullUrl!.path!
                print("fullFilePath=\(fullFilePath)")
                //fullFilePath=/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C9EAE23F-BCF3-4C2E-9D90-DD282CE8FBB2/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/张炳恋设计.docx
               
//                let openFileVC = OpenFileViewController(fileUrl: NSURL(fileURLWithPath: self.fileItem.downloadedFilename, relativeToURL: docDirUrl))
//                let openFileVC = OpenFileViewController(fileUrl:fullUrl!)
                let openFileVC = OpenFileViewController(fullFilePath:fullFilePath)
                self.showViewController(openFileVC, sender: self)
class OpenFileViewController: UIViewController, QLPreviewControllerDataSource{
//    init(fileUrl:NSURL){
    init(fullFilePath:String){
        self.fileUrl = NSURL.fileURLWithPath(fullFilePath)
        print("self.fileUrl=\(self.fileUrl)")
        //self.fileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C9EAE23F-BCF3-4C2E-9D90-DD282CE8FBB2/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx
        self.dic = UIDocumentInteractionController(URL: self.fileUrl)
        super.init(nibName: nil, bundle: nil)
    }
}

就可以了。

[总结]

iOS采用了SandBox沙箱技术

-》使得更安全

-》但也使得不同程序间共享文件数据更麻烦

-》不同程序间共享文件,往往用的是:UIDocumentInteractionController

-》也可以用:UIDocumentInteractionController来:预览文件,调用其他程序打开文件

此处,确保调用:

UIDocumentInteractionController(URL: self.fileUrl)

时传入的self.fileUrl是文件类型的URL

以file://开头的,比如:

file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C9EAE23F-BCF3-4C2E-9D90-DD282CE8FBB2/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx

即可。

对应的这种URL,是通过:

NSURL.fileURLWithPath(fullFilePath)

得到的。

另外还要注意到:

如果之前已经得到了file://类型的NSURL,但是传入函数后,则会变成普通的不带file://的NSURL!

所以,要注意这点。。。

所以,要在真正调用UIDocumentInteractionController传入file的url时确保你带file://类型的NSURL

转载请注明:在路上 » [已解决]NSInternalInconsistencyException’, reason: ‘UIDocumentInteractionController: invalid scheme (null). Only the file scheme is supported

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.225 seconds, using 22.18MB memory