Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Short links to the App Store for your apps and company
//
// NSString+AppStore.h
// NSString+Additions
//
// Created by Darcy Liu on 11/5/14.
// Copyright (c) 2014 Darcy Liu. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSString (AppStore)
+ (NSString *)standardAppStoreLinkByAppleID:(NSString *)appleID;
+ (NSString *)appStoreLinkByAppleID:(NSString *)appleID;
+ (NSString *)appStoreLinkByCompanyName:(NSString *)companyName;
+ (NSString *)appStoreLinkByAppName:(NSString *)appName;
+ (NSString *)appStoreLinkApp:(NSString *)appName byCompany:(NSString *)companyName;
+ (void)appStoreLinksTest;
@end
//
// NSString+AppStore.m
// NSString+Additions
//
// Created by Darcy Liu on 11/5/14.
// Copyright (c) 2014 Darcy Liu. All rights reserved.
//
#import "NSString+AppStore.h"
// via https://developer.apple.com/library/ios/qa/qa1633/_index.html
#define IOS_APP_STORE_URL @"http://appstore.com/%@"
#define MAC_APP_STORE_URL @"http://appstore.com/mac/%@"
@implementation NSString (AppStore)
- (NSString *)as_stringByAccentRemoved
{
NSData *asciiEncoded = [self dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
return [[NSString alloc] initWithData:asciiEncoded encoding:NSASCIIStringEncoding];
}
- (NSString *)as_cleanStringForAppStore
{
NSMutableCharacterSet *mutableCharacterSet = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
[mutableCharacterSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[mutableCharacterSet formUnionWithCharacterSet:[NSCharacterSet characterSetWithCharactersInString:@"©™®"]];
NSString *cleanString = [[self lowercaseString] stringByReplacingOccurrencesOfString:@"&" withString:@"and"];
NSArray *components =[[cleanString lowercaseString] componentsSeparatedByCharactersInSet:mutableCharacterSet];
cleanString = [[components componentsJoinedByString:@""] as_stringByAccentRemoved];
return cleanString;
}
// via https://linkmaker.itunes.apple.com/us/
+ (NSString *)standardAppStoreLinkByAppleID:(NSString *)appleID
{
NSParameterAssert(appleID);
return [NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?mt=8",appleID];
}
+ (NSString *)appStoreLinkByAppleID:(NSString *)appleID
{
NSParameterAssert(appleID);
return [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@",appleID];
}
+ (NSString *)appStoreLinkByCompanyName:(NSString *)companyName
{
NSParameterAssert(companyName);
#if TARGET_OS_IPHONE
return [NSString stringWithFormat:IOS_APP_STORE_URL,[companyName as_cleanStringForAppStore]];
#else
return [NSString stringWithFormat:MAC_APP_STORE_URL,[companyName as_cleanStringForAppStore]];
#endif
}
+ (NSString *)appStoreLinkByAppName:(NSString *)appName
{
NSParameterAssert(appName);
#if TARGET_OS_IPHONE
return [NSString stringWithFormat:IOS_APP_STORE_URL,[appName as_cleanStringForAppStore]];
#else
return [NSString stringWithFormat:MAC_APP_STORE_URL,[appName as_cleanStringForAppStore]];
#endif
}
+ (NSString *)appStoreLinkApp:(NSString *)appName byCompany:(NSString *)companyName
{
NSParameterAssert(appName);
NSParameterAssert(companyName);
#if TARGET_OS_IPHONE
return [NSString stringWithFormat:@"http://appstore.com/%@/%@",[companyName as_cleanStringForAppStore],[appName as_cleanStringForAppStore]];
#else
return [NSString stringWithFormat:@"http://appstore.com/mac/%@/%@",[companyName as_cleanStringForAppStore],[appName as_cleanStringForAppStore]];
#endif
}
+ (void)appStoreLinksTest
{
#if TARGET_OS_IPHONE
NSAssert([[NSString standardAppStoreLinkByAppleID:@"361285480"] isEqualToString:@"https://itunes.apple.com/us/app/id361285480?mt=8"],@"https://itunes.apple.com/us/app/id361285480?mt=8");
NSAssert([[NSString appStoreLinkByAppleID:@"361285480"] isEqualToString:@"https://itunes.apple.com/app/id361285480"],@"https://itunes.apple.com/app/id361285480");
NSAssert([[NSString appStoreLinkByCompanyName:@"Gameloft"] isEqualToString:@"http://appstore.com/gameloft"], @"Gameloft");
NSAssert([[NSString appStoreLinkByCompanyName:@"Activision Publishing, Inc."] isEqualToString:@"http://appstore.com/activisionpublishinginc"], @"Activision Publishing, Inc.");
NSAssert([[NSString appStoreLinkByCompanyName:@"Chen's Photography & Software"] isEqualToString:@"http://appstore.com/chensphotographyandsoftware"], @"Chen's Photography & Software");
NSAssert([[NSString appStoreLinkByAppName:@"Ocarina"] isEqualToString:@"http://appstore.com/ocarina"], @"Ocarina");
NSAssert([[NSString appStoreLinkByAppName:@"Where’s My Perry?"] isEqualToString:@"http://appstore.com/wheresmyperry"], @"Where’s My Perry?");
NSAssert([[NSString appStoreLinkByAppName:@"Brain Challenge™"] isEqualToString:@"http://appstore.com/brainchallenge"], @"Brain Challenge™");
#endif
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.