How can I get response from UIWebView page to an iOS app? - ios

I'm using UIWebView as a subview in a UIViewController. What this WebView does is it allows user to submit a form and that form responses as JSON object. How can I get this response to an app? I'm not able to find what response comes. All I know is that response is JSON. I even tried using shouldStartLoadWithRequest(). But it only shows URL. Any help will be appreciated.

I use the 'window.location'. A little uncomfortable but fine.

You can try implementing this method may be it helps you
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
and check for the navigation types out of these
typedef NS_ENUM(NSInteger, UIWebViewNavigationType) {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
}

Related

Error logging into Instagram in iOS app using UIWebView

I'm following Instagram authentication [recommended] steps using UIWebView on an iOS app.
After entering credentials, hitting login loads a page with following error.
This page could not be loaded. If you have cookies disabled in your
browser, or you are browsing in private mode, please try enabling
cookies or turning off private mode, and then retrying your action.
And, this only happens on first run through the steps of authentication; on the next attempt, everything works smooth as silk. I get the code suffixed to redirect url and I request access token using it.
Screenshot:
There's already another question here and it doesn't help.
EDIT:
It seems like Cookies issue. Though, I haven't been able to fix it yet.
I had a similar problem when I was deleting cookies to make sure the log in screen appeared and not just using the currently logged in user. Try (swift):
let storage = HTTPCookieStorage.shared
storage.cookieAcceptPolicy = .always
I managed to solve this problem about an hour ago.
First, DON'T use UIWebView or WebView; use a WKWebView instead.
You see, you need to implement the method
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
when instagram's login page forwards you to your redirect_url , it FAILS to navigate. Probably because we're not doing the server-side auth, but only the client-side auth, and the redirect_url is not a valid url.
We're expecting the page to REDIRECT to an invalid url, but when that happens, the WKWebView doesn't call the method
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
or the method
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
So. What you need to do is focus on that first method, check the error variable, and extract error.userInfo.
In my case, this is how I solved the problem:
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
NSURL *err = [error.userInfo objectForKey:#"NSErrorFailingURLKey"];
if([err.absoluteString hasPrefix:INSTA_REDIRECT_URL]){
NSString *token = [[err.absoluteString componentsSeparatedByString:#"#access_token="] objectAtIndex:1];
[Utils saveToken:token];
[self.webView setHidden:YES];
//open next view controller
}else{
//TODO: No internet? something else went wrong..
NSLog(#"Error: %#", error);
}
}
Hope it helps.
Best of luck! ;)

Recognise click on link within web page in iOS

I want to load one URL in web view which is in my application. If user clicks on any URL on that web page then I want to open clicked URL in default safari app i.e. out of my application.
I know shouldStartLoadWithRequest: but it will get call whenever new URL starts loading even if it is loading an image in my web page.
How can I recognise that user clicked on URL on my web page?
You can check the navigationType parameter in webView:shouldStartLoadWithRequest:navigationType:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
switch (navigationType) {
case UIWebViewNavigationTypeLinkClicked:
// user clicked on link
break;
case UIWebViewNavigationTypeOther:
// request was caused by an image that's being loaded
break;
}
return YES;
}
There are more UIWebViewNavigationTypes that you can use to determine what caused the request:
enum {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
};
typedef NSUInteger UIWebViewNavigationType;

Access requested URL in webViewDidFinishLoad?

I'm very new to iOS development so please be gentle.
I understand that webViewDidFinishLoad will fire for each <iframe> plus the original html page.
I'm trying to find the URL of the request that resulted in that webViewDidFinishLoad. As far as I can tell, I can only access webView.request.mainDocumentURL.
I find this perplexing. Inside the shouldStartLoadWithRequest method, which fires also for each <iframe> and the original html request, you can access the URL in question, be it the iframe or the parent page.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"Parent page request: %#", request.mainDocumentURL);
NSLog(#"Actual URL request: %#", [request URL]); // This returns what I want.
return YES;
}
That first log statement will output the url.
However, inside webViewDidFinishLoad
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSURLRequest* request = [webView request];
NSLog(#"Parent page request: %#", request.mainDocumentURL);
NSLog(#"Request outputs the parent, not the iframe: %#", [request URL]);
// how do I access the <iframe> url?
}
Help me StackOverflow. My google-fu is weak and your wisdom is strong.
EDIT: I should be clear on the goal of this. I want to Do Stuff™ when the load event of the parent page fires, not for iframes. I want this to happen as soon as possible, so I don't want to wait until everything finishes.
Think of the UIWebView as your browser. The scope of the object you are interrogating is at the browser level, the iFrames are more granular than that. The URL that populates the iFrame is used to stream content for that section of the page, but for the purposes of the UIWebView, you only need to remember the main URL so the page can be managed (reloaded, etc...) as that URL clearly brings with it instructions as to how to manage the iFrame's content.
If you really need to those iFrame source URLS, simply extendeding UIWebView to hold an array of links (populate the array inside "shouldStartLoadWithRequest"). You can then do whatever you want with that list of links.
Good Luck.

How get the status of a server without download the page content in IOS

I want to check if a web ressource is available without download any data. For exemple, if I do a NSURLConnection on a webPage, I can get the status code:
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
int code = [httpResponse statusCode];//200 is ok
}
But all page datas are downloaded.
How can I get this code (or equivalent) without download this page ?
The status code comes along with the server answer, once you get the status code you will have the body as well.
Another option is maybe to check if the resource is reachable using the Reachability classes.
Check this example, it may help.
Update: borrrden pointed in the right direction here (use HEAD method instead of GET) check the comment on your anwser

Phonegap 2.0 , Cordova external links

I have a situation and I spend so much time in google with no success.
I want to open in my app (IOS), external links which are like that
"External Link" to open in safari not web view. where I have set up in "Cordova.plist"
OpenAllWhitelistURLsInWebView : true
Because I hav as well some Iframe inside my app, where I want to keep user in web view and not to leave the app.
An I have no idea why target="_blank" doesn't work, where here :
https://build.phonegap.com/blog/access-tags it says:
"
on iOS, if a domain is whitelisted, a link will take over the entire webview, unless the link's target is _blank, in which case it will open in the browser. If it is not, it will log an error on the device, while doing nothing from the user's perspective.
"
I tried to use JS way as well,
window.open('http://www.google.com', '_blank');
with no success :(
PS: I do have all my links in External host set up
I appreciate any help.
Thanks!
What you need is this charmer in your MainViewController.m
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:#"http"] || [[url scheme] isEqualToString:#"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
It works with me using following setup:
Cordova.plist:
OpenAllWhitelistURLsInWebView: false
external Hosts: google.com
Link in Code:
< a target='_blank' href='http://maps.google.com/maps?q=something'>
Hope it works for you as well :)

Resources