How to escape characters in href=mailto (iPhone) - objective-c

I want to create a link on a UIWebView that emails content to the user. A simple example is:
<a href="mailto:zippy#example.com?subject=Sarcasm&body=I ยป
<b>love</b> <html> mail!">Hi!</a>
Which creates a message that looks like this:
-- begin message ---
To: zippy#example.com
Subject: Sarcasm
I love mail!
-- end message --
I need something more elaborate. The subject will contain multiple words with spaces. The body will contain HTML, list (<ul>) and hyperlinks with quotes in their href. How do I create something like that?
Here's an example:
subject= "This is only a test"
body= "This is the body part. Here's a list of links:
<ul>
<li>abc.com</li>
<li>xyz.com</li>
</ul>
The end."
Also, why does the simulator do anything when clicking a mailto link?

Fields are URL-encoded (in Cocoa you can use stringByAddingPercentEscapesUsingEncoding: for that).
4thspace mentioned that Mail.app does allow HTML. This is against the
mailto RFC2368, which clearly says that body is supposed to be text/plain.

The simulator doesn't have Mail.app, as you can see from the home screen of it, so it has nothing to open when it encounters a mailto link.
As far as I know though, there is no way to use mailto: to send an html formatted email.

You will need to the MessageUI.framework reference to your project.
Add the following to your .h file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
Add the delegate <MFMailComposeViewControllerDelegate>
Create a couple methods similar to the following in your .m file.
-(IBAction)checkCanSendMail:(id)sender{
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil) {
if ([mailClass canSendMail]) {
[self displayComposerSheet];
}
else {
//Display alert for not compatible. Need iPhone OS 3.0 or greater. Or implement alternative method of sending email.
}
}
else {
//Display alert for not compatible. Need iPhone OS 3.0 or greater. Or implement alternative method of sending email.
}
}
-(void)displayComposerSheet {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"Email Subject"];
//Set our to address, cc and bcc
NSArray *toRecipients = [NSArray arrayWithObject:#"primary#domain.com"];
//NSArray *ccRecipients = [NSArray arrayWithObjects:#"first#domain.com",#"second#domain.com",nil];
//NSArray *bccRecipients = [NSArray arrayWithObjects:#"first#domain.com",#"second#domain.com",nil];
[mailer setToRecipients:toRecipients];
//[mailer setCcRecipients:ccRecipients];
//[mailer setBccRecipients:bccRecipients];
NSString *emailBody = #"\
<html><head>\
</head><body>\
This is some HTML text\
</body></html>";
[mailer setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
Apple sample code with more instructions available at: http://developer.apple.com/iphone/library/samplecode/MailComposer/
I know this doesn't use the webView but it does allow you to create HTML emails from within your application.

Related

Parse ios8 User Push Notification Actions

I've registered the Notification Action properly and confirmed with a call to [[UIApplication sharedApplication] currentUserNotificationSettings].
I'm using Parse to assemble and send the push like this:
NSString *pushMessage = [NSString stringWithFormat:#"%# just directly asked: %#", [[PFUser currentUser] username], [self.question objectForKey:#"text"]];
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
pushMessage, #"alert",
#"questionNotification", #"category",
nil];
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setData:data];
[push sendPushInBackground];
The notification does get delivered to the device but it does not have any of the custom actions available that have been registered for the category.
At this point I feel like Parse cannot properly convey the category in the payload? Has anyone gotten them to work with Parse?
Edit:
This is the response I get from currentUserNotificationSettings that make me thing the action has been registered successfully:
<UIUserNotificationSettings: 0x14e3eaa0; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);categories: {(
<UIUserNotificationCategory: 0x14e54c40; identifier: questionNotification, actions: {
1 = (
"<UIMutableUserNotificationAction: 0x14e4b860; identifier: yupAction, title: YUP activationMode: UIUserNotificationActivationModeBackground, isAuthenticationRequired:NO, isDestructive:NO>"
);}>)};>
Update: More research, in case anyone else is having this problem, when I output the userInfo received from the push notification I get:
{aps = {alert = "m just directly asked: Abaxinj";}; category = "QUESTION_CATEGORY";}
So it appears that Parse is putting the category field outside of the aps container, which is probably the problem.
Opened a bug with Parse, they confirmed they're not equipped to handle this yet. They said to check the Parse.com blog for updates when they add ios8's new functionality.

Objective-C delegate incompatible type

I am using http://www.vfr.org/, pdf viewer, and I try to add a button to open PDF in IBOOK.
I added the button and action to it, but I stuck t the moment when I want open ibooks application with this pdf.
I tried 2 solutions:
button tapped action:
NSURL *fileURL = document.fileURL;
NSString *fileName = document.fileName; // Document
[[UIApplication sharedApplication] openURL:fileURL];
It opens the IBOOKS but the PDF never gets loaded.
I am assuming the URL format can be wrong I tried even hard code a PDF URL like:
NSString *stringURL = #"itms-books://linktopdf.pdf";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
But the same result.
2) Button tapped action:
NSURL *fileURL = document.fileURL;
NSString *fileName = document.fileName; // Document
UIDocumentInteractionController *docController = [[UIDocumentInteractionController alloc] init];
docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
docController.delegate = self;
But I have an warning where I try to delegate: docController.delegate = self;
assigning to id UIDocumentInteractionControllerDelegate from Incompatible ReaderViewController
Can someone help me to make it work at least 1 of these solution.
Has the ReaderViewController this kind of line in the header file: #interface ReaderViewController : NSObject < UIDocumentInteractionControllerDelegate > ?
Your class must conform to the UIDocumentInteractionControllerDelegate protocol, so in your .h file make it look like
#interface ReaderViewController : PerantClass <UIDocumentInteractionControllerDelegate>
{
...
}
Then the compiler will know that your class conforms to that protocol. Of course, you still need to implement the needed methods.
ALso Check this Link - 1 And Link - 2.

How to set email subject from dataDetectorTypeLink?

In the text of a UITextView, I have an email address, and the dataDetectorType is set to dataDetectorTypeLink. Is there any way to set the subject line of an email with this configuration? I know how to set the subject line of an email using an MFMailComposeController, but is there a way to combine that with dataDetectorType?
EDIT: Here's my (re)definition of `openURL:(NSURL *)url in my app delegate:
-(void)openURL:(NSURL *)url
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:#"feedback on Gay Haiku"];
[self presentViewController:mailer animated:YES completion:NULL];
}
But I get an error No visible #interface for AppDelegate declares the selectorpresentViewController:animated:`.
Did you try appending ?subject= to the link?
#"mailto:webmaster#site.com?subject=Web Site Extraordinaire"
I just realize could use that only if you switched to a UIWebView... Is that an option?
EDIT:
The other way is to subclass UIApplication and override openURL:. This is described here.
Since this seems to be fixed text that you control, you can add a NSLinkAttributeName attribute to the NSAttributedString at the range of your email address instead of enabling automatic link detection. This will let you specify the full URL that you want the system to open when the link is tapped. Then you can use Mundi's suggestion (include subject= in the URL) to set the subject of the email.

Get mails from iphone settings

there any way to get my personal email accounts created in the iphone settings ??
I am making an application to send mail and would like to select one of the email accounts I have on my iPhone to send and then select the destianatorios of my agenda.
Thanks
I'm not exactly sure what you're trying to do, but iOS offers several ways to display an email composition view controller, as well as methods to access the contacts a user has saved on his /her iDevice.
To display a mail composition view, make a weak link to the MessageUI.framework in your project (a weak link is preferred since the MessageUI.framework is not available on very old iOS versions), then do something like this:
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil) {
// MessageUI Library is available. Presenting modal mail composer view.
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:#"This is the subject of the email"];
[mailViewController setMessageBody:#"This is the body of the email." isHTML:NO];
[self presentModalViewController:mailViewController animated:YES];
[mailViewController release];
} else {
// MessageUI Library not available. Opening mail.app using a URL scheme.
// Note that this URL scheme only works on iOS3 and below, and seems to only accept a
// limited number of characters. For this reason, we only attach the URL.
NSString *mailBody = #"This is the body of the email."
NSString *mailSchemeURL = [NSString stringWithFormat:#"mailto:?body=%#", mailBody];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[mailSchemeURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
}
If you want to access contacts on the iDevice, link AddressBook.framework into your project. You can access the values on the device by following the instructions in Apple's programming guide. For example, you can get an array of all contacts like so:
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *contacts = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
I expect you can instantiate a mail composition view with a specific contact by using a combination of the above. Hope this helps!

Detect if NSString contains a URL and generate a "link" to open inside the app a Safari View

I have am reading a twitter feed in my iPhone application and can do it correctly, but I'd like to evolve it in a way to detect if the whole NSString contains any URL or URLs and create a "link" that will open a UIWebView within the same application.
Can you guide me on how to perform this task?
-(void) setTextTwitter:(NSString *)text WithDate:(NSString*)date
{
[text retain];
[textTwitter release], textTwitter = nil;
textTwitter = text;
[date retain];
[dateTwitter release], dateTwitter = nil;
dateTwitter = date;
[self setNeedsDisplay];
}
Check out Craig Hockenberry's IFTweetLabel, which can be found here.
Or you can use the label provided by Three20 library, which can be found here.
Or the simplest solution: use UIWebView with dataDetectorTypes set to UIDataDetectorTypeLink.
Cheers, Paul