私のアプリに、ユーザーが私のアプリケーションのiTunesURLを使用して友人にメールを送信できる機能が必要です。どうしてそれは可能ですか?
ありがとう。
通常見られる長くて紛らわしい URL ではなく、よりシンプルで論理的な App Store リンクを作成できます。iTunes Store には、はるかに論理的な隠し URL 形式があります。リンク先に応じて、次のいずれかの形式で URL を作成するだけです。
作成するメールの本文にこの形式の URL を含めるだけです。
(スペースは問題を引き起こす可能性があることに注意してください。ただし、それらを完全に省略してもうまくいくことがわかりました-http ://itunes.com/app/FrootGrooveは「Froot Groove」というアプリにリダイレクトします。)
(また、これがうまくいかない場合は、iTunes リンク メーカーがここにあります) 。
あなたのコードはこのようなものになります(私のものから抽出され、匿名化され、テストされていません)
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
iPhone 3.0 ではもっと良いことができますが、それについてはまだお話しできません。
OS 3.0では、MessageUIフレームワークを使用して、アプリを終了せずにこれを行うことができます(3.0より前のデバイスのフォールバックとしてJaneのコードを使用)。
- (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];
}
クラスはMFMailComposeViewControllerDelegate
プロトコルを採用する必要があることに注意してください。添付ファイルを含めたり、本文でHTMLを使用したりすることもできます。
appstore.com/APP_NAME を使用して、iTunes でアプリを起動できるようになりました。これは、デスクトップおよび iOS デバイスで機能します。ただし、これは他の方法ほど信頼性が高くありません。こちらの回答を参照してくださいApple appStore のバニティ URL を作成する方法は?
このコードは、アプリ名に基づいてアプリ ストア リンクを自動的に生成します。他には何も必要ありません。ドラッグ アンド ドロップします。
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);
http://itunes.com/app/angrybirdsのようなリンクが表示されます
ところで、ID によるアプリケーションへのリンクは、アプリケーションの App Store にアクセスし、[友達に教える] をクリックして見つけることができます。その後、自分自身にメールを送信します。これは非常に有益であることがわかりました。