dsh*_*per 14 cocoa objective-c webarchive webview
我正在为Lion开发一个应用程序,我想要做的是打开.webarchive文件,修改DOM的片段,然后将修改后的DOM写出到同一个文件中.
到目前为止,这是我的代码.它打开webarchive,修改它,然后将其保存回文件.
NSString *archivePath = @"/Users/tigger/Library/Mail/V2/MailData/Signatures/1216DD8D-C7E2-4DE1-9FCD-0A9A3412C788.webarchive";
NSData *plistData = [NSData dataWithContentsOfFile:archivePath];
NSString *error;
NSPropertyListFormat format;
NSMutableDictionary *plist;
plist = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&error];
if(!plist){
printf("no plist");
[error release];
}else{
NSString *s = [NSString stringWithUTF8String:[[[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"] bytes]];
NSString *new = [s stringByReplacingOccurrencesOfString:@"</body>" withString:@"hey there!</body>"];
[[plist objectForKey:@"WebMainResource"] setObject:new forKey:@"WebResourceData"];
printf("Archive: %s", [[plist description] UTF8String]);
NSData *data = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 errorDescription:nil];
[data writeToURL:[NSURL fileURLWithPath:@"/Users/tigger/Library/Mail/V2/MailData/Signatures/test.webarchive"] atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)
问题是生成的webarchive无效.原件看起来像这样:
bplist00—_WebMainResource’
_WebResourceTextEncodingName_WebResourceFrameName^WebResourceURL_WebResourceData_WebResourceMIMETypeUUTF-8PUdata:O<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Dan Shipper</div><div>dshipper@gmail.com</div><div><br></div></body></span><br class="Apple-interchange-newline">Ytext/html(F]l~îöõ°™
¥
Run Code Online (Sandbox Code Playgroud)
虽然最终的webarchive看起来像这样:
bplist00—_WebMainResource’
^WebResourceURL_WebResourceFrameName_WebResourceMIMEType_WebResourceData_WebResourceTextEncodingNameUdata:PYtext/html_<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Dan Shipper</div><div>dshipper@gmail.com</div><div><br></div>hey there!</body></span><br class="Apple-interchange-newline">UUTF-8(7Ndvîöõ•?
æ
Run Code Online (Sandbox Code Playgroud)
任何人都有任何想法,为什么它无效或如何解决它?非常感谢你的帮助!
我也尝试使用textutil convert命令生成webarchive,但它不起作用,因为在我的原始HTML文件中我有这样的图像:
<img src="http://www.domainpolish.com/images/crowd.png">
Run Code Online (Sandbox Code Playgroud)
但是当我使用textutil时,它会下载图像并将其保存为:
<img src"file:///1.png">
Run Code Online (Sandbox Code Playgroud)
即使我不希望它下载或更改网址.我使用了noload,nostore和baseurl选项无济于事.
编辑:修复它!所以问题是当我更换HTML时,我将其作为NSString而不是NSData插入:
NSString *s = [NSString stringWithUTF8String:[[[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"] bytes]];
NSString *new = [s stringByReplacingOccurrencesOfString:@"</body>" withString:@"hi there!</body>"];
NSData *sourceData = [new dataUsingEncoding:NSUTF8StringEncoding];
[[plist objectForKey:@"WebMainResource"] setObject:sourceData forKey:@"WebResourceData"];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1891 次 |
| 最近记录: |