Cordova 3.1 open link in Safari on iOS6 - iphone

I'm building a iOS app with Cordova 3.1. I have a link I would like to open in Safari. I've installed the org.apache.cordova.inappbrowser plugin and it worked well on my iPhone (iOS 7) and on the simulator (iOS5;iOS6.1;iOS7) but if I try (iOS6) on all devices it doesn't work.
Does anybody know how to fix this or tried it on a real device running iOS6?
I'm using this code to open the link:
window.open('http://www.google.nl', '_system');

well I've implemented this through native side (Objective C)
Add this method in 'MainViewController.m'
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
NSString *str = url.absoluteString;
NSRange range = [str rangeOfString:#"http://"];
NSRange range1 = [str rangeOfString:#"https://"];
if (range.location != NSNotFound || range1.location != NSNotFound) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
This take cares of both 'http' and 'https' link for both iOS6 & iOS7, and opens the link in default browser of the device.

I know this is an old question, but I've encountered it too and just written a small plugin to help with it. Siddhartha's answer is almost right, but when I used it, it intercepted all web requests, including those to my index.html, and this seemed to move my app into Safari. What I needed was a way to handle only explicit requests, so I could open specific (external) URLs in Safari.
There are many similar questions about Phonegap, which appears to embed special handling for window.open with _system. That would be nice, but Cordova doesn't have this functionality.
In the end, I wrote a small plugin that embeds enough Objective C (closely modelled on Siddhartha's answer) but due to the magic of plugins, it can be executed on demand, using cordova.exec. I'd love this to patch into window.open to achieve the same functionality as Phonegap, but that'll be for another day, and it doesn't really affect this answer.
As far as I can tell, in modernish Cordova, this is the only viable strategy.

Related

Detecting programmatically an installed app

I am trying to detect installed Instagram on my device , formerly I used this code to detect an app , but it seems it does not work with iOS 6 or non-JB devices :
NSString *filePath = #"/Applications/Instagram.app";
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
[self checkingInstalledApp];
}
else
{
NSLog(#"no instagram installed");
}
I check this question but his answer gives me a lot errors ! any solution ?
This is invalid code for two reasons.
1) It attempts to interact with an area outside of its sandbox.
2) It relies on an undocumented implementation detail (Perhaps the install location has moved or the application name has changed?)
What you need to do is use the canOpenURL: method on UIApplication to determine if the system can launch an application via its custom scheme (note: If the App has no custom scheme then you are out of luck)
Custom URL Scheme Opening instagram://, followed by one of the following parameters, will open our app and perform a custom action.
For example, for camera, you would direct users on the iPhone to the
custom URL instagram://camera.
NSURL *instagramURL = [NSURL URLWithString:#"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
From Instagram iPhone Hooks

Access Settings app in iOS

Is there an opportinity to show up the settings.app in iOS by clicking on a button? It should work with iOS 5.1 so the "prefs:root..." url is no option.
Do you have an idea how to solve this?
I know the question is about 5.1 specifically, but in case anyone else is interested:
As of iOS 8, it is possible to take a user from your app directly into the Settings app. They will be deep linked into your app's specific Settings page, but they can back out into the top level Settings screen.
UPDATE:
Thanks to Pavel's comment, I simplified the if statement and avoided the EXC_BAD_ACCESS on iOS 7.
UPDATE 2:
If your deployment target is set to 8.0 or above, Xcode 6.3 will give you the following warning:
Comparison of address of 'UIApplicationOpenSettingsURLString' not equal to a null pointer is always true
This is because the feature was available starting in 8.0, so this pointer will never be NULL. If your deployment target is 8.0+, just remove the if statement below.
if (&UIApplicationOpenSettingsURLString != NULL) {
NSURL *appSettings = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:appSettings];
}
You are not able to do this on iOS 5.1. Most likely Apple removed that ability intentionally (you'll get "Please enter a valid URL", while Twitter can still call the Settings, though). Please refer to:
How to open preferences/settings with iOS 5.1?.
Apple Disables Home Screen Shortcuts For Settings Toggles In iOS 5.1
On iOS 8 Apple gave us the possibility to go to the App Settings right from our app
you can apply this code:
- (IBAction)openSettings:(id)sender {
BOOL canOpenSettings = (UIApplicationOpenSettingsURLString != NULL);
if (canOpenSettings) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}
iOS6 shows an option to open the settings app directly from an 'AlertView' (shown automatically) if it detects if you're trying to post to FB or Twitter without having those accounts setup.
I have elaborated this over here
The method openURL: is now deprecated.
iOS 10 +
The correct way to open the settings URL (or any URL for that matter) is as follows:
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL options:#{} completionHandler:^(BOOL success) {
// Do anything here
}];

How to go back from Safari to UIWebView (iphone)

I really want to thank you all you guys first , I began iOS programming learning several weeks ago ,and I have learnt a lot from here these days,.
I have a UIWebView and loading some html content with "loadHTMLString" method, when i click hyperlinks in the UIwebView, It comforms the
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType{
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[Request URL]];
return NO;
}
return YES;
}
to open a Safari.
My question is: Can I go back from the Safari to the UIWebView? how?
You can't get Safari to send the user back whenever you want, but one way to get back to your application is by creating a custom URL scheme (e.g. myapp://return, or whatever scheme makes sense to you), assuming that the web page the person is going to can have links that are intended to go back to your appliaction.
You can do this by registering that your application handles the URL scheme, and then processing the request appropriately when iOS tells you to. The Apple docs are fairly complete about this.
But if you want your application to be able to arbitrarily pull people back whenever you want, I don't think that's possible. Safari isn't under the control of anyone but Apple.

How can I make iPhone hotspot login UIWebView open a link in Safari?

We have a wifi hotspot that displays a terms of service page before people are allowed to start using it.
After acceptance, a new page with a few links is displayed. I'd like to have those links open in Safari instead of in UIWebView.
I know there's a way to program UIWebView to open links in Safari, but that's not an option as this is the default UIWebView for logging into wifi hotspots and not a custom app.
Is there another way to have the links open in Safari and not in UIWebView? I've tried javascript and I've tried setting the target to _blank.
EDIT: After reading some responses, it seems like the only way to do this is to set the UIWebView delegate. I don't think this is an option because I'm not the one launching the UIWebView.
When the iPhone connects to the network (at least with version 3.0), it checks to see if it's being redirected to a login page. If it is, it does what you're saying with authentication and such inside of a UIWebView. Have you checked to see what it's reporting as the browser in use? If it isn't Mobile Safari, then you could do something server-side the first time someone connects using Mobile Safari.
If it does report as Mobile Safari when it's checking redirection, then another alternative is to figure out what site it tries to go to - maybe apple.com? Then, on the server side, the first time a different URL is loaded, redirect to your links. This will only work when the user opens Mobile Safari, however.
Other than that, I think what you're asking to do is outside of the scope of the current iPhone OS. I'd recommend filing a ‘feature request’ with Apple at bugreport.apple.com.
The only way I know how to do this is to set up a UIWebView delegate and supply something like the following:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked &&
[request.URL isFileURL] == false)
{
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
The code above will cause Safari to open a link clicked on by the user. I know you said you had little control over this UIWebView -- perhaps you have more than you think by setting the delegate?
Use the openURL method of UIApplication:
NSURL *url = [[NSURL alloc] initWithString:#"http://example.com"];
[[UIApplication sharedApplication] openURL:url];
[url release];
An Apple example of this is available as part of the LaunchMe sample.

Using tel: url to initiate a call doesn't return control

I have an app that worked just fine in version 2.2.1 of the iphone, but have ran into an issue when I upgraded my dev iphone to 3.1.2. Before, dialing a number worked fine, as when the call was ended, my application was loaded. Now, after I hit end call, it loads the default phone application. Does anybody know why this is? I've looked at the diff's from sdk 2.x to 3.x and can't find any reason why this would change. Thanks
This was indeed changed from 3.0 to 3.1. If you need the "prompt-for-call" and "relaunch-app-after-call" there are 2 work-arounds:
Option 1: Create a UIWebView and load your tel: URL.
// assuming you have an ivar to store a weak reference to a UIWebView:
// UIWebView *phoneCallWebView;
- (void) dialPhoneNumber:(NSString *)aPhoneNumber
{
NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#",aPhoneNumber]];
if ( !phoneCallWebView ) {
phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];
}
[phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
}
- (void) dealloc
{
// cleanup
[phoneCallWebView release], phoneCallWebView = nil;
[super dealloc];
}
Option 2: Initiate your call with with the telprompt:<number> URL scheme instead of tel:<number>. Note that this is an undocumented API feature, but it's what UIWebView uses when you tap on a phone number link in a webview (or in MobileSafari). If you are targeting iPhone >= 3.0, there are not any problems using telprompt: (tel: and telprompt: are identical on 3.0). I'm not sure about iPhone OS 2.x.
In general, option 2 works and is easier, but option 1 is actually a "legal" workaround. Unfortunately, there does not seem to be a way to separate the "prompt-for-call" and "relaunch-after-call" behaviors. On iPhoneOS >= 3.1, you can either get both, or neither.
Yes, Apple changed this behavior from 3.0 to 3.1 (I believe, could also be from 2.x to 3.0). They have acknowledged that the change was deliberate and not a bug. There is no workaround that I know of. You just have to live with it a file an enhancement request if you think the old behavior should be made available again.
But both UIWebView and telepump will introduce a "pop up box" require user to confirm if user would like to dial the number. I am wondering if there are some solutions that could get rid of "pop up box" Thank you.
NSURL* url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"telprompt:%#",[SLUser sharedInstance].ivrNumber]];
[[UIApplication sharedApplication] openURL:url];