SFSafariViewController doesn't open url in mobile mode - ios

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"];

Related

Prevent UIWebView Pop Out to Safari, But Still Load Page

I'm working on an iOS application that uses a lot of UIWebViews. Is there are way to prevent the UIWebView from popping out to Safari (iFrame code) but still load the page?
In other words return YES in webView:shouldStartLoadWithRequest:navigationType: but not let the web page pop out to Safari.
EDIT:
The web view side has processes that need to be executed, which is why I need to return YES. But the processes also inadvertently cause the application to launch the URL in Safari. I'm looking for a way to intercept that, and choose whether or not to launch the URL in Safari.
Apple introduced a Safari View Controller on iOS 9. Safari View Controller is a pre-built browser that has all the bells and whistles of Mobile Safari, but can be presented without leaving your app.
First we need to import Safari Services
#import <SafariServices/SafariServices.h>
For Objective C:-
NSString *yourUrl = #"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];
For Swift:-
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)

UIApplication sharedApplication openURL only work once

I have a button when user click it and it launch the map app to show the direction to the destination.
NSURL *mapURL = [NSURL URLWithString:#"http://maps.apple.com/?daddr=San+Francisco,+CA&saddr=cupertino"];
[[UIApplication sharedApplication] openURL:mapURL];
and I found on iOS 8.1 it only opens once and it won't work the second time or third time.
I had to uninstall the app and re-install it again to make it work.
And the view was nested in a navigation hierarchy.
any ideas?

Launching the Mail.app from my application

In my application, i need to launch the Mail.app of Apple. The screen i need to show to the user is
so that he can select whichever option he wants.
Using the MFMailComposeViewController shows the mail composition interface
Is is possible to launch the Mail.app programmatically, or is it forbidden by Apple.
Try this:
NSURL *myURL = [NSURL URLWithString:#"mailto:adress#example.com"];
[[UIApplication sharedApplication] openURL:myURL];

how to open iPhone call screen keypad with number, when click on the button in iOS?

I have call button in my view controller in my application. when i will click on call button, it will show my iPhone call keypad with some number.
I am not getting any solution for this. Please help me.
Thank-you
Before you think I am, I am not the Popeye that has supplied the other answer.
Right down to my answer. There are a couple of ways you can do this
// Pick one of the two ways from below, one will return you to the app afterwards the other will not.
// This version of of creating the number will open the dialer but will not return you to the app afterwards.
NSURL *phoneNumber = [NSURL urlWithString:[NSString stringWithFormat:#"tel://%#", yourNumberString]];
// Whilst this version will return you to your app once the phone call is over.
NSURL *phoneNumber = [NSURL urlWithString:[NSString stringWithFormat:#"telprompt://%#", yourNumberString]];
// Now that we have our `phoneNumber` as a URL. We need to check that the device we are using can open the URL.
// Whilst iPads, iPhone, iPod touchs can all open URLs in safari mobile they can't all
// open URLs that are numbers this is why we have `tel://` or `telprompt://`
if([[UIApplication sharedApplication] canOpenURL:phoneNumber]) {
// So if we can open it we can now actually open it with
[[UIApplication sharedApplication] openURL:phoneNumber];
}
Hope this helps if you have any questions please just ask.
iPhone SDK doesn't gives you direct access to dial numbers from the app.So,you can't do this,but there is a way to achieve this.
-> Have a TextField(yourPhoneNumber) with keyBoardType UIKeyboardTypePhonePad.
-> [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#",yourPhoneNumber.text]]];
Hope this will help you out.

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