【问题标题】:swift custom NSURLProtocol errorswift自定义NSURLProtocol错误
【发布时间】:2015-09-26 02:11:25
【问题描述】:

我想加载远程url并使用本地资源(image,css,js...),所以我继承了NSURLProtocol。

import Foundation

class MyURLProtocol: NSURLProtocol {

    override class func canInitWithRequest(request: NSURLRequest) -> Bool {
        print(request.URL!.absoluteString)
        if request.URL!.absoluteString.hasSuffix("jquery.min.js"){
            return true
        }
        return false
    }

    override class func canonicalRequestForRequest(request: NSURLRequest) -> NSURLRequest{
        let urlPath = NSBundle.mainBundle().pathForResource("jquery.min", ofType: "js", inDirectory:"res/js")
        let url = NSURL.fileURLWithPath(urlPath!)
        let mutableReqeust = request.mutableCopy() as! NSMutableURLRequest
        mutableReqeust.URL = url
        return mutableReqeust
    }
}

在 AppDelegate 中注册新的 url 协议

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    NSURLProtocol.registerClass(MyURLProtocol)
    return true
}

我的资源目录结构

res
└── js
    └── jquery.min.js

代码运行时出现如下错误

    0x111785900 <+0>:   pushq  %rbp
    0x111785901 <+1>:   movq   %rsp, %rbp
    0x111785904 <+4>:   pushq  %rbx
    0x111785905 <+5>:   pushq  %rax
    0x111785906 <+6>:   movq   %rdi, %rbx
    0x111785909 <+9>:   movq   0x118(%rbx), %rdi
->  0x111785910 <+16>:  movq   0x368(%rdi), %rax  [WebThread(7):EXC_BAD_ACCESS(code=1.address=0x368)].
    0x111785917 <+23>:  movq   %rax, 0x508(%rbx)

我用 lldb 多次输入 continue,但代码停留在 0x111785910

代码有什么问题?

【问题讨论】:

标签: ios swift


【解决方案1】:

我找到了解决办法,我写博客如下。 http://codeboy.me/2015/09/25/ios-uiwebview-optimization/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2015-03-16
    • 2015-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多