今日Adiumコードを読んで、NSURLの興味深い使用法を見つけました:
NSURL *baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"adium://%@/adium", [messageStyle.bundle bundleIdentifier]]];
[[webView mainFrame] loadHTMLString:[messageStyle baseTemplateForChat:chat] baseURL:baseURL];
URL をログに記録しようとしたところ、このadium://im.adium.Smooth Operator.style/adiumが取得されました。その後、空のプロジェクトを作成して、そのような NSURL を作成する方法を確認しましたが、失敗しました。プロジェクトの webview のフレームに loadHTMLString メッセージを送信すると、baseURL が nil の場合は問題なく、そうでない場合はビューに空白のページが表示されます。
これが私のコードです。プロジェクト名はwebkitです
NSURL *baseURL = [NSURL URLWithString:@"webkit://resource"];
//if baseURL is nil or [[NSBundle mainBundle] bundleURL], everything is fine
[[webView mainFrame] loadHTMLString:@"<html><head></head><body><div>helloworld</div></body></html>"
baseURL: baseURL];
[frameView setDocumentView:webView];
[[frameView documentView] setFrame:[frameView visibleRect]];
問題は、http:// の代わりに自己定義プロトコルを作成する方法です。