project_files/HedgewarsMobile/Classes/Appirater/Appirater.m
author sheepluva
Sun, 27 Nov 2016 13:08:12 +0100
changeset 12095 e1bca1ebbad9
parent 11309 402baa6d4f64
permissions -rwxr-xr-x
fix problem with pas2c build. please don't use "and not xyz", use "and (not xyz)"
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11309
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     1
/*
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     2
 This file is part of Appirater.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     3
 
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     4
 Copyright (c) 2012, Arash Payan
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     5
 All rights reserved.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     6
 
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     7
 Permission is hereby granted, free of charge, to any person
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     8
 obtaining a copy of this software and associated documentation
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     9
 files (the "Software"), to deal in the Software without
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    10
 restriction, including without limitation the rights to use,
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    11
 copy, modify, merge, publish, distribute, sublicense, and/or sell
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    12
 copies of the Software, and to permit persons to whom the
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    13
 Software is furnished to do so, subject to the following
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    14
 conditions:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    15
 
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    16
 The above copyright notice and this permission notice shall be
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    17
 included in all copies or substantial portions of the Software.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    18
 
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    19
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    20
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    21
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    22
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    23
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    24
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    25
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    26
 OTHER DEALINGS IN THE SOFTWARE.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    27
 */
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    28
/*
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    29
 * Appirater.m
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    30
 * appirater
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    31
 *
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    32
 * Created by Arash Payan on 9/5/09.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    33
 * http://arashpayan.com
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    34
 * Copyright 2012 Arash Payan. All rights reserved.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    35
 */
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    36
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    37
#import "Appirater.h"
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    38
#import <SystemConfiguration/SCNetworkReachability.h>
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    39
#include <netinet/in.h>
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    40
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    41
#if ! __has_feature(objc_arc)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    42
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    43
#endif
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    44
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    45
NSString *const kAppiraterFirstUseDate				= @"kAppiraterFirstUseDate";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    46
NSString *const kAppiraterUseCount					= @"kAppiraterUseCount";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    47
NSString *const kAppiraterSignificantEventCount		= @"kAppiraterSignificantEventCount";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    48
NSString *const kAppiraterCurrentVersion			= @"kAppiraterCurrentVersion";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    49
NSString *const kAppiraterRatedCurrentVersion		= @"kAppiraterRatedCurrentVersion";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    50
NSString *const kAppiraterDeclinedToRate			= @"kAppiraterDeclinedToRate";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    51
NSString *const kAppiraterReminderRequestDate		= @"kAppiraterReminderRequestDate";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    52
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    53
NSString *templateReviewURL = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=APP_ID";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    54
NSString *templateReviewURLiOS7 = @"itms-apps://itunes.apple.com/app/idAPP_ID";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    55
NSString *templateReviewURLiOS8 = @"itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=APP_ID&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software";
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    56
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    57
static NSString *_appId;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    58
static double _daysUntilPrompt = 30;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    59
static NSInteger _usesUntilPrompt = 20;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    60
static NSInteger _significantEventsUntilPrompt = -1;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    61
static double _timeBeforeReminding = 1;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    62
static BOOL _debug = NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    63
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    64
	static id<AppiraterDelegate> _delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    65
#else
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    66
	__weak static id<AppiraterDelegate> _delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    67
#endif
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    68
static BOOL _usesAnimation = TRUE;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    69
static UIStatusBarStyle _statusBarStyle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    70
static BOOL _modalOpen = false;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    71
static BOOL _alwaysUseMainBundle = NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    72
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    73
@interface Appirater ()
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    74
@property (nonatomic, copy) NSString *alertTitle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    75
@property (nonatomic, copy) NSString *alertMessage;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    76
@property (nonatomic, copy) NSString *alertCancelTitle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    77
@property (nonatomic, copy) NSString *alertRateTitle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    78
@property (nonatomic, copy) NSString *alertRateLaterTitle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    79
- (BOOL)connectedToNetwork;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    80
+ (Appirater*)sharedInstance;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    81
- (void)showPromptWithChecks:(BOOL)withChecks
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    82
      displayRateLaterButton:(BOOL)displayRateLaterButton;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    83
- (void)showRatingAlert:(BOOL)displayRateLaterButton;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    84
- (void)showRatingAlert;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    85
- (BOOL)ratingAlertIsAppropriate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    86
- (BOOL)ratingConditionsHaveBeenMet;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    87
- (void)incrementUseCount;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    88
- (void)hideRatingAlert;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    89
@end
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    90
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    91
@implementation Appirater 
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    92
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    93
@synthesize ratingAlert;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    94
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    95
+ (void) setAppId:(NSString *)appId {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    96
    _appId = appId;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    97
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    98
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    99
+ (void) setDaysUntilPrompt:(double)value {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   100
    _daysUntilPrompt = value;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   101
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   102
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   103
+ (void) setUsesUntilPrompt:(NSInteger)value {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   104
    _usesUntilPrompt = value;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   105
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   106
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   107
+ (void) setSignificantEventsUntilPrompt:(NSInteger)value {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   108
    _significantEventsUntilPrompt = value;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   109
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   110
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   111
+ (void) setTimeBeforeReminding:(double)value {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   112
    _timeBeforeReminding = value;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   113
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   114
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   115
+ (void) setCustomAlertTitle:(NSString *)title
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   116
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   117
    [self sharedInstance].alertTitle = title;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   118
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   119
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   120
+ (void) setCustomAlertMessage:(NSString *)message
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   121
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   122
    [self sharedInstance].alertMessage = message;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   123
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   124
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   125
+ (void) setCustomAlertCancelButtonTitle:(NSString *)cancelTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   126
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   127
    [self sharedInstance].alertCancelTitle = cancelTitle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   128
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   129
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   130
+ (void) setCustomAlertRateButtonTitle:(NSString *)rateTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   131
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   132
    [self sharedInstance].alertRateTitle = rateTitle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   133
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   134
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   135
+ (void) setCustomAlertRateLaterButtonTitle:(NSString *)rateLaterTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   136
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   137
    [self sharedInstance].alertRateLaterTitle = rateLaterTitle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   138
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   139
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   140
+ (void) setDebug:(BOOL)debug {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   141
    _debug = debug;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   142
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   143
+ (void)setDelegate:(id<AppiraterDelegate>)delegate{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   144
	_delegate = delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   145
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   146
+ (void)setUsesAnimation:(BOOL)animation {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   147
	_usesAnimation = animation;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   148
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   149
+ (void)setOpenInAppStore:(BOOL)openInAppStore {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   150
    [Appirater sharedInstance].openInAppStore = openInAppStore;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   151
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   152
+ (void)setStatusBarStyle:(UIStatusBarStyle)style {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   153
	_statusBarStyle = style;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   154
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   155
+ (void)setModalOpen:(BOOL)open {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   156
	_modalOpen = open;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   157
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   158
+ (void)setAlwaysUseMainBundle:(BOOL)alwaysUseMainBundle {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   159
    _alwaysUseMainBundle = alwaysUseMainBundle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   160
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   161
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   162
+ (NSBundle *)bundle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   163
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   164
    NSBundle *bundle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   165
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   166
    if (_alwaysUseMainBundle) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   167
        bundle = [NSBundle mainBundle];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   168
    } else {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   169
        NSURL *appiraterBundleURL = [[NSBundle mainBundle] URLForResource:@"Appirater" withExtension:@"bundle"];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   170
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   171
        if (appiraterBundleURL) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   172
            // Appirater.bundle will likely only exist when used via CocoaPods
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   173
            bundle = [NSBundle bundleWithURL:appiraterBundleURL];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   174
        } else {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   175
            bundle = [NSBundle mainBundle];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   176
        }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   177
    }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   178
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   179
    return bundle;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   180
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   181
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   182
- (NSString *)alertTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   183
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   184
    return _alertTitle ? _alertTitle : APPIRATER_MESSAGE_TITLE;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   185
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   186
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   187
- (NSString *)alertMessage
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   188
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   189
    return _alertMessage ? _alertMessage : APPIRATER_MESSAGE;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   190
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   191
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   192
- (NSString *)alertCancelTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   193
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   194
    return _alertCancelTitle ? _alertCancelTitle : APPIRATER_CANCEL_BUTTON;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   195
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   196
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   197
- (NSString *)alertRateTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   198
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   199
    return _alertRateTitle ? _alertRateTitle : APPIRATER_RATE_BUTTON;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   200
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   201
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   202
- (NSString *)alertRateLaterTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   203
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   204
    return _alertRateLaterTitle ? _alertRateLaterTitle : APPIRATER_RATE_LATER;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   205
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   206
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   207
- (void)dealloc {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   208
    [[NSNotificationCenter defaultCenter] removeObserver:self];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   209
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   210
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   211
- (id)init {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   212
    self = [super init];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   213
    if (self) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   214
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   215
            self.openInAppStore = YES;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   216
        } else {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   217
            self.openInAppStore = NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   218
        }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   219
    }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   220
    
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   221
    return self;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   222
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   223
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   224
- (BOOL)connectedToNetwork {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   225
    // Create zero addy
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   226
    struct sockaddr_in zeroAddress;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   227
    bzero(&zeroAddress, sizeof(zeroAddress));
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   228
    zeroAddress.sin_len = sizeof(zeroAddress);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   229
    zeroAddress.sin_family = AF_INET;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   230
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   231
    // Recover reachability flags
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   232
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   233
    SCNetworkReachabilityFlags flags;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   234
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   235
    Boolean didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   236
    CFRelease(defaultRouteReachability);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   237
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   238
    if (!didRetrieveFlags)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   239
    {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   240
        NSLog(@"Error. Could not recover network reachability flags");
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   241
        return NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   242
    }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   243
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   244
    BOOL isReachable = flags & kSCNetworkFlagsReachable;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   245
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   246
	BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   247
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   248
	NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   249
	NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   250
	NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:self];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   251
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   252
    return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   253
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   254
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   255
+ (Appirater*)sharedInstance {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   256
	static Appirater *appirater = nil;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   257
	if (appirater == nil)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   258
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   259
        static dispatch_once_t onceToken;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   260
        dispatch_once(&onceToken, ^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   261
            appirater = [[Appirater alloc] init];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   262
			appirater.delegate = _delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   263
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive) name:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   264
                UIApplicationWillResignActiveNotification object:nil];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   265
        });
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   266
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   267
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   268
	return appirater;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   269
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   270
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   271
- (void)showRatingAlert:(BOOL)displayRateLaterButton {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   272
  UIAlertView *alertView = nil;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   273
  id <AppiraterDelegate> delegate = _delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   274
    
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   275
  if(delegate && [delegate respondsToSelector:@selector(appiraterShouldDisplayAlert:)] && ![delegate appiraterShouldDisplayAlert:self]) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   276
      return;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   277
  }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   278
  
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   279
  if (displayRateLaterButton) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   280
  	alertView = [[UIAlertView alloc] initWithTitle:self.alertTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   281
                                           message:self.alertMessage
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   282
                                          delegate:self
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   283
                                 cancelButtonTitle:self.alertCancelTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   284
                                 otherButtonTitles:self.alertRateTitle, self.alertRateLaterTitle, nil];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   285
  } else {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   286
  	alertView = [[UIAlertView alloc] initWithTitle:self.alertTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   287
                                           message:self.alertMessage
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   288
                                          delegate:self
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   289
                                 cancelButtonTitle:self.alertCancelTitle
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   290
                                 otherButtonTitles:self.alertRateTitle, nil];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   291
  }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   292
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   293
	self.ratingAlert = alertView;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   294
    [alertView show];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   295
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   296
    if (delegate && [delegate respondsToSelector:@selector(appiraterDidDisplayAlert:)]) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   297
             [delegate appiraterDidDisplayAlert:self];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   298
    }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   299
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   300
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   301
- (void)showRatingAlert
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   302
{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   303
  [self showRatingAlert:true];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   304
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   305
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   306
// is this an ok time to show the alert? (regardless of whether the rating conditions have been met)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   307
//
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   308
// things checked here:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   309
// * connectivity with network
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   310
// * whether user has rated before
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   311
// * whether user has declined to rate
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   312
// * whether rating alert is currently showing visibly
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   313
// things NOT checked here:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   314
// * time since first launch
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   315
// * number of uses of app
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   316
// * number of significant events
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   317
// * time since last reminder
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   318
- (BOOL)ratingAlertIsAppropriate {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   319
    return ([self connectedToNetwork]
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   320
            && ![self userHasDeclinedToRate]
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   321
            && !self.ratingAlert.visible
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   322
            && ![self userHasRatedCurrentVersion]);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   323
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   324
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   325
// have the rating conditions been met/earned? (regardless of whether this would be a moment when it's appropriate to show a new rating alert)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   326
//
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   327
// things checked here:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   328
// * time since first launch
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   329
// * number of uses of app
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   330
// * number of significant events
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   331
// * time since last reminder
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   332
// things NOT checked here:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   333
// * connectivity with network
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   334
// * whether user has rated before
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   335
// * whether user has declined to rate
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   336
// * whether rating alert is currently showing visibly
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   337
- (BOOL)ratingConditionsHaveBeenMet {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   338
	if (_debug)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   339
		return YES;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   340
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   341
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   342
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   343
	NSDate *dateOfFirstLaunch = [NSDate dateWithTimeIntervalSince1970:[userDefaults doubleForKey:kAppiraterFirstUseDate]];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   344
	NSTimeInterval timeSinceFirstLaunch = [[NSDate date] timeIntervalSinceDate:dateOfFirstLaunch];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   345
	NSTimeInterval timeUntilRate = 60 * 60 * 24 * _daysUntilPrompt;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   346
	if (timeSinceFirstLaunch < timeUntilRate)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   347
		return NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   348
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   349
	// check if the app has been used enough
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   350
	NSInteger useCount = [userDefaults integerForKey:kAppiraterUseCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   351
	if (useCount < _usesUntilPrompt)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   352
		return NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   353
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   354
	// check if the user has done enough significant events
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   355
	NSInteger sigEventCount = [userDefaults integerForKey:kAppiraterSignificantEventCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   356
	if (sigEventCount < _significantEventsUntilPrompt)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   357
		return NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   358
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   359
	// if the user wanted to be reminded later, has enough time passed?
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   360
	NSDate *reminderRequestDate = [NSDate dateWithTimeIntervalSince1970:[userDefaults doubleForKey:kAppiraterReminderRequestDate]];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   361
	NSTimeInterval timeSinceReminderRequest = [[NSDate date] timeIntervalSinceDate:reminderRequestDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   362
	NSTimeInterval timeUntilReminder = 60 * 60 * 24 * _timeBeforeReminding;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   363
	if (timeSinceReminderRequest < timeUntilReminder)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   364
		return NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   365
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   366
	return YES;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   367
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   368
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   369
- (void)incrementUseCount {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   370
	// get the app's version
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   371
	NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   372
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   373
	// get the version number that we've been tracking
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   374
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   375
	NSString *trackingVersion = [userDefaults stringForKey:kAppiraterCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   376
	if (trackingVersion == nil)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   377
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   378
		trackingVersion = version;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   379
		[userDefaults setObject:version forKey:kAppiraterCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   380
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   381
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   382
	if (_debug)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   383
		NSLog(@"APPIRATER Tracking version: %@", trackingVersion);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   384
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   385
	if ([trackingVersion isEqualToString:version])
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   386
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   387
		// check if the first use date has been set. if not, set it.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   388
		NSTimeInterval timeInterval = [userDefaults doubleForKey:kAppiraterFirstUseDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   389
		if (timeInterval == 0)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   390
		{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   391
			timeInterval = [[NSDate date] timeIntervalSince1970];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   392
			[userDefaults setDouble:timeInterval forKey:kAppiraterFirstUseDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   393
		}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   394
		
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   395
		// increment the use count
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   396
		NSInteger useCount = [userDefaults integerForKey:kAppiraterUseCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   397
		useCount++;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   398
		[userDefaults setInteger:useCount forKey:kAppiraterUseCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   399
		if (_debug)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   400
			NSLog(@"APPIRATER Use count: %@", @(useCount));
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   401
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   402
	else
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   403
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   404
		// it's a new version of the app, so restart tracking
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   405
		[userDefaults setObject:version forKey:kAppiraterCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   406
		[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterFirstUseDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   407
		[userDefaults setInteger:1 forKey:kAppiraterUseCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   408
		[userDefaults setInteger:0 forKey:kAppiraterSignificantEventCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   409
		[userDefaults setBool:NO forKey:kAppiraterRatedCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   410
		[userDefaults setBool:NO forKey:kAppiraterDeclinedToRate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   411
		[userDefaults setDouble:0 forKey:kAppiraterReminderRequestDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   412
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   413
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   414
	[userDefaults synchronize];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   415
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   416
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   417
- (void)incrementSignificantEventCount {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   418
	// get the app's version
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   419
	NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   420
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   421
	// get the version number that we've been tracking
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   422
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   423
	NSString *trackingVersion = [userDefaults stringForKey:kAppiraterCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   424
	if (trackingVersion == nil)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   425
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   426
		trackingVersion = version;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   427
		[userDefaults setObject:version forKey:kAppiraterCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   428
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   429
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   430
	if (_debug)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   431
		NSLog(@"APPIRATER Tracking version: %@", trackingVersion);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   432
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   433
	if ([trackingVersion isEqualToString:version])
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   434
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   435
		// check if the first use date has been set. if not, set it.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   436
		NSTimeInterval timeInterval = [userDefaults doubleForKey:kAppiraterFirstUseDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   437
		if (timeInterval == 0)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   438
		{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   439
			timeInterval = [[NSDate date] timeIntervalSince1970];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   440
			[userDefaults setDouble:timeInterval forKey:kAppiraterFirstUseDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   441
		}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   442
		
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   443
		// increment the significant event count
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   444
		NSInteger sigEventCount = [userDefaults integerForKey:kAppiraterSignificantEventCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   445
		sigEventCount++;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   446
		[userDefaults setInteger:sigEventCount forKey:kAppiraterSignificantEventCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   447
		if (_debug)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   448
			NSLog(@"APPIRATER Significant event count: %@", @(sigEventCount));
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   449
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   450
	else
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   451
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   452
		// it's a new version of the app, so restart tracking
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   453
		[userDefaults setObject:version forKey:kAppiraterCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   454
		[userDefaults setDouble:0 forKey:kAppiraterFirstUseDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   455
		[userDefaults setInteger:0 forKey:kAppiraterUseCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   456
		[userDefaults setInteger:1 forKey:kAppiraterSignificantEventCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   457
		[userDefaults setBool:NO forKey:kAppiraterRatedCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   458
		[userDefaults setBool:NO forKey:kAppiraterDeclinedToRate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   459
		[userDefaults setDouble:0 forKey:kAppiraterReminderRequestDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   460
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   461
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   462
	[userDefaults synchronize];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   463
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   464
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   465
- (void)incrementAndRate:(BOOL)canPromptForRating {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   466
	[self incrementUseCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   467
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   468
	if (canPromptForRating &&
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   469
        [self ratingConditionsHaveBeenMet] &&
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   470
        [self ratingAlertIsAppropriate])
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   471
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   472
        dispatch_async(dispatch_get_main_queue(),
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   473
                       ^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   474
                           [self showRatingAlert];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   475
                       });
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   476
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   477
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   478
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   479
- (void)incrementSignificantEventAndRate:(BOOL)canPromptForRating {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   480
	[self incrementSignificantEventCount];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   481
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   482
    if (canPromptForRating &&
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   483
        [self ratingConditionsHaveBeenMet] &&
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   484
        [self ratingAlertIsAppropriate])
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   485
	{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   486
        dispatch_async(dispatch_get_main_queue(),
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   487
                       ^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   488
                           [self showRatingAlert];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   489
                       });
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   490
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   491
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   492
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   493
- (BOOL)userHasDeclinedToRate {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   494
    return [[NSUserDefaults standardUserDefaults] boolForKey:kAppiraterDeclinedToRate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   495
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   496
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   497
- (BOOL)userHasRatedCurrentVersion {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   498
    return [[NSUserDefaults standardUserDefaults] boolForKey:kAppiraterRatedCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   499
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   500
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   501
#pragma GCC diagnostic push
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   502
#pragma GCC diagnostic ignored "-Wdeprecated-implementations"
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   503
+ (void)appLaunched {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   504
	[Appirater appLaunched:YES];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   505
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   506
#pragma GCC diagnostic pop
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   507
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   508
+ (void)appLaunched:(BOOL)canPromptForRating {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   509
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   510
                   ^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   511
                       Appirater *a = [Appirater sharedInstance];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   512
                       if (_debug) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   513
                           dispatch_async(dispatch_get_main_queue(),
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   514
                                          ^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   515
                                              [a showRatingAlert];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   516
                                          });
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   517
                       } else {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   518
                           [a incrementAndRate:canPromptForRating]; 
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   519
                       }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   520
                   });
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   521
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   522
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   523
- (void)hideRatingAlert {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   524
	if (self.ratingAlert.visible) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   525
		if (_debug)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   526
			NSLog(@"APPIRATER Hiding Alert");
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   527
		[self.ratingAlert dismissWithClickedButtonIndex:-1 animated:NO];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   528
	}	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   529
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   530
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   531
+ (void)appWillResignActive {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   532
	if (_debug)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   533
		NSLog(@"APPIRATER appWillResignActive");
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   534
	[[Appirater sharedInstance] hideRatingAlert];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   535
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   536
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   537
+ (void)appEnteredForeground:(BOOL)canPromptForRating {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   538
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   539
                   ^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   540
                       [[Appirater sharedInstance] incrementAndRate:canPromptForRating];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   541
                   });
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   542
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   543
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   544
+ (void)userDidSignificantEvent:(BOOL)canPromptForRating {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   545
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   546
                   ^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   547
                       [[Appirater sharedInstance] incrementSignificantEventAndRate:canPromptForRating];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   548
                   });
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   549
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   550
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   551
#pragma GCC diagnostic push
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   552
#pragma GCC diagnostic ignored "-Wdeprecated-implementations"
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   553
+ (void)showPrompt {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   554
  [Appirater tryToShowPrompt];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   555
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   556
#pragma GCC diagnostic pop
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   557
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   558
+ (void)tryToShowPrompt {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   559
  [[Appirater sharedInstance] showPromptWithChecks:true
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   560
                            displayRateLaterButton:true];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   561
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   562
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   563
+ (void)forceShowPrompt:(BOOL)displayRateLaterButton {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   564
  [[Appirater sharedInstance] showPromptWithChecks:false
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   565
                            displayRateLaterButton:displayRateLaterButton];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   566
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   567
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   568
- (void)showPromptWithChecks:(BOOL)withChecks
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   569
      displayRateLaterButton:(BOOL)displayRateLaterButton {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   570
  if (withChecks == NO || [self ratingAlertIsAppropriate]) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   571
    [self showRatingAlert:displayRateLaterButton];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   572
  }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   573
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   574
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   575
+ (id)getRootViewController {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   576
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   577
    if (window.windowLevel != UIWindowLevelNormal) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   578
        NSArray *windows = [[UIApplication sharedApplication] windows];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   579
        for(window in windows) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   580
            if (window.windowLevel == UIWindowLevelNormal) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   581
                break;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   582
            }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   583
        }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   584
    }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   585
    
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   586
    return [Appirater iterateSubViewsForViewController:window]; // iOS 8+ deep traverse
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   587
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   588
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   589
+ (id)iterateSubViewsForViewController:(UIView *) parentView {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   590
    for (UIView *subView in [parentView subviews]) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   591
        UIResponder *responder = [subView nextResponder];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   592
        if([responder isKindOfClass:[UIViewController class]]) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   593
            return [self topMostViewController: (UIViewController *) responder];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   594
        }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   595
        id found = [Appirater iterateSubViewsForViewController:subView];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   596
        if( nil != found) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   597
            return found;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   598
        }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   599
    }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   600
    return nil;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   601
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   602
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   603
+ (UIViewController *) topMostViewController: (UIViewController *) controller {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   604
	BOOL isPresenting = NO;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   605
	do {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   606
		// this path is called only on iOS 6+, so -presentedViewController is fine here.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   607
		UIViewController *presented = [controller presentedViewController];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   608
		isPresenting = presented != nil;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   609
		if(presented != nil) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   610
			controller = presented;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   611
		}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   612
		
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   613
	} while (isPresenting);
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   614
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   615
	return controller;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   616
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   617
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   618
+ (void)rateApp {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   619
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   620
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   621
	[userDefaults setBool:YES forKey:kAppiraterRatedCurrentVersion];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   622
	[userDefaults synchronize];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   623
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   624
	//Use the in-app StoreKit view if available (iOS 6) and imported. This works in the simulator.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   625
	if (![Appirater sharedInstance].openInAppStore && NSStringFromClass([SKStoreProductViewController class]) != nil) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   626
		
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   627
		SKStoreProductViewController *storeViewController = [[SKStoreProductViewController alloc] init];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   628
		NSNumber *appId = [NSNumber numberWithInteger:_appId.integerValue];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   629
		[storeViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier:appId} completionBlock:nil];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   630
		storeViewController.delegate = self.sharedInstance;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   631
        
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   632
        id <AppiraterDelegate> delegate = self.sharedInstance.delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   633
		if ([delegate respondsToSelector:@selector(appiraterWillPresentModalView:animated:)]) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   634
			[delegate appiraterWillPresentModalView:self.sharedInstance animated:_usesAnimation];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   635
		}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   636
		[[self getRootViewController] presentViewController:storeViewController animated:_usesAnimation completion:^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   637
			[self setModalOpen:YES];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   638
			//Temporarily use a black status bar to match the StoreKit view.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   639
			[self setStatusBarStyle:[UIApplication sharedApplication].statusBarStyle];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   640
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   641
			[[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:_usesAnimation];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   642
#endif
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   643
		}];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   644
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   645
	//Use the standard openUrl method if StoreKit is unavailable.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   646
	} else {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   647
		
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   648
		#if TARGET_IPHONE_SIMULATOR
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   649
		NSLog(@"APPIRATER NOTE: iTunes App Store is not supported on the iOS simulator. Unable to open App Store page.");
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   650
		#else
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   651
		NSString *reviewURL = [templateReviewURL stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", _appId]];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   652
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   653
		// iOS 7 needs a different templateReviewURL @see https://github.com/arashpayan/appirater/issues/131
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   654
        // Fixes condition @see https://github.com/arashpayan/appirater/issues/205
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   655
		if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 && [[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   656
			reviewURL = [templateReviewURLiOS7 stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", _appId]];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   657
		}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   658
        // iOS 8 needs a different templateReviewURL also @see https://github.com/arashpayan/appirater/issues/182
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   659
        else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   660
        {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   661
            reviewURL = [templateReviewURLiOS8 stringByReplacingOccurrencesOfString:@"APP_ID" withString:[NSString stringWithFormat:@"%@", _appId]];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   662
        }
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   663
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   664
		[[UIApplication sharedApplication] openURL:[NSURL URLWithString:reviewURL]];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   665
		#endif
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   666
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   667
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   668
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   669
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   670
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   671
    
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   672
    id <AppiraterDelegate> delegate = _delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   673
	
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   674
	switch (buttonIndex) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   675
		case 0:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   676
		{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   677
			// they don't want to rate it
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   678
			[userDefaults setBool:YES forKey:kAppiraterDeclinedToRate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   679
			[userDefaults synchronize];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   680
			if(delegate && [delegate respondsToSelector:@selector(appiraterDidDeclineToRate:)]){
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   681
				[delegate appiraterDidDeclineToRate:self];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   682
			}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   683
			break;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   684
		}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   685
		case 1:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   686
		{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   687
			// they want to rate it
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   688
			[Appirater rateApp];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   689
			if(delegate&& [delegate respondsToSelector:@selector(appiraterDidOptToRate:)]){
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   690
				[delegate appiraterDidOptToRate:self];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   691
			}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   692
			break;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   693
		}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   694
		case 2:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   695
			// remind them later
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   696
			[userDefaults setDouble:[[NSDate date] timeIntervalSince1970] forKey:kAppiraterReminderRequestDate];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   697
			[userDefaults synchronize];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   698
			if(delegate && [delegate respondsToSelector:@selector(appiraterDidOptToRemindLater:)]){
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   699
				[delegate appiraterDidOptToRemindLater:self];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   700
			}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   701
			break;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   702
		default:
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   703
			break;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   704
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   705
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   706
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   707
//Delegate call from the StoreKit view.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   708
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   709
	[Appirater closeModal];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   710
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   711
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   712
//Close the in-app rating (StoreKit) view and restore the previous status bar style.
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   713
+ (void)closeModal {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   714
	if (_modalOpen) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   715
		[[UIApplication sharedApplication]setStatusBarStyle:_statusBarStyle animated:_usesAnimation];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   716
		BOOL usedAnimation = _usesAnimation;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   717
		[self setModalOpen:NO];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   718
		
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   719
		// get the top most controller (= the StoreKit Controller) and dismiss it
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   720
		UIViewController *presentingController = [UIApplication sharedApplication].keyWindow.rootViewController;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   721
		presentingController = [self topMostViewController: presentingController];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   722
		[presentingController dismissViewControllerAnimated:_usesAnimation completion:^{
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   723
            id <AppiraterDelegate> delegate = self.sharedInstance.delegate;
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   724
			if ([delegate respondsToSelector:@selector(appiraterDidDismissModalView:animated:)]) {
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   725
				[delegate appiraterDidDismissModalView:(Appirater *)self animated:usedAnimation];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   726
			}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   727
		}];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   728
		[self.class setStatusBarStyle:(UIStatusBarStyle)nil];
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   729
	}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   730
}
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   731
402baa6d4f64 - Appirater:
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   732
@end