Integrate native iOS navigation and web navigation - ios

In my iOS app I have to have some native code and some web code. I am having a little difficulty integrating the navigation of the two.
For iOS I am using an embedded UINavigationController. In the web app I am using a single page application control with the Application Page doing the navigation through the web pages.
Here is the problem. On the first web page I suppress the navigation, as I want to use the iOS navigation there (to go back to from the main menu). A user taps the a list entry and the 2nd web page appears. Here I want to do the opposite - suppress the iOS Navigation but show the web navigation.
So I use the method shouldStartLoadingWithRequest to control whether or not to show the native UINavigation Controller. [method below]
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
NSString *urlString = [url absoluteString];
if ([urlString
isEqual:
#"https://myURL.xsp"] ||
[urlString isEqual:#"https://myURL.xsp/"]) {
[self.navigationController setNavigationBarHidden:FALSE animated:YES];
} else {
[self.navigationController setNavigationBarHidden:TRUE animated:YES];
}
return YES;
}
My problem is that when a user taps the web navigation to go back to the first view, the event does not fire again (as I am in the uiWebView I assume and so nothing in iOS has changed) and so I lose the native navigation. What I think I need is a way to fire off this method if the uiView changes?
Or is there a better way to handle this?

Related

How to create a button close webview or goback to app in webview objective c

I am very new at Objective-C and iOS programming and I want to create something like X or Back button to close a web view. I am displaying a video from a web URL and on iPhone I have no physical back button like Android devices. So when I show the web view there is nothing to close it and go back to the application, just showing video.
Here is my code
if([_mServiceID isEqual:#"movFilePlay"] == YES) //Stark
{
dispatch_async(dispatch_get_main_queue(), ^{
[_mainView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[dic strValueForKey:#"filePath"]]]];
[self.mainView canGoBack];
});
}
I have no idea where to put a button in this application. There are too many packages and controllers. Please help me, thanks!

SFSafariViewController doesn't open url in mobile mode

I have simple code
NSURL *url = [NSURL URLWithString:#"https://en.wikipedia.org/wiki/Cat"];
if ([SFSafariViewController class] != nil) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:url];
[self presentViewController:sfvc animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:url];
}
I've tested on iOS 9.3. When I first open url I can see the page in mobile mode.
(Picture 1)
Next I click Desktop. And I can see this page (Picture 2)
(Picture 2)
I restarted application and SFSafariViewController still opens page in Desktop mode (Picture 2).
Can I force open the url in the mobile mode using SFSafariViewController and How Can I do this?
The "Desktop" link in this context means the user will see what s/he would normally see when clicking on their Macintosh or PC. This is expected behavior.
If you suspend and then resume the application, especially when SFSafariViewController is visible to the user, the app will resume on the same page where it was left off, unless you tell it to re-open the original URL (which you can do by having your application delegate watch for applicationWillEnterForeground: and then force reload the view controller).
if it were working fine, then all you need to do is to click mobile view in wikipedia footer, otherwise rebuild your app and clean it, it will restore the default behavior
The final solution that I found, It's using specific links with a prefix (m.) that the server supports, It will force open the urls with the mobile mode:
NSURL *url = [NSURL URLWithString:#"https://en.m.wikipedia.org/wiki/Cat"];

Facebook and G+ login with UIWebView

My iOS app has a login page that is set up in a UIWebView. It also has a Facebook and G+ login. I can get the normal login screen to work, alright. But I am having problems when I do that with Facebook and G+. I am assuming that the reason is the New Window that pops up with the button click.
Is there any way that I can handle this without resorting to Manual Login?
It would be much easier to use native Facebook and G+ login. What you are trying to accomplish is much more complex than using native logins.
If you really, for some reason, must do it in a UIWebView, you would have to implement some strategy like this:
override -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType method of UIWebViewDelegate protocol.
in your implementation, you should open the login interface in another modal UIWebView and handle login events there, and on the dismissal of that modal view, if login was successful continue with the app flow.
That would look something like this:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
//for facebook login
if ([[url absoluteString] containsString:#"m.facebook.com"] && [[url absoluteString] containsString:#"login"]) {
// instantiate and present new modal UIViewController containing UIWebView that loads the url.
return NO;
}
//for G+ something similar
return [super webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
}
Then, the modal viewControllers for login would also have to implement the logic of how to handle the possible login events, and dismiss itself on success, which would be very complex.
The first webView would than also have to know when the modal is dismissed (can be done with protocol or some other way), and then check the login status, and proceed if successful.
So, as I said, it would be best if you used the native logins.

Flurry Analytics in a WebView in iOS

In my iOS Application I want to use Flurry analytics. On one of the application's screen there is UIWebView. And the site is on the server but not in the code of App. And every time when I loading that screen, information going from the server to my screen and everything is OK. There is a lot of buttons on my WebView and I want track them when they are pressed. I read about how to implement flurry analytics into the code of App if the buttons are in the code of App, but if the buttons are on the side of server(html/css/JS) I can't understand how can I track events from the UIWebView over the App to Flurry.
Flurry is writing this:
Can I track Events (e.g. pressing of a button) with Flurry Analytics if my app is a wrapper to my mobile website? The actions take place on the site server that the app is wrapping.
Yes, you can track these Events. If the action you want to track (e.g. button pressing) exists in the app code, then simply log an Event on the app side. If the button exists on the mobile webpage shown in a Web View, you will need to inject a Javascript function that will fire off an additional custom URL when the button is pressed. This URL will have a custom scheme, for example, “myapp://buttonclicked”. Then in the app code, you will need to capture that URL before your Web View fires it. When you see that the URL is your specified URL (“myapp://...”), you can log an Event on the app side.
There are several ways to capture the custom URL before it is loaded by the Web View. In iOS you would need to implement the UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:. On Android, you could implement a WebViewClient with the shouldOverrideUrlLoading method.
you can easily catch webview events through a delegate method
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
bellow is that sample code, I used in on of my application:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if([[[request URL] absoluteString] rangeOfString:#"payment_successful_mobile.php"].location != NSNotFound)
return YES;
if([[[request URL] absoluteString] rangeOfString:#"index.php?file=c-my_campaingns"].location != NSNotFound)
{
[self onClickBackButton:nil];
return NO;
}
if([[[request URL] absoluteString] rangeOfString:#"Timeout"].location != NSNotFound)
{
[self performSelectorOnMainThread:#selector(displayTimeOutMessage) withObject:nil waitUntilDone:NO];
return NO;
}
return YES;
}

open url with back button

I have one little truble.
I want to open URL in my iOS application.
The way I found on the web was :
[[UIApplication sharedApplication] openURL:url];
and it works fine, but this approach close my app and open safari without back button to return in my app. How to achieve this?
I would like something like
MFMessageComposeViewController * picker = [[MFMessageComposeViewController alloc] init];
[self presentModalViewController:picker animated:YES];
Rather than opening the URL in Safari why not just create a new view controller containing a UIWebView?
Show the view controller, pass in the URL, and load the URL into the UIWebView. Something like this:
MyWebViewController *mwvc = [[MyWebViewController alloc] initWithURL:myURL];
[self.navigationController pushViewController:mwvc animated:YES];
Then in MyWebViewController:initWithURL
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]];
[myWebView loadRequest:request];
Safari is the application provided by Apple itself, so one cannot make change in behaviour of Sarari.
If you require a back button then create your custom UIViewController using UIWebView and place your back button over there.
For back functionality, you can either use UINavigationController or UIViewAnimation.
To get the control back to main native iOS application, you can URLScheme concept. URLScheme concept gives you unique identifier url of a native iOS application.
Check this http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-working-with-url-schemes/
It gives you fair idea on the procedure to achieve your requirement.

Resources