我有一个应用程序,我想提出一个警报视图,询问他们是否可以查看该应用程序?
任何帮助表示赞赏!
答案 0 :(得分:5)
别。这是糟糕的用户体验。相反,请尝试以下方法:
答案 1 :(得分:2)
这样的事情可以做到:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
答案 2 :(得分:0)
Apple已为提交到应用商店的应用添加了额外规则。
我们无法再在Documents文件夹中存储任意数据文件。只有用户生成的内容(例如他们手动输入和保存的文本文件或他们用相机拍摄的照片)才能保存在Documents文件夹中。
我们现在希望将应用生成的文件存储在 Library / Cache 文件夹中,而不是Documents文件夹中。此外,我们应该使用跳过备份属性标记我们不希望与iCloud同步的文件。
如果不遵守此规定,将导致Apple拒绝该应用。
原因:Documents文件夹现在用于同步到iCloud。 iCloud每隔几分钟就进行一次同步,如果我们将应用程序生成的文件存储在Documents文件夹中,那么它将与用户自己的iCloud同步文件混在一起。
使用这些新代码更新MediaDirectory.m文件是安全的:
+ (NSString *) mediaPathForFileName:(NSString *) fileName
{
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [directoryPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", cachesDirectory, fileName];
return filePath;
}
+ (BOOL)addSkipBackupAttributeToFile:(NSString *) fileName
{
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDirectory = [directoryPaths objectAtIndex:0];
NSString *filePathStr = [NSString stringWithFormat:@"%@/%@", cachesDirectory, fileName];
const char* filePath = [filePathStr fileSystemRepresentation];
const char* attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}
在生成文件后,请记住使用skip backup属性标记我们生成的文本文件:
[MediaDirectory addSkipBackupAttributeToFile:@"fileStatus.txt"];
我之前也有过一个拼写错误,我说写入文件“firstLaunch.txt”应该是“fileStatus.txt”(糟糕的复制和粘贴技巧:P)
当用户点击“不再询问”按钮时,您可以尝试将值(例如“否”)写入文本文件并存储在应用程序的沙箱文档文件夹中。
下次加载应用时,视图控制器会读取此文本文件中的值,如果是“否”,则不显示警报视图。
希望有所帮助。
好的,让我先解释一下我们将要经历的过程:
1)您的应用程序委托类didFinishLaunchingWithOption是在启动应用程序时调用的方法。
2)此方法仅在应用程序启动时被调用一次,因此这是我们创建文本文件的位置,我们写下警报视图的状态是“再次显示”还是“不再显示” UIAlertView
3)我们将在你的UIViewController的viewDidLoad方法中调出警报视图。但是,我们只显示此UIAlertView IF,我们的文本文件中的值不是“不再显示”。如果文本文件中的值是“再次显示”,我们会显示UIAlertView,如果它“不再显示”,我们不显示UIAlertView,有意义吗?
(这些值只是字符串,你可以设置你想要的任何值,我只是用这些随机值来演示)。
是的,现在我们得到了一般流程,让我们实现它。
在AppDelegate.h头文件中:
@interface MyAppDelegate : NSObject
{
...
bool isFirstLaunch;
}
现在在你的AppDelegate.m实现文件中:
// -------------------------------------------------------------------------------------
// I wrote a helper class called "MediaDirectory" that returns me the path to a file
// in the Documents directory of the application sandbox.
// Each application has a copy of the Documents directory so you can safely
// assume this directory will always exist.
//
// This class has been very useful for me, hope you find it useful too.
//
// DOWNLOAD THE FILES AT:
//
// http://www.chewedon.com/classes/MediaDirectory.h
// http://www.chewedon.com/classes/MediaDirectory.m
// -------------------------------------------------------------------------------------
#import <Foundation/NSFileManager.h>
#import <Foundation/NSFileHandle.h>
#import "MediaDirectory.h"
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
NSString *fileStatus = nil;
// Here, the NSFileManager defaultManager fileExistsAtPath method needs a path to
// the file in the Document directory, to get this path, use the static method from
// my MediaDirectory class and pass it the name of the file.
// I called it "fileStatus.txt"
//
// We're checking if the file exist in the Documents directory or not.
// If it does not exist then we create the text file and pass it the
// value "show again", otherwise we do nothing.
// (We don't want to overwrite the value everytime the app starts)
if([[NSFileManager defaultManager] fileExistsAtPath:[MediaDirectory mediaPathForFileName:@"fileStatus.txt"]] == NO)
{
// prepare fileStatus.txt by setting "show again" into our string
// we will write this string into the text file later
fileStatus = [[NSString alloc] initWithString:@"show again"];
// now we write the value "show again" so that the UIAlertView will show
// when it checks for the value, until the user clicks on no and we set
// this value to "don't show again" later in another piece of code
[fileStatus writeToFile:[MediaDirectory mediaPathForFileName:@"fileStatus.txt"]
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
[fileStatus release];
...
// rest of your didFinishLaunchingWithOptions method
[window addSubview:[viewController view]];
[self.window makeKeyAndVisible];
[self initGlobals];
return YES;
}
现在在你的UIViewController类中,我们需要使这个类符合UIAlertView协议。我们将使用委托方法之一告诉我们何时单击UIAlertView上的按钮。
@interface MyViewController : UIViewController <UIAlertViewDelegate>
{
...
}
在我们的实现文件(MyViewController.m文件)中,我们在显示UIAlertView之前检查存储在文本文件中的值。
#import "MediaDirectory.h"
#import <Foundation/NSFileManager.h>
#import <Foundation/NSFileHandle.h>
-(void)viewDidLoad
{
...
BOOL shouldShowAlert = false;
NSString *fileStatus = nil;
// check if the file exists in the Documents directory and read its value
// if it does. If the value read from file is "show again",
// we bring up a UIAlertView
if([[NSFileManager defaultManager] fileExistsAtPath:[MediaDirectory mediaPathForFileName:@"fileStatus.txt"]] == YES)
{
fileStatus = [[NSMutableString alloc] initWithContentsOfFile:[MediaDirectory mediaPathForFileName:@"fileStatus.txt"]
encoding:NSUTF8StringEncoding
error:nil];
if([fileStatus isEqualToString:@"show again"] == YES)
{
shouldShowAlert = true;
}
else if([fileStatus isEqualToString:@"don't show again"] == YES)
{
shouldShowAlert = false;
}
}
if(shouldShowAlert == true)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"My Alert View Title"
message:@"my message"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Don't Show Again", nil];
// we set a tag for this UIAlertView to distinguish between this UIAlertView
// and other UIAlertView in case there are any.
// I use a value of 10, you can use any value but make sure it is unique
[alert setTag:10];
[alert show];
[alert release];
}
}
现在我们来到最后一部分,我们处理用户点击UIAlertView的按钮。
在MyViewController.m文件的某处,编写此委托方法:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// making sure its the UIAlertView we want
if([alertView tag] == 10)
{
// By default, the "cancel" button has an index of 0
// and the next button would have a index of 1
// In our case, we set the first button is "OK"
// and "Don't Show Again" as second button
if(buttonIndex == 1)
{
NSString *fileStatus = [[NSString alloc] initWithString:@"don't show again"];
[fileStatus writeToFile:[MediaDirectory mediaPathForFileName:@"fileStatus.txt"]
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
[fileStatus release];
}
}
}
希望我没有错过任何内容,让我知道它是否有效。