Cocoa: get WebView mainFrame html string - objective-c

I'm creating an Mac app that loads some content from a URL in a WebView and then needs to save the content to a file. The content may and may not be HTML. Loading works fine, the problem is to get the content from the WebView. How do I do that?
Thanks!

I do not know if you use WebView (MacOS) or UIWebVoew (iOS...). I use the following code (in MacOS) which works well for me:
WebFrame *frame = [myWebView mainFrame];
WebDataSource *source = [frame dataSource];
NSData *data = [source data];
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
[someTextView setString:str]; // shows the content of myWebView as string

Related

How to remove a div tag in html String in UIWebview

I m extracting an html string using #"document.getElementsByClassName('entry-content')[0].innerHTML"; and i was successful in getting the html response where i m storing it in a string ,the problem is i need to remove a div named as "collections-form" on the html response from the string.could you guys help me out.below is the code.
-(void)webViewDidFinishLoad:(UIWebView *)webView1 {
NSLog(#"finish");
[activity hide:YES];
NSString *app;
if (currentPage == 3)
app=#"<style>table {font-size:10px;}</style>";
else app=#"<style>table {font-size:10px;}</style>";
NSString *temp1 = #"document.getElementsByClassName('entry-content')[0].innerHTML; " ;
NSString *class =[webView1 stringByEvaluatingJavaScriptFromString:temp1];
NSString *localString = [NSString stringWithFormat:#"%#%#",class,app];
[webView1 loadHTMLString:localString baseURL:nil];
webView.hidden = NO;
webView.delegate =nil;
}
One solution is call js code which removes that element after content is loaded.
NSString *jsCommand = [NSString stringWithFormat:
#"var element = document.getElementById('%#'); element.parentElement.removeChild(element);",
#"id2",
];
[webView stringByEvaluatingJavaScriptFromString:jsCommand]
And This is another way if you wish to use JavaScript
How to remove tag from html with JavaScript in iphone UIWebView

Export image to Instagram - iPhone

I have an image and I want to export it to Instagram, so I can post it.
The code I'm using is:
NSURL *instagramURL = [NSURL URLWithString:#"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"Image.igo"];
UIImage *image = [UIImage imageNamed:#"01.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
NSURL *imageUrl = [NSURL fileURLWithPath:savedImagePath];
NSLog(#"%#",imageUrl);
UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];
docController.delegate = self;
docController.UTI = #"com.instagram.exclusivegram";
docController.URL = imageUrl;
//[docController setURL:imageUrl];
[docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
When I run the app, the App shows the button written "Instagram" its icon, but when I touch it, the button disappear, and nothing happens. The app didn't crash, but nothing happen.
What I missed in my code ?
Regards
Bruno
I guess the problem is you do not retain the UIDocumentInteractionController. Make an ivar for it in your class.
Make sure the method documentInteractionController:didEndSendingToApplication: is called on the delegate.
Also check out the Instagram documentation: http://instagram.com/developer/iphone-hooks/
When triggered, Instagram will immediately present the user with our
filter screen. The image is preloaded and sized appropriately for
Instagram. Other than using the appropriate image format, described
above, our only requirement is that the image is at least 612px tall
and/or wide. For best results, Instagram prefers opening a JPEG that
is 612px by 612px square. If the image is larger, it will be resized
dynamically.
To verify that Instagram is installed check for the URL instagram://app. The URL instagram://location?id=LOCATION_ID is intended for location feeds only!

Multithreading to download data from a website

I'm doing an app which downloads info via a request to a website, giving me back and HTML and parsing this data I obtain my app info. For downloading this data I'm using, using a url with all the parameters the request needs at the end.
NSData *data = [NSData dataWithContentsOfURL:url];
NSString* htmlString;
htmlString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[self parserHTML:htmlString]; // here I fill a NSArray with the info parsed
[self searchSomething:htmlString]; // continue filling the NSArray
...
The task for download the data and parser the HTML takes long time.
What can I do to make this faster? Grand Central Dispatch? If so, how can I use it, because I'm using this and it doesn't works, because the NSSArray is empty:
dispatch_queue_t downloadQueue = dispatch_queue_create("pharmacy downloader", NULL);
dispatch_async(downloadQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:urlReal];
NSString* htmlString;
htmlString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
[self parserHTML:htmlString]; // here I fill a NSArray with the info parsed
[self searchSomething:htmlString]; // continue filling the NSArray
});
dispatch_release(downloadQueue);
If I don't use GCD it works. What can be the problem??
Thanks for your help. I'm totally lost!!! :S
Use NSURLDownload or NSURLConnection instead.
For some sample code you may take a look at QuickLookDownloader

How to play a Vimeo video in iOS?

I've tried to search to web, but I couldn't find a topic not older than 1 year regarding this problem, therefore;
How can I play a Vimeo video in an iOS App?
EDIT1: When using the solution I'm sometimes getting this HTTP response from Vimeo
Why?
This is my way of play a Vimeo video inside a app.
I am using iFrame to load Vimeo video inside my app.
follow this steps and you will too.
create a uiwebview and connect it to your .h file. Mine is _webView.
Add this method to your .m file.
-(void)embedVimeo{
NSString *embedHTML = #"<iframe width=\"300\" height=\"250\" src=\"http://www.vimeo.com/embed/rOPI5LDo7mg\" frameborder=\"0\" allowfullscreen></iframe>";
NSString *html = [NSString stringWithFormat:embedHTML];
[_webView loadHTMLString:html baseURL:nil];
[self.view addSubview:_webView];
}
I am using the embedded code in Vimeo video. (I hope you know what it is)
call this method inside your viewdidload
[self embedVimeo];
Run the app and you will see the video in your view. This way is perfectly working for me and i think this will help for your too.
You can use YTVimeoExtractor, works fine for me.
You can use this code
NSString *htmlStringToLoad = [NSString stringWithFormat:#"http://player.vimeo.com/video/%#?title=0&byline=0&portrait=0\%%22%%20width=\%%22%0.0f\%%22%%20height=\%%22%0.0f\%%22%%20frameborder=\%%230\%%22", videoID];
[aWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:htmlStringToLoad]]];
Please try this,
It works for me, just some lines of code.
- (void)viewDidLoad
{
[super viewDidLoad];
vimeoHelper = [[VimeoHelper alloc] init];
[vimeoHelper getVimeoRedirectUrlWithUrl:#"http://vimeo.com/52760742" delegate:(id)self];
}
- (void)finishedGetVimeoURL:(NSString *)url
{
_moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[self presentViewController:_moviePlayerController animated:NO completion:nil];
}
Use Below Code the it will work fine
NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ;
[html appendString:#"<html><head>"];
[html appendString:#"<style type=\"text/css\">"];
[html appendString:#"body {"];
[html appendString:#"background-color: transparent;"];
[html appendString:#"color: white;"];
[html appendString:#"}"];
[html appendString:#"</style>"];
[html appendString:#"</head><body style=\"margin:0\">"];
[html appendString:#"<iframe src=\"//player.vimeo.com/video/84403700?autoplay=1&loop=1\" width=\"1024\" height=\"768\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>"];
[html appendString:#"</body></html>"];
[viewWeb loadHTMLString:html baseURL:urlMovie];
I've used this code:
NSString *embedSr = #"<iframe width=\"304\"height=\"350\" src=\"http://player.vimeo.com/video/... \" frameborder=\"0\" allowfullscreen></iframe>";
[[self WebView] loadHTMLString:embedSr baseURL:nil];
I've tried the universal player, it is successful in device with iOS 5, but failed in iOS 4.2 with iPhone 3G. I don't know why. Here's the link to embed it.
Or you can embed manually from the Vimeo site, click embed, and configure the config as you wish.

Print Local HTML using Cocoa

I have a simple list of items that needs to be printed using Cocoa. I have a half-baked solution that uses an NSView with a custom drawRect: method, but it's fairly complex and not very easy to maintain.
What I would like to have is an HTML string (which could be easily constructed from the list) that can be embedded in a one-off WebView, then printed.
Assuming I have a simple NSString like:
NSString *htmlString = #"<b>Test</b>";
What's the easiest method for creating a WebView displaying this content? I've tried the below code, but it results in a single blank page:
WebView *webView = [[WebView alloc] init];
NSString *dir = #"/Users/Me/Desktop/";
NSString *fileUrl = [dir stringByAppendingPathComponent:#"Temp_Print.html"];
NSString *htmlString = #"<b>Hi!</b>";
[[htmlString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileUrl atomically:YES];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileUrl]]];
[webView setFrame:NSMakeRect(0, 0, 500, 500)];
NSPrintOperation *po = [NSPrintOperation printOperationWithView:webView printInfo:pi];
[pi release];
[po runOperation];
Another one of those questions you solve right after asking it!
The run loop needs to iterate in order for the content to actually load. I simply finished running the actual print operation in the frame load delegate method:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
...
}
Source