Links inside email message body - ios

I need to modify an existing project. In this project there are several (many) places where the app sends an email with preset text within it. The used function is
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:emailString]];
Which obviously opens iOS Mail, ready to send the message.
Now I need to include links in the body of the messages. Is it possible to do it without switching to MFMailComposeViewController in all that places? How?

it is very simple.
Each html tag should appended, use front slash before double quote in the url beginning and end
Example :
NSString *bodyText =#"<html>";
bodyText = [bodyText stringByAppendingString:#"<head>"];
bodyText = [bodyText stringByAppendingString:#"</head>"];
bodyText = [bodyText stringByAppendingString:#"<body>"];
bodyText = [bodyText stringByAppendingString:#"<a href=\"http://www.devaski.com\">My blog"];
bodyText = [bodyText stringByAppendingString:#"</a>"];
[mailComposer setMessageBody:bodyText isHTML:YES];

Have a look at this document - https://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MailLinks.html#//apple_ref/doc/uid/TP40007892-SW1
The short answer, yes, you can pre-populate the body of the message by adding a body parameter to the URL (i.e mailto:john#apple.com?body=This%20goes%20to%20body). Notice though, that the string has to be properly escaped, which you can do easily with the help of NSString's stringByAddingPercentEscapesUsingEncoding:
I assume that any URL in the body that looks like a URL will be converted into a link by Mail app itself - not sure about that, test it out.

Related

iOS - Pinterest Sharing Description Text not working if I send "&" in description text

I had implemented pinterest sharing code in my app as well. Its working fine. But problem arrives at one scenario see follow
Correct working:
[pinterest createPinWithImageURL:[NSURL URLWithString:imageUrl]
sourceURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",shareUrl]]
description:#"My Description"];
Then it will share Pinterest Description same My Description as per my expectation.
But when I send Description Test like :
[pinterest createPinWithImageURL:[NSURL URLWithString:imageUrl]
sourceURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",shareUrl]]
description:#"My Details & Description"];
Then it will share Pinterest Description like My Details.
My expected text here is My Details & Description this is trunks my string after & symbol.
What actually wrong happening with me please look at here.
AFAIK, Pinterest's Pin It SDK isn't open source, so it's difficult to find out what's really going on.
I would guess, however, that this method is creating a GET request under-the-hood that's incorrectly URL encoding the description parameter (perhaps they're using stringByAddingPercentEscapesUsingEncoding or some other naive method).
I'd recommend contacting the Pinterest SDK developers/maintainers to look into this.
As a quick fix, you might try URL encoding the & yourself. For example, you might try replacing & with %26, e.g.
NSString *description = // ...whatever it should be set to...
description = [description stringByReplacingOccurrencesOfString:#"&" withString:#"%26"];
However, this might actually lead to other problems as the Pinterest SDK is likely doing some sort of URL encoding and would likely encode the % symbol.
Another naive approach may simply be to replace & with the word and, such as
NSString *description = // ...whatever it should be set to...
description = [description stringByReplacingOccurrencesOfString:#"&" withString:#"and"];
Again, it's hacky, but it's a workaround for a bug that's likely in the underlying SDK.

Why am I unable to post to Twitter using SLComposeViewController?

I'm trying to post an article title and an article URL to twitter and then append the app's name to the end of the tweet. So something like
"How to grow a cactus (via #appname)" attached URL
I was having trouble figuring out how to balance the length of the title and URL to make sure that the tweet doesn't exceed 140 characters. So if the URL is really long, cut some of the article title off so it can be under 140 characters.
Looking at Twitter's guidelines for SLComposeViewController they state this part:
Note that the methods for setting initial content respond with Boolean values; this allows you, the developer, to not have to worry about the current count of characters in the body of the Tweet that you are initializing. If the method returns YES, there was enough room to add the content. If the method returns NO, the content you attempted to add would result in a Tweet longer than 140 characters. The logic for character counting also takes into effect the current number of characters required for t.co URL wrapping.
(From the "Code Example" section.)
Given that, I wrote the following code to build a tweet and balance the URL length and article length:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *twitterViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitterViewController addURL:[NSURL URLWithString:self.article.url]];
NSString *titleToShare = self.article.title;
while ([twitterViewController setInitialText:[NSString stringWithFormat:#"%# (via #SyllableApp)", titleToShare]]) {
titleToShare = [titleToShare substringToIndex:titleToShare.length - 1];
}
[self presentViewController:twitterViewController animated:YES completion:nil];
}
Which basically adds the URL then constructs the rest of the tweet by looping through the setInitialText: method until it returns YES, decreasing the length of the title by 1 each time it returns NO in order to get closer to the required length.
But it never returns YES! Even when I know it should. I was using one article where it could potentially exceed 140 characters as the title is 105 characters long and the URL is 55, plus the app credit. So it should theoretically be able to shorten the title down and then add it fine, but it never happens.
So what's going on? How do I accomplish link attachment with SLComposeViewController?
while ([twitterViewController setInitialText:[NSString stringWithFormat:#"%# (via #SyllableApp)", titleToShare]])
=>
while (![twitterViewController setInitialText:[NSString stringWithFormat:#"%# (via #SyllableApp)", titleToShare]])
There is a ! missing in condition, so you shorten the post when it fits, not when it is too long ;)
The problem with this approach is that it works only on iOS6.
SLComposeViewController *social = [[SLComposeViewController alloc] init];
NSString *stringToShare = #"";
for (int i = 0; i < 150; i++)
{
stringToShare = [stringToShare stringByAppendingString:#"x"];
}
NSLog(#"%#",[social setInitialText:stringToShare]?#"YES":#"NO");
yields different results on iOS6 (NO) and iOS7 (YES). The answer to this behaviour comes from the documentation of SLComposeViewController
// Sets the initial text to be posted. Returns NO if the sheet has already been
// presented to the user. On iOS 6.x, this returns NO if the specified text
// will not fit within the character space currently available; on iOS 7.0 and
// later, you may supply text with a length greater than the service supports,
// and the sheet will allow the user to edit it accordingly.
- (BOOL)setInitialText:(NSString *)text;
Probably is worth either having different approaches on iOS6 and 7, or check the length without using SLComposeViewController method.
As imihaly said, you did miss a "!".
And 140 characters count is the limit of title only, not including URL.So your title is 105 characters long which is less than 140,this method should return YES.
There is an open bug with link lengths not getting calculated correctly (radar://10469407). This might be related. You might try sending a Tweet with a link in it to check which URL shortener is being used (I imagine it's using t.co, but I could be wrong).

How to specify localized property list?

I have an ios application which is calling a rest web service. I have created a property list file in my app which contains a dictionary of error codes that will be returned by the server and a corresponding message to show.
I am thinking of having multiple property list files for different languages.
How can i make the app to pick up a specific property list based on locale?
You say that the server is generating an error code and a message.
So I think there are more ways to do it.
First you could send a locale header in your request to the server, so that the server would do all localizations for you (which in my opinion is not as good as solution #2).
So I would prefer the way of letting the server just return error codes and handling the messages on client side.
You could create a language project in xcode, for each language you want to support:
http://www.ibabbleon.com/iphone_app_localization.html#extract
In the localizable.strings file I would do the following:
"RestServiceXYErrorTitle_1" = "Authentication failed";
"RestServiceXYErrorMessage_1" = "Your credentials were wrong";
"RestServiceXYErrorTitle_2" = "Resource not available";
"RestServiceXYErrorMessage_2" = "The resource you requested is no longer available";
....
Then I would take the error code returned by the server, e.g. 1 and put it together with my localization string:
NSString *localizedTitleKey = [NSString stringWithFormat:#"RestServiceXYErrorTitle_%#", errorCode];
NSString *localizedMessageKey = [NSString stringWithFormat:#"RestServiceXYErrorMessage_%#", errorCode];
NSString *errorTitle = NSLocalizedString(localizedTitleKey,#"");
NSString *errorMessage = NSLocalizedString(localizedMessageKey,#"");
I think this would be a good solution

UIApplication openURL can not open a url

I'm trying to implement a job search App. The results are shown to the user in a UITableView.
When a user clicks on a cell, it should open the original Job announcement.
To do this, i implemented the following method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *url = [[NSString alloc]init];
url=[[[xmlParser jobs] objectAtIndex:indexPath.row] urlAddress]; //UrlAddress is an instance variable of type NSString
NSURL *urlJobDetail = [NSURL URLWithString:(url)];
[[UIApplication sharedApplication] openURL: urlJobDetail];
}
The interesting part is: if i type an NSString like #"http://www.google.com" or any other link, it works. But when i try to open a "the urlJobDetail", it just doesn't work... Nothing happens at all...
And i searched it in stackoverflow.com and found this:
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Than the url works but this method changes the original url address and adds lots of % signs like:
"http://www.google.com%20 %20 %20"
So i get an page not found error.
I don't understand why this function doesn't accept a regular NSString variable as?
I checked it with NSLog and the url seems to be perfectly in order.
Any help would be much, very much appreciated !
Thanks in advance
Because it's URL specification related restrictions
Spaces and control characters in URLs must be escaped for transmission in HTTP, as must other disallowed characters... It is necessary to encode any characters disallowed in a URL, including spaces and other binary data not in the allowed character set, using the standard convention of the "%" character followed by two hexadecimal digits.

Alternative to MFMailComposeViewController?

is there an alternative to MFMailComposeViewController that has more-or-less the same functionality, but that will let me handle the sending of the email myself, outside of Apples email-sending system?
What I want to do is to provide a nice email dialog, and then send it through a CRM system instead of directly from the iPad.
Cheers
Nik
YES, You can use the MailCore Framework. You can handle the sending of email by yourself. Before sending the mail you have to set its fields such as To,CC,BCC,Subject,body etc and then send by the following code..
CTCoreMessage *msg = [[CTCoreMessage alloc] init];
[CTSMTPConnection sendMessage:msg server:[server stringValue] username:[username stringValue]
password:[password stringValue] port:[port intValue] useTLS:tls useAuth:auth];
[msg release];
It works for me fine. Hope it will help you..
You will need to create your own email view and handler. You can use something like JSTokenField : https://github.com/jasarien/JSTokenField to get the functionality on the mail "To:" field.

Resources