I've got this issue, I set up some contact information of a company to be displayed by a ABUnknownPersonViewController. One of the info is the Facebook page, if I click on it on the simulator it opens safari on the correct page. On the device the URL is intercepted by the FB app that doesn't open nothing but my timeline.
I'm aware that the same happens when you try to open an FB page URL inside the app using the method -openURL: and I know that this could be easily fixed by using that snippet.
NSURL *facebookURL = [NSURL URLWithString:#"fb://profile/113810631976867"];
if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {
[[UIApplication sharedApplication] openURL:facebookURL];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://facebook.com/pagename"]];
}
The fact is that I've tried to intercept the URL by using that app delegate method - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
(handleOpenURL: is deprecated), but it doesn't seems to be called.
Is there a way to intercept this opening?
I've found an answer, using the protocol methods of ABUnknownPresonViewcontroller is possible to intercept some events. I must be honest and say that I still didn't know how to recognize different actions, but in this way it works.
- (BOOL)unknownPersonViewController:(ABUnknownPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
if (property == 46) { //46 seems to be number associated to the property facebook page
NSURL * facebookURL = [NSURL URLWithString:#"fb://profile/PAGE_ID"];
if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {
[[UIApplication sharedApplication] openURL:facebookURL];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://facebook.com/PAGE_NAME"]];
}
return NO;
}
return YES;
}
Related
Is there a way to launch safari only? I know in order to send an intent and have ios to handle it we can do [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://"]];, but if user has other browser installed (i.e. chrome), there's no guarantee safari will be used.
Reason I want to use safari is that I'm trying to have safari to handle certificate authentication for me, and according to here, only system app has permission to do so
try this
//initially we need to check safari is installed or not in our device
NSURL *url = [NSURL URLWithString:#"safari://"];
UIApplication *application = [UIApplication sharedApplication];
if ([application canOpenURL:url]) {
// if success again need to validate the our calling URL.
NSURL *linkURL = [NSURL URLWithString:#"https://iostree.wordpress.com/2017/07/29/launch-safari-from-ios-app/"];
if ([application canOpenURL:linkURL]) {
if ([application respondsToSelector:#selector(openURL:options:completionHandler:)]) {
[application openURL:linkURL options:#{}
completionHandler:^(BOOL success) {
NSLog(#"success");
}];
}
}
}else{
NSLog(#"safari is not installed");
}
I have an issue with URL Scheme in plist file as "m.zameen.com"
but i type this in iPhone's safari browser not op[en but when i open using :// it opened
// In AppDelegate.m file
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
if([[url host] isEqualToString:#"page"]){
if([[url path] isEqualToString:#"/main"]){
[self.mainController setViewControllers:#[[[DLViewController alloc] init]] animated:YES];
}
else if([[url path] isEqualToString:#"/page1"]){
[self.mainController pushViewController:[[Page1ViewController alloc] init] animated:YES];
}
return YES;
}
else{
return NO;
}
}
// In DLViewController.m file
- (IBAction)page1Clicked:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"m.zameen.com://page/page1"]];
}
// In Page1ViewController.m file
- (IBAction)mainPageClicked:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"m.zameen.com://page/main"]];
}
In your project Info tab, add a new URL Type with the scheme you want to use to open the app, for example 'myAwesomeAppScheme', using your app bundle identifier 'com.myCompany.myAwesomeApp' in the identifier field and the scheme you want in the URL Schemes field:
Then in you app delegate you can check if the opened url has your scheme with something like this
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if ([url.scheme isEqualToString:#"myAwesomeAppScheme"]) {
...
}
}
And finally to open the app from an external app, the link has to be something like myAwesomeAppScheme://parameters/for/opening/viewcontrollers?otherParam=blahblah
Can I use the URL Scheme mechanism in app 1 to get a background runtime in app 2?
I mean to avoid app 2 lunching?
In the app 1 I do:
NSURL *myUrl = [NSURL URLWithString:#"myScheme://"];
if ([[UIApplication sharedApplication] canOpenURL:myUrl]) {
[[UIApplication sharedApplication] openURL:myUrl];
}
In app 2, this method:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation;
Can I avoid from lunching app 2, and get this method (or any other related method) to run in background?
Thanks in advance!
Im trying to open my Facebook app page from iPhone.
Im using this:
[[UIApplication sharedApplication] openURL:[NSURL
URLWithString:#"http://www.facebook.com/(my page name)"]];
Unfortunately, this redirects to https protocol, and because of that the device is unable to open the page.
What can I do to open this page?
pass your Page ID - xxxxxx not the page Name
[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"fb://profile/herepassyourPageID"]];
or
[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"https://www.facebook.com/herepassyourPageID (id, not name)"]];
You are mistaken by using page name instead of page ID.
You should use something like below:
NSURL *facebookURL = [NSURL URLWithString:#"fb://profile/{pageid}"];
if ([[UIApplication sharedApplication] canOpenURL:facebookURL]) {
[[UIApplication sharedApplication] openURL:facebookURL];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#""https://www.facebook.com/{pageid}"]];
}
[If desired]: You should check the availability of installed facebook app to open the page, as shown in the above code sample.
This method opens up the url in Safari when the website string is not null and it is atleast of length 3. But when I have supplierWebsite=#"www.heritage.com", nothing happens. I know that heritage.com is not valid website and so it is not activating in UIApplication. I would like to display atleast a pop up that would tell user that website is not available. Is there any way i can show Alertview telling that website is not available.
- (IBAction)doWebOpen:(UIButton *)sender {
if (self.provider.supplierWebSite && [self.provider.supplierWebSite length] > 3) {
NSString *urlString = [self.provider supplierWebSite];
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];
}else {
NSError *err = [NSError errorWithDomain:#"com.cantopenweb" code:509 andDescription:#"This supplier does not have a website."];
[self showErrorAlert:err];
}}
You could use canOpenURL method,
[[UIApplication sharedApplication] canOpenURL:[NSURL
URLWithString:#"your website"]];
The method returns a BOOL, so check that for YES or NO.
If YES, it CAN else NO.
Just use canOpenURL of UIApplication class, like:
if([[UIApplication sharedApplication] canOpenURL:url])
{
[[UIApplication sharedApplication] openURL:url];
}
else
{
//show alert
}
canOpenURL:
Returns whether an application can open a given URL resource.
- (BOOL)canOpenURL:(NSURL *)url
Parameters
url
A URL object that identifies a given resource. The URL’s scheme—possibly a custom scheme—identifies which application can
handle the URL.
Return Value
NO if no application is available that will accept the URL; otherwise,
returns YES. Discussion
This method guarantees that that if openURL: is called, another
application will be launched to handle it. It does not guarantee that
the full URL is valid. Availability
Available in iOS 3.0 and later.
Declared In UIApplication.h