Saving Webarchive in Cocoa without images - objective-c

I have a WebView that I want to save as a WebArchive. I just want to save the HTML part and not the referenced images. When I do the following, it appears to be creating a WebArchive with the images embedded as well.
[[[[[sourceSignatureWebView mainFrame] dataSource] webArchive] data]
writeToFile:#"MyWebarchive.webarchive" atomically:YES];
Besides the fact that I don't want to embed the images, Safari seems to have trouble displaying the file and is showing a lot of binary gibberish wrapped around the HTML part I want. I know I can use textutil to create the file, but if possible would like to do it all inside Cocoa.
Does anyone know if this is doable? Any ideas would be much appreciated. Thanks!

How about something like this:
WebDataSource *dataSource = [[sourceSignatureWebView mainFrame] dataSource];
WebArchive *archive = [[WebArchive alloc]
initWithMainResource:[dataSource mainResource]
subresources:nil
subframeArchives:nil];
[[archive data] writeToURL…
Or are you actually after an HTML file rather than web archive?

Related

Array Doesn't load PNGs Exported from After Effects: ios, xcode

Our graphic designer is sending us .PNGs named appropiately "hide_00~iphone.png", "hide_00#2X~iphone.png" etc
He is exporting the images from after effects. I add them to the project and try to load them into an array on init. An exception is thrown each time for all of his files. Now, if I go in AND RENAME the files in the finder to exactly the same name, everything compiles fine. I have no idea what's going on here. Xcode cannot find them in the file system until I rename them. But the name is EXACTLY the same as what he sent me. I checked for white space around his file naming but everything looks fine.
Does anybody know if After Effects puts weird header info in the images? Or does this sound familiar at all to anyone. There are a whole bunch of images we are working with and I would hate to have to rename them by hand.
So I just used imageNamed and the image loaded just fine. So obviously your routine to load the image by path/name is flawed. You can post that and it can be evaluated - but this has nothing to do with 'After Effects'.
EDIT: For the record, my system is Lion Xcode 4.4.1 and my project set for iOS 5.1. I took your file from dropbox, and verified that in my project I CAN load the image as you are trying to do:
for (int i = 6; i < 7; i++) {
NSString *path = [NSString stringWithFormat:#"hide_step_seq_%02d", i];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:#"png"]];
NSLog(#"PATH %# image=%#", path, image);
}
2012-09-13 07:15:23.241 Searcher[58114:f803] PATH hide_step_seq_06 image=<UIImage: 0x6a4cb30>
So, where to go from here? I've tried to help several people here who get burned by the #2x or ~iphone suffixes. For some reason a few people cannot ever seem to get this to work - all I can think of is there is some flag deep in the system that gets toggled and there is no way to untoggle it.
My suggestion is to try using the actual complete file name - try appending ~iphone and see if that works.
You can also in Terminal do a 'ls *.png | od -c' before changing the name and afterwards, to verify that absolutely the characters are the same.
The last thought I have on this is that files have many attributes: creation time, last access, last modiied, extended attributes, permissions, etc. It is possible (while unlikely) that for some reason one of these values blocks the system from attempting to use the ~iphone suffix.
I really wish I could help you further. If you want to put a simple little project together that does nothing more than tries to open a few images and it fails, zip the whole project up, put on dropbox, I'd be more than willing to run it on my system to try and duplicate the problem. You can also do as I did in the code above and verify that path looks good and the image is nil.

Finding links in NSTextView with dynamic contents

I have an NStextView that loads a description from a JSON source. The NSTextView loads a different string every time the user clicks a button. These descriptions sometimes have a URL in them which is rendered as plain text and not as a clickable URL. It loads just fine, it's just that URL's aren't recognized. I've set up automatic link detection to no avail. I also tried AttributedString which didn't work, but that may be my incompetence (new to this.)
Here's the code:
if ([firstCommenter isEqualToString:playerID]) {
NSString *commentBody = [[allComments objectAtIndex:0] valueForKeyPath:#"body"];
[shotDescriptionTextView setString:commentBody];
}
Refer Automatic Link Detection in an NSTextView. Hope helpful
For even better solution without need of any category look here
TL;DR:
[shotDescriptionTextView setEditable:YES];
[shotDescriptionTextView checkTextInDocument:nil];
[shotDescriptionTextView setEditable:NO];

can't open image with NSOpenPanel

I use below code for open image with NSOpenPanel but doesn't work
//panel=NSOpenPanel
NSString *imgg = [NSString stringWithFormat:#"%#",panel.URL];
self.imgUser.image=[NSImage imageNamed:imgg];
The problem is that +[NSImage imageNamed:] doesn't load an image by URL. If you read the documentation, it explains what it actually does: it looks for an image stored in the cache under that name, or stored in the app's bundle or AppKit's framework under that filename.
There are a large number of ways to actually open an image by URL. The one you're probably looking for is:
NSImage *image = [[[NSImage alloc] initWithContentsOfURL:panel.URL] autorelease];
Also, as a side issue, the way you're trying to turn a URL into a path is incorrect. If you have an NSURL for file://localhost/Users/user437064/Pictures/mypic.jpg, just converting that to a string just gives you #"file://localhost/Users/user437064/Pictures/mypic.jpg". That isn't a path that you can use with path-based APIs. What you actually want is #"/Users/user437064/Pictures/mypic.jpg", and the way you get that is -[NSURL path]. So "NSString *imgg = [panel.URL path];". But this is irrelevant; unless you need to deal with very old versions of OS X, or out-of-the-way APIs, there's almost always a method that takes a URL for each method that takes a path, and often the path-based ones are deprecated.
As an even farther-off-the-side issue, you don't need stringWithFormat: to convert something to a string; "[panel.URL description]" gives the exact same result as "[NSString stringWithFormat:#"%#", panel.URL]", much more simply and a little more efficiently.

view .doc, .docx, .rtf, .ppt file in iphone using Webview

I want to view .doc, .docx, .rtf, .ppt file in iphone.
But I guess something is going wrong at my side and its not working for the above formats but my code is working fine for .txt and .pdf files.
I have the read the document regarding Webview it states it supports viewing of the above document.
below is my snippet for .doc
[webView loadData:requestData MIMEType:#"application/msword" textEncodingName:#"UTF-8" baseURL:nil];
for .ppt I am using MIME type as "application/vnd.ms-powerpoint"
Note: If I am making MIME type as "text/html" for .doc/.rtf then it displays some garbage data So I think there is something missing in MIME type from my side.
Any help is highly appreciated.
Waiting for your reply.
Update:........
It seems there is some issue with NSData for opening these type of file formats.
My data is encrypted so I cannot use
requestWithURL directly and other thing is I need to pass credentials to get the file and the credentials doesnt work proplerly if I save the credentials using
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
and then make the request using below
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]] ]
Is there any way I can use NSData for the above formats so that I can make async call and pass my credentials and then decrypt the data and show it in WebView.
Try using a QuickLook Framework which is an amazing feature given by Apple. If you have seen this that when you select ANY FILE on MAC System and click SPACE BAR then you will a quick preview of your file it may be any thing... *.pdf, *.doc, *.docx or any thing... Even if you have not installed those softwares for same files you will get a PREVIEW for those files...
QuickLook framework also gives same functionality... Please go through some of the links for the same..
http://robsprogramknowledge.blogspot.com/2011/02/quick-look-for-ios_21.html
https://github.com/rob-brown/Demos
https://github.com/rob-brown/RBFilePreviewer
http://robsprogramknowledge.blogspot.com/2011/02/quick-look-for-ios_21.html
(Recommended..) http://iosdevelopertips.com/data-file-management/preview-documents-with-qlpreviewcontroller.html
Not all of those formats (docx) are supported. This looks like the official word. In the example Apple does not specify a mime type.
This question suggests that you must use an NSURLRequest instead of loadData.
Edit:
As far as I have seen, you cannot directly pass data to loadData for these types.
The easy work around is to write to a temporary file. You can delete it in webViewDidFinishLoad or, technically, as soon as the the file has been opened.
The hard work around is to use NSURLCache. You should be able to implement a custom NSURLCache and have WebKit use it via setSharedURLCache. Your cache would basically know how to get your local encrypted files and pretend that they are cached. I have not tried this, but I think it is your best bet. This approach may be blocked just like loadData is.
You need to supply something to baseURL:. For example:
[webView loadData:requestData MIMEType:#"application/msword" textEncodingName:#"UTF-8" baseURL:[NSURL URLWithString:#"http://localhost/"]];
Works for me now.
Try it on an iPhone instead of Simulator

Reading .ppt (MS PowerPoint) file in Cocoa Touch

Any idea how to read a .ppt file in Cocoa Touch ?
I tried to load the contents of the file in UIWebView but it didn't work.
Here is the code :
[aWebView loadData:[NSData dataWithContentsOfFile:filePath]
MIMEType:#"application/vnd.ms-powerpoint"
textEncodingName:#"utf-8"
baseURL:[NSURL fileURLWithPath:filePath]];
[powerWeb loadData:[NSData dataWithContentsOfFile:filePath]
MIMEType:#"application/vnd.ms-powerpoint"
textEncodingName:#"utf-8"
baseURL:[NSURL fileURLWithPath:filePath]];
All suggestions are highly appreciated.
Thanks
UIWebView is for displaying web content, in a format you could view directly in a browser. It has no idea about how to display PowerPoint files — most applications don't, since it's a proprietary Microsoft format. (I stand corrected — apparently, UIWebView does know how to display PowerPoint files and others. If it's not working, I'd suggest trying a different MIME type, such as application/mspowerpoint.) Just remember that simply loading a file into an NSData doesn't mean that anyone else you pass that data to will know how to interpret the bytes.
You might check whether Microsoft offers any tools for parsing PPT files, or look around for open-source tools — for example, Google searches often convert to HTML. Just be aware that your browser is usually not loading a PowerPoint version of the file directly.
If Webkit doesn't have a PPT parser, you're on your own - you have to manually load PPT files, parse them, and render them; it might be easiest to make a web service to do this (this way you get real libraries), then have them download the images over HTTP so the client-side implementation is simple
NSString *powerPointFilePath = [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"ppt"];
NSURL *powerPointFileURL = [NSURL fileURLWithPath:powerPointFilePath];
[self.webView loadRequest:[NSURLRequest requestWithURL:powerPointFileURL]];