iOs Custom scheme in MFMailComposeViewController - objective-c

I'm trying to send via MFMailComposeViewController a custom url scheme so that the receipient could tap on the embedded link and open my application on his iphone.
So far the receipient gets an email but nothing happens when he taps on the LINK.
here is the code:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self; // <- very important step if you want feedbacks on what the user did with your email sheet
[picker setSubject:#"Incoming task"];
// Fill out the email body text
NSString * types = #"";
for(NSString *type in task.location_types){
types = [types stringByAppendingFormat:#"%#|",type];
}
if(types.length > 1){
types = [types substringToIndex:types.length - 1];
}
NSString *desc = [task.description stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *link = [NSString stringWithFormat:#"helpy://?taskid=%d&desc=%#&types=%#\"",task.TaskId,desc,types];
NSString *emailBody = [NSString stringWithFormat:#"TAP HERE<script type=\"text/javascript\" charset=\"utf-8\">function doSomething() { window.location = \"%#\;return false;}</script>",link];
[picker setMessageBody:emailBody isHTML:YES];
picker.navigationBar.barStyle = UIBarStyleBlack;
[controller presentModalViewController:picker animated:YES];
[picker release];
helpy://?taskid=..... is my custom registered URL Scheme and if I type it from the address bar of the browser it opens up my application on the iPhone.
For some reason when this link is embedded into email, tapping it does not do anything.
Any help?
thanks

I highly doubt that Mail will execute JavaScript on iOS (even on the desktop I highly doubt it, but haven't tried it though). Simply set the URL as the href attribute of your hyperlink and everything should work fine. Any reason why you want to do it with JavaScript? I'm just curious ;-)
NSString *link = [NSString stringWithFormat:#"helpy://?taskid=%d&desc=%#&types=%#\"",task.TaskId,desc,types];
NSString *emailBody = [NSString stringWithFormat:#"TAP HERE", link];

Related

Objective-C MPMediaItem with null URL

I have a project that makes use of a MPMediaPickerController to select audio files from the Media Player. However, when trying to store it's URL, I am getting nothing but null returned. My code can be seen below:
- (void)showMediaPicker:(NSString *)title {
MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAny];
picker.delegate = self;
picker.prompt = title;
picker.allowsPickingMultipleItems = NO;
picker.showsCloudItems = NO;
[self.viewController presentViewController:picker animated:YES completion:NULL];
}
- (void)mediaPicker: (MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
MPMediaItem *item = [mediaItemCollection.items firstObject];
NSURL *assetURL = [item valueForProperty:MPMediaItemPropertyAssetURL];//returning null
NSString *type = [self contentTypeForFile:assetURL.lastPathComponent];
NSString *title = [item valueForProperty:MPMediaItemPropertyTitle];
[self callbackWithName:title type:type url:assetURL];
}
For whatever reason, my *assetURL is nil upon selecting a single audio item from the device's library. The only answers i could find with respect to this problem often relate to the url being null when a cloud item is selected from the MediaPicker. However, as seen above, I have set showCloudItems = NO.
I would greatly appreciate any help; let me know if you need any additional information!
Turns out, music from the "iCloud Music Library" is DRM protected and therefore, the assetURL is left null when selected in the MPMediaPickerController. The simple fix was to turn off the "iCloud Music Library" setting from within the settings for the "Music" app. I would expect picker.showsCloudItems = NO; to prevent this, but apparently I was mistaken.

Apple URL Scheme for launching app from database information

I'm quite frustrated with an issue I'm experiencing that Is coming with a lack of information. I'm unable to locate any information regarding my issue and I'm starting to believe It isn't possible what I want to do. Here is my issue and thank you for reading.
I would like for a button on my ViewController to open Apple Maps from Longitude and Latitude coordinates that are stored in my Sqlite database Tables. I have been able to successfully do this by adding a MapView. I want to reproduce this function and have the user be able to click "Get Directions" button and It will popup the Apple Maps app with the end address setup automatically. By using #"finishLat" and #"finishLng" I'm able to collect the database information. Is this possible when using the Apple URL Scheme?
This is a sample of what the Mapview does with the information. The only part I want from this is the #"finishLat" and #"finishLng". I will also need to use the "Run" feature as well to collect the correct address.
[super viewDidLoad];
CGRect bounds = self.view.bounds;
mapview = [[MapView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
[self.view addSubview:mapview];
NSArray *result = [_runs objectForKey:#"results"];
NSDictionary *arr = [result objectAtIndex:0];
arr = [arr objectForKey:#"run"];
NSMutableArray *arrPlaces = [[NSMutableArray alloc] init];
NSArray *arrDirvers = [arr objectForKey:#"drivers"];
for (int i =0; i < [arrDirvers count]; i++) {
NSDictionary *asdfg = [arrDirvers objectAtIndex:i];
Place *startPt = [[Place alloc] init];
Place *endPt = [[Place alloc] init];
NSString *temp = [asdfg objectForKey:#"startLat"];
startPt.latitude = [temp floatValue];
temp = [asdfg objectForKey:#"startLng"];
startPt.longitude = [temp floatValue];
startPt.name = [asdfg objectForKey:#"name"];
temp = [asdfg objectForKey:#"finishLat"];
endPt.latitude = [temp floatValue];
temp = [asdfg objectForKey:#"finishLng"];
endPt.longitude = [temp floatValue];
endPt.name = [asdfg objectForKey:#"name"];
[arrPlaces addObject:startPt];
[arrPlaces addObject:endPt];
This is what I have.
- (IBAction)GetDirections:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://maps.apple.com/?q"]];
}
Rather than using openURL: you can create an instance of MKMapItem and use openInMapsWithLaunchOptions:. You can supply the MKLaunchOptionsDirectionsModeKey option to request directions from the users current location.
Alternatively, use openMapsWithItems:launchOptions: to navigate between 2 known locations.
To open in the Apple Maps app use:
- (IBAction)GetDirections:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"http://maps.apple.com/?ll=<lattitude>,<longitude>"]];
}
If you want to open Apple Maps to a driving directions screen, you will need to use this url:
http://maps.apple.com/?saddr=<origin>&daddr=<destination>
You can also set one of these to be "Current%20Location" to use the user's current location.

ShareKit email form not always displaying

I'm using ShareKit to share objects to Twitter, Facebook, and Email. Twitter and Facebook works fine, but email doesn't. And I don't really know why.
It seems like it works quite randomly.
I set it up like this:
[self.customView.shareButton addTarget:self action:#selector(sharePost:) forControlEvents:UIControlEventTouchUpInside];
- (void)sharePost:(UIButton *)sender
{
NSURL *url = [NSURL URLWithString:self.currentPost.link];
// Add image.
NSString *imageUrlString = [NSString stringWithFormat:[link]];
NSURL *imageUrl = [NSURL URLWithString:imageUrlString];
UIImageView *postImage = [[UIImageView alloc] init];
[postImage setImageWithURL:imageUrl];
SHKItem *item = [SHKItem image:postImage.image title:[NSString stringWithFormat:#"%#", self.currentPost.title]];
[item setURL:url];
[item setMailBody:[NSString stringWithFormat:#"%# %#", self.currentPost.title, self.currentPost.link]];
// Get the ShareKit action sheet
// This works
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// ShareKit detects top view controller (the one intended to present ShareKit UI) automatically,
// but sometimes it may not find one. To be safe, set it explicitly.
[SHK setRootViewController:self];
// Display the action sheet.
[actionSheet showInView:self.view];
}
Any ideas or what I'm missing or doing wrong?
Try to change the RootViewController and view you are using:
[SHK setRootViewController:self.view.window.rootViewController];
[actionSheet showInView:self.view.window.rootViewController.view];
Most likely your view controller hierarchy is somewhat messed up.

Set HTML content to the mail body of MFMailComposeViewController's view

I'm using MessageUI framework to compose email in my app. I want set HTML content for the mail body.
So I refered Mugunthkumar's blog and I coded as follow,
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
NSString *pageLink = #"http://mugunthkumar.com/mygreatapp"; // replace it with yours
NSString *iTunesLink = #"http://link-to-mygreatapp"; // replate it with yours
NSString *emailBody =
[NSString stringWithFormat:#"%#\n\n<h3>Sent from <a href = '%#'>MyGreatApp</a> on iPhone. <a href = '%#'>Download</a> yours from AppStore now!</h3>", #"title", pageLink, iTunesLink];
[picker setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:picker animated:YES];
[picker release];
When I ran the app and go to the email compose view, I'm getting the following mail body,
I'm using XCode4 and iOS 4.3.
I can not see any issue with my code.
But I need to insert clickable URLs to my mail body.
please help !!
If you want to send an HTML-formatted email, it is wrong to escape the HTML tags in the email body. Use </a> instead of </a> etc.

Sharekit not populating facebook text box

Here is the code I use. I am able to see popup but I do not see the text pre-populated in the facebook popup.
Any suggestions?
- (IBAction) shareViaFacebook
{
[self prepareToShare];
// create the item to share
NSString *share = [NSString stringWithFormat:#"I'm tracking %# in some stuff, check it out! http://somewebsite.com/g/%d #TEST",
self.game.name,
[self.game.serverId intValue]];
SHKItem *item = [SHKItem text:share];
// share the item
[SHKFacebook shareItem:item];
}
On the same age, the following code for twitter works just fine. That is everything gets prepopulated
- (IBAction) shareViaTwitter
{
[self prepareToShare];
// create the item to share
NSString *share = [NSString stringWithFormat:#"I'm tracking %# in some stuff, check it out! http://somewebsite.com/g/%d #TEST",
self.game.name,
[self.game.serverId intValue]];
SHKItem *item = [SHKItem text:share];
// share the item
[SHKTwitter shareItem:item];
}
This is an example from my project using ShareKit:
NSString *finalPostedString = [[NSString alloc] initWithFormat:#"Check out this"];
SHKItem *item1= [SHKItem text:finalPostedString];
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item1];
[actionSheet showInView:self];
Try that out and hopefully it helps.
EDIT: Or more specifically, check this thread out that also should answer your question:
iPhone/iOS - How to use "ShareKit" to post only to Facebook or only to Twitter