iOS: App Share URL using objective C - ios

I have created the code to share my app on whatsapp. The problem is when the share link is sent from ios to an android phone, the url link and text all are displayed in plain text format in android phone's whatsapp chat box, without highlighted link and url.
Can this be fixed? How?
NSString *urlString = [NSString stringWithFormat:#"%#", APP_SHARE_URL];
NSString *initialText1 = [NSString stringWithFormat:#"Hey I am using App: %#\n%#",urlString, profileModel.name];
NSCharacterSet *set = [NSCharacterSet URLHostAllowedCharacterSet];
NSString *whatsappString = [NSString stringWithFormat:#"%#", [[NSString stringWithFormat:#"%#", initialText1] stringByAddingPercentEncodingWithAllowedCharacters:set]];
NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:#"whatsapp://send?text=%#", whatsappString]];
if ([[UIApplication sharedApplication] canOpenURL:whatsappURL]) {
[[UIApplication sharedApplication] openURL:whatsappURL];
}
else {
[self showMessage:#"Unable to open WhatsApp"];
}

I am using this code for sharing on whatsapp and it works absolutely fine. Plain text issue occurs in Android OS 5.0 and earlier.
NSString *textToShare = [self getEncodedString:text];
NSString *urlStr = #"whatsapp://send?text=";
urlStr = [NSString stringWithFormat:#"%#%#",urlStr,textToShare];
NSURL *url = [NSURL URLWithString:urlStr];
[[UIApplication sharedApplication] openURL:url]
- (NSString*)getEncodedString:(NSString*)string {
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)string,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
kCFStringEncodingUTF8 ));
return encodedString;
}

Related

stringwithcontentsofurl file deprecated in ios 2.0 warning IOS7

Hi in my application I'm showing the route map using google map in that its showing warning like .
stringwithcontentsofurl file deprecated in ios 2.0
The code which I'm using.
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:#"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:#"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.google.com/maps?output=dragdir&saddr=%#&daddr=%#", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSLog(#"api url: %#", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
NSString* encodedPoints = [apiResponse stringByMatching:#"points:\\\"([^\\\"]*)\\\"" capture:1L];
return [self decodePolyLine:[encodedPoints mutableCopy]];
}
In the above code in a particular line.
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
This above line its showing warning like that please tell me how to resolve this issue.
Thanks.
As it was depreciated.You can use it as below
NSString *apiResponse = [NSString stringWithContentsOfURL: apiUrl encoding:NSUTF8StringEncoding error:nil];
The method you are using is deprecated. Use [NSString stringWithContentsOfURL: encoding: error:].

Calling Through App with openUrl

I've been trying to use the openUrl function is iOS to dial phone numbers from my app, but it's not going through, the numbers from the response have white spaces, and I've tried to remove it but when I NSLog it's not removing it
(void)phone:(id)sender{
NSString *phone = [[information valueForKeyPath:#"place_detail"] objectForKey:#"phone"];
[phone stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *dial = [NSString stringWithFormat:#"tel://%#", phone];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:dial]];
}
Try replacing this:
[phone stringByReplacingOccurrencesOfString:#" " withString:#""];
With this:
phone = [phone stringByReplacingOccurrencesOfString:#" " withString:#""];
Use this method to remove all forms of white space:
NSArray* words = [yourString componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
NSString* nospacestring = [words componentsJoinedByString:#""];
This is advantageous because it removes not only the space character.
Next just call with the following method:
NSString *value=#"your number";
NSURL *url = [[ NSURL alloc ] initWithString:[NSString stringWithFormat:#"tel://%#",value]];
[[UIApplication sharedApplication] openURL:url];

Apple maps not opening on link

i am very new to iOS-Development. We are developing a WebApp with Cordova at the moment and we need a plugin to open the native maps application when we click on a button. Our current implementation looks like this:
#import "Maps.h"
#implementation Maps
-(void)startNavigation:(CDVInvokedUrlCommand *)command{
NSString* street = [command.arguments objectAtIndex:0];
NSString* town = [command.arguments objectAtIndex:1];
street = [street stringByReplacingOccurrencesOfString:#" " withString:#"+"];
town = [town stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSString* address = [NSString stringWithFormat: #"http://maps.apple.com/?q=%#, %#", street, town];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: address]];
}
#end
But it has no effect, although i can see that the adress-String is correctly. If I pass the town, everything is working fine... Any ideas?
Thanks
Wal
edit:
Thanks I checked the String and it was not a valid URL. No i am escaping the String through
address = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
And everything works fine.
the line:
NSString *address = [NSString stringWithFormat: #"http://maps.apple.com/?q=%#, %#", street, town];
would product 'http://maps.apple.com/?q=blah, blah" -- is that a valid URL (the space)?
you should confirm the application can open using:
[[UIApplication sharedApplication] canOpenURL:someURL];

iOS clickable text inside UITextView

Is there a way to make clickable parts of UITextView. Actually I want to make text something like
By clicking “Register” above, you are agreeing to the Terms of Services and Privacy Statement
where Terms of Services should be one link and Privacy Statement another. And by clicking on those I should do something.
I did it with the code above using this project
- (void)_configureTermsLabel
{
self.termsOfUseLabel.hidden = YES;
self.termsAndConditionsLabel = [[TTTAttributedLabel alloc] initWithFrame:self.termsOfUseLabel.frame];
self.termsAndConditionsLabel.font = [UIFont systemFontOfSize:14];
self.termsAndConditionsLabel.lineBreakMode = UILineBreakModeWordWrap;
self.termsAndConditionsLabel.numberOfLines = 0;
NSString *termsStr = NSLocalizedString(#"Terms of use", #"Terms of use");
NSString *privacyStr = NSLocalizedString(#"Privacy Policy", #"Privacy Policy");
NSString *andStr = NSLocalizedString(#"and", #"and");
NSString *conductStr = NSLocalizedString(#"Code of conduct", #"Code of conduct");
NSString *termsAndConditionsStr = [NSString stringWithFormat:#"%# - %# %# %#", termsStr,
privacyStr, andStr, conductStr];
self.termsAndConditionsLabel.text = termsAndConditionsStr;
NSString *languageCode = [[GLQAppDelegate sharedDelegate] languageIdentifier];
NSURL *termsURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQTermsOfUseURL, languageCode]];
NSURL *privacyURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQPrivacyPolicyURL, languageCode]];
NSURL *conductURL = [NSURL URLWithString:[NSString stringWithFormat:kGLQCodeOfConductURL, languageCode]];
NSRange termsRange = [self.termsAndConditionsLabel.text rangeOfString:termsStr];
NSRange privacyRange = [self.termsAndConditionsLabel.text rangeOfString:privacyStr];
NSRange conductRange = [self.termsAndConditionsLabel.text rangeOfString:conductStr];
[self.termsAndConditionsLabel addLinkToURL:termsURL withRange:termsRange];
[self.termsAndConditionsLabel addLinkToURL:privacyURL withRange:privacyRange];
[self.termsAndConditionsLabel addLinkToURL:conductURL withRange:conductRange];
self.termsAndConditionsLabel.delegate = self;
self.termsAndConditionsLabel.userInteractionEnabled = YES;
[self.scrollView addSubview:self.termsAndConditionsLabel];
}

iOS: phone number dialing not working

I'm building an app that lets you call different service centers from Netherlands. The problem is that some of them have a different commercial format (like 0800-xxxx) and the device can't make the call.
The code looks like this:
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
Do you have any idea how to format the number or to make the phone call, no matter it's format?
EDIT: This is how the phoneNumber is created:
NSString *phoneNumberString = phoneNumber; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", phoneNumberString];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
I used this code and it worked:
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
NSString *strm = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)mob, NULL, CFSTR(":/?#[]#!$&’()*+,;="), kCFStringEncodingUTF8);
NSString *strMob = [[NSString alloc] initWithFormat:#"tel://%#",strm];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strMob]];
try this code . This works perfectly for any kind of format.It will convert into perfect calling URL .

Resources