Question

I want to have a feature in my app where the user can send an email to a friend with the iTunes URL to my application. How is it possible?

Thanks.

Was it helpful?

Solution

Rather than the long and confusing urls that you usually see, you can create App Store links that are much simpler and more logical. The iTunes Store has a hidden URL format that’s much more logical. Depending on what you’re linking to, you just need to build a URL in one of these formats:

  1. Artist’s name or App Store developer’s name: http://itunes.com/Artist_Or_Developer_Name
  2. Album name: http://itunes.com/Artist_Name/Album_Name
  3. Apps: http://itunes.com/app/App_Name
  4. Movies: http://itunes.com/movie/Movie_Title
  5. TV: http://itunes.com/tv/Show_Title

Just include a url of this format in the body of the email you create.

(Note that spaces might cause problems, but I found that omitting them entirely worked for me - http://itunes.com/app/FrootGroove redirects to the app called "Froot Groove".)

(Also note that if this doesn't work for you, the iTunes link maker is here)

Your code will be something like this (extracted from mine, anonymised and not tested)

NSString* body = [NSString stringWithFormat:@"Get my app here - %@.\n",myUrl];

#if __IPHONE_OS_VERSION_MIN_REQUIRED <= __IPHONE_2_2
[NSThread sleepForTimeInterval:1.0];
NSString* crlfBody = [body stringByReplacingOccurrencesOfString:@"\n" withString:@"\r\n"];
NSString* escapedBody = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  (CFStringRef)crlfBody, NULL,  CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease];

NSString *mailtoPrefix = [@"mailto:xxx@wibble.com?subject=Get my app&body=" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

// Finally, combine to create the fully escaped URL string
NSString *mailtoStr = [mailtoPrefix stringByAppendingString:escapedBody];

// And let the application open the merged URL
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoStr]];
#endif

You can do better things in iPhone 3.0, but I can't talk about those yet.

OTHER TIPS

In OS 3.0 you can use the MessageUI framework to do this without leaving the app (using Jane's code as the fallback for pre-3.0 devices):

- (void)sendEmail
{
    NSString* body = [NSString stringWithFormat:@"Get my app here - %@.\n",myUrl];

#if __IPHONE_OS_VERSION_MIN_REQUIRED <= __IPHONE_2_2
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil && [mailClass canSendMail])
    {
        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
        picker.subject = @"Get my app";
        [picker setToRecipients:[NSArray arrayWithObject:@"xxx@wibble.com"];
        [picker setMessageBody:body isHTML:NO];

        [self presentModalViewController:picker animated:NO];
        [picker release];
    } else {
        [NSThread sleepForTimeInterval:1.0];
        NSString* crlfBody = [body stringByReplacingOccurrencesOfString:@"\n" withString:@"\r\n"];
        NSString* escapedBody = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,  (CFStringRef)crlfBody, NULL,  CFSTR("?=&+"), kCFStringEncodingUTF8) autorelease];

        NSString *mailtoPrefix = [@"mailto:xxx@wibble.com?subject=Get my app&body=" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        // Finally, combine to create the fully escaped URL string
        NSString *mailtoStr = [mailtoPrefix stringByAppendingString:escapedBody];

        // And let the application open the merged URL
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailtoStr]];
    }
#endif
}

#pragma mark -
#pragma mark Mail Composer Delegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{
    if (result == MFMailComposeResultFailed) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    [self dismissModalViewControllerAnimated:YES];
}

Note that your class must adopt the MFMailComposeViewControllerDelegate protocol. You can also include attachments, use HTML in the body, and more.

You can now use appstore.com/APP_NAME to launch an app in iTunes. This works on the desktop and on iOS devices. This is not as reliable as other methods however. See answer here How to create vanity url for apple appStore?

This code generates the app store link automatically based on the app name, nothing else is required, drag & drop:

NSCharacterSet *trimSet = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];    
NSArray *trimmedAppname = [[NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]] componentsSeparatedByCharactersInSet:trimSet];
NSString *appStoreLink = @"http://itunes.com/app/"; 
for (NSString *part in trimmedAppname) appStoreLink = [NSString stringWithFormat:@"%@%@",appStoreLink,part];
NSLog(@"App store URL:%@",appStoreLink);

It gives you a link like http://itunes.com/app/angrybirds

By the way, the link to the application by its ID can be found by visiting the Apps Store for your application and clicking on the "Tell A Friend" -- then send an email to yourself. I found this to be very informative.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top