How can i send information and receive information from a website without "downloading" the file once and then using it. Like, every time I push on a button, i could receive what the text on the website is trough the code, and not by UIWebView.
And also, if i have a textfield, i could type some text in and then push at a button, and send this text to the website, in some kind of way.
Could someone give me an example of this?
By the way, sorry for my english. Let me know if it was something you didn't understand.
NSString has a method + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error that you can use for grabbing text in a very simple way. To upload the text you could use NSURLRequest together with NSURLConnection.
There are also this framework that might do stuff you'd like: http://restkit.org/
You can use NSURLConnection for grabbing stuff from HTML requests. You can read the Apple documentation on NSURLConnection here.
Related
Using the new iOS 9 feature - Universal links, from my understanding, is supposed top open my app whenever a specific domain is opened in browser (or other apps?). I have gone through the documentation and through this guide.
However, when the app opens I do not receive the parameter that is meant to help me open the correct page for the user to view....
I would share the code I'm using, but it's quite a big infrastructure and not really a couple of lines of code (server side JSON, plist rows and some IDs on the developer portal).
Anyone encountered it and could give me a hand here, please?
The Branch guide you linked to (full disclosure: I work with the Branch team) unfortunately doesn't cover a rather important step: what to do after your app opens. Which is exactly the issue you're encountering :). But the good news is you've already done the hard part with all the server and entitlement config.
What you need to complete the loop is a continueUserActivity handler in your AppDelegate.m file. This will pass you a webpageURL property containing the actual URL of the Universal Link that opened your app, which you can then parse and use for routing. It'll look something like this:
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler {
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
NSString *myUrl = [userActivity.webpageURL absoluteString];
// parse URL string or access query params
}
return YES;
}
Also, when testing keep in mind that Universal Links unfortunately don't work everywhere yet:
P.S., gotta ask...since you found the Branch blog already, had you considered using the service to handle the link routing for you? It can definitely help simplify things!
I'm creating an iOS5 app (programmed in Objective-C) which provides a couple of functions. One of them is to allow a user to fill out a text field with JavaScript. When the user presses a "Test" button, I want it to save to a specific JS file and move to a new view, displaying an HTML page that will display the results of that JavaScript on a canvas element.
Unfortunately for me, I have no idea how to save to a JS file for a text field. Nor do I know if this is actually the best way to achieve the results I'm after.
So can anyone tell me the code I'd need to place in the IBAction of my test button to save the file, or if there is a better way to get the user's script into the HTML file with the canvas element?
You have some options to do that.
Add a callback in WebView is possible only in desktop apps, but you can make a workaround.
1- Set a handler to click in javascript;
2- This handler parse the value via windows.location (trying to change the current url);
3- In UIWebviewDelegate set webView:shouldStartLoadWithRequest:navigationType: to recognize this values parsed by javascript and returning NO (canceling the url change).
After you have the code typed by user, is easier pass to the new one UIWebView. You can save the file via NSData or other class and load in by the path, or you can parse directly the code to be showed via NSString.
EDIT
I, still, belive what I said is what you want, but with a little more info. Yes, 3 NSString probably solve your problem. You even can call eval in javascript, via objective-c and parse user code too. Those logic ideas are a good approaching. You can choose the easier for you.
There are more info in this another Q&A
You can use html 5 local storage for that.
I am using iOS 4.3 & was wondering if there is any way that I can access the Safari's "Reader" feature through which webpages are removed of ads & other riff raff & the content takes the center stage.
If one opens any article in Safari (on say Wikipedia website), then a "Reader" button appears on the URL bar. Clicking on it presents a new window presenting the content beautifully.
How can I leverage this this functionality in iOS through UIWebView ?
PS: I know there is something called Readability Project. But I have no idea how to use this through UIWebView. Also for some websites Safari's Reader takes a call not to enable "Reader" feature, maybe it has no sufficient confidence?
Important: THIS ANSWER NO LONGER WORKS!
Readability shut down on September 30, 2016.
Here is something they recommend as a replacement:
https://mercury.postlight.com/web-parser/
Keeping the answer as a historical reference
--- Original answer ---
You can use Readability mobilizer for this. You will get a cleaned up version of any article, in the Readability styling:
http://www.readability.com/m?url=http://{URLOFTHEARTICLE}
Just prepare the URL and load it in your UIWebView. Here is how it looks in action:
http://www.readability.com/m?url=http%3A%2F%2Fwww.cnn.com%2F2013%2F01%2F11%2Fshowbiz%2Ftv%2Fgolden-globes-tv-vineyard%2Findex.html%3Fhpt%3Dhp_abar
Apple is making a pretty big deal about the inclusion of "Reader" in iOS 5. I'm assuming by the noise it's not available in 4.3
re: How to use through UIWebView
I can't find any mention of it in the Web Content Guide.
There's nothing about it in the UIWebView class reference.
And there's nothing in QA1630.
Dont parse HTML natively on iOS, I have done it before and its a messy business. Either create your own web service to do all the nasty work or look into using readability (readability.com) they provide an API.
There is also an open source ruby, python and php readability port that you can find here
https://github.com/iterationlabs/ruby-readability
https://github.com/gfxmonk/python-readability
http://code.fivefilters.org/p/php-readability/source/tree/master/
For you ruby enthusiasts, readability is also available as a gem, just google it.
Actually reader button do a bit of analysis where it parse the HTML Page and then it sees a clear body tag to parse. If that plugin is able to extract the exact body it will enable the reader button (My understanding from the readability source code). Now to implement the same for webview you just need to embed java script in your code (this java script is already available in the readability source code) and then you can achieve the same effect.
But I suspect the future plan from apple for the same. Because they can not just let anyone else do this content extraction with the huge business opportunity associated with iCloud with the combination of readability.
If you want you can simple extract the HTML from UIWebView and then extract the body and use it for your purpose. It's not a very rocket science to extract.
For analysis point of view, just have randomly some 10 HTML pages with Reader button enabled, you will see the core cotent belongs to body only and rest of the add, header, footer are separated.
I believe this is the time to re-invent the web content we use, and this is the perfect example of doing the same.
You can even do this by injecting javascript.
#define readJS #"(function(){window.baseUrl='https://www.readability.com';window.readabilityToken='';var s=document.createElement('script');s.setAttribute('type','text/javascript');s.setAttribute('charset','UTF-8');s.setAttribute('src',baseUrl+'/bookmarklet/read.js');document.documentElement.appendChild(s);})()"
And then when your webpage finishes loading
- (void)webViewDidFinishLoad:(UIWebView *)webview
{
[webview stringByEvaluatingJavaScriptFromString:readJS];
You can do it in iOS9.
first import SafariServices:
#import <SafariServices/SafariServices.h>
Afterwards we are instantiating SFSafariViewController and adding it as a subview. We have two options doing so:
Creating with only base URL
Creating with bas URL as well as entering 'Reading Mode' in case it is available
NSString *sURL = #"http://google.com";
NSURL *URL = [NSURL URLWithString:sURL];
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL]; // 1.
SFSafariViewController *safari = [[SFSafariViewController alloc] initWithURL:URL entersReaderIfAvailable:YES]; // 2.
[self presentViewController:safari animated:YES completion:nil];
I am looking for a starting point on a project that needs to display a UIWebView on an iPad. THe catch is that the HTML will be generated by the pad and displayed in the UIWebView, and will contain many input controls.
What is needed is a way to grab the contents of these controls after the user has completed entry similar to how I would do it on a server. I need to grab this entered data on the iPad without an actual submit.
Does anyone know the starting point for this type of interaction?
You can do this by implementing the UIWebViewDelegate delegate's shouldStartLoadWithRequest method:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSData* data = request.HTTPBody;
NSString* s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if([s length] == 0)
return YES;
else
return NO;
}
It works fine with a post.
Within the previously posted article it also mentioned the UIWebViewDelegate method,
webView:shouldStartLoadWithRequest:navigationType:
This gets invoked on the delegate before a link is followed. I haven't tried it, but this method might be invoked when submitting the form. Use a GET method. Easier than having to loop out of the app and back.
It can be done in simple way..
we know HTTP request contains -
Method (GET,POST..etc)
HTTP header
HTTP body
we can check header field value for Conent-type if it is x-www-form-urlencoded
then form field values are sending thru them as key=value pairs
then we can catch therse paires in
webView:shouldStartLoadWithRequest:navigationType: - in request parameter as
[request HTTPBody], similarly we can get method [HTTPMethod]..etc
if it is simply GET method then all pairs will be in request itself.
:) hope it helps
Here's a way to do it:
Register a custom URL scheme for your App (see here f.e. http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html)
When the user touches your save/submit/whatever button you read out the values of all needed form-fields, construct a url that matches your URL scheme and redirect to this URL with JavaScript (window.location) and work with the data in Objective-C and do what you have to do.
Example URL could be: myapp://value_of_field1/value_of_field2/...
See the linked tutorial on how to register a custom scheme and how to retrieve the data in Obj-C.
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