Skip to content

Commit 0c305fc

Browse files
committed
support set delay for upgrade notification
1 parent 9aca92c commit 0c305fc

File tree

5 files changed

+83
-39
lines changed

5 files changed

+83
-39
lines changed

AWVersionAgent.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Pod::Spec.new do |s|
22
s.name = "AWVersionAgent"
3-
s.version = "0.0.3"
3+
s.version = "0.0.4"
44
s.summary = "Check and notify user that new version is available from Local Notification."
55
s.homepage = "https://github.com/appwilldev/AWVersionAgent"
66
s.license = { :type => 'MIT', :file => 'LICENSE' }
77
s.author = { "Heyward Fann" => "[email protected]" }
8-
s.source = { :git => "https://github.com/appwilldev/AWVersionAgent.git", :tag => "0.0.3" }
8+
s.source = { :git => "https://github.com/appwilldev/AWVersionAgent.git", :tag => s.version.to_s }
99
s.platform = :ios
1010
s.source_files = 'AWVersionAgent/*.{h,m}'
1111
s.requires_arc = true

AWVersionAgent/AWVersionAgent.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313
+ (AWVersionAgent *)sharedAgent;
1414

1515
@property (nonatomic) BOOL debug;
16+
@property (nonatomic, assign) NSTimeInterval delay;
17+
@property (nonatomic, strong) NSString *actionText;
18+
@property (nonatomic, strong) NSString *alertMessage;
1619

1720
- (void)checkNewVersionForApp:(NSString *)appid;
21+
22+
- (BOOL)isNewVersion;
23+
1824
- (void)upgradeAppWithNotification:(UILocalNotification *)notification;
1925

2026
@end

AWVersionAgent/AWVersionAgent.m

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,16 @@
99
#import "AWVersionAgent.h"
1010

1111
#define kAppleLookupURLTemplate @"http://itunes.apple.com/lookup?id=%@"
12-
#define kAppStoreURLTemplate @"itms-apps://itunes.apple.com/app/id%@"
12+
#define kAppStoreURLTemplate @"https://itunes.apple.com/app/id%@"
1313

14-
#define kUpgradeAlertMessage @"A new version is available, current version: %@, new version: %@. Upgrade from the App Store now."
15-
#define kUpgradeAlertAction @"kUpgradeAlertAction"
16-
#define kUpgradeAlertDelay 3
14+
#define kUpgradeAlertMessage NSLocalizedString(@"A new version is available, current version: %@, new version: %@. Upgrade from the App Store now.", nil)
15+
#define kUpgradeAlertAction NSLocalizedString(@"upgrade", nil)
1716

1817
#define kAWVersionAgentLastNotificationDateKey @"lastNotificationDate"
1918
#define kAWVersionAgentLastCheckVersionDateKey @"lastCheckVersionDate"
2019

2120
@interface AWVersionAgent ()
22-
23-
@property (nonatomic, copy) NSString *appid;
21+
@property (nonatomic) NSString *appID;
2422
@property (nonatomic) BOOL newVersionAvailable;
2523

2624
@end
@@ -49,7 +47,12 @@ - (id)init
4947
if (self) {
5048
_newVersionAvailable = NO;
5149
_debug = NO;
52-
50+
_delay = 3;
51+
52+
if ([self.actionText length] == 0) {
53+
self.actionText = kUpgradeAlertAction;
54+
}
55+
5356
[[NSNotificationCenter defaultCenter] addObserver:self
5457
selector:@selector(showUpgradeNotification)
5558
name:UIApplicationDidEnterBackgroundNotification
@@ -59,11 +62,33 @@ - (id)init
5962
return self;
6063
}
6164

65+
- (NSString *)appID
66+
{
67+
return [[NSUserDefaults standardUserDefaults] objectForKey:@"AWVersionAgentAppID"];
68+
}
69+
70+
- (void)setAppID:(NSString *)appID
71+
{
72+
[[NSUserDefaults standardUserDefaults] setObject:appID forKey:@"AWVersionAgentAppID"];
73+
[[NSUserDefaults standardUserDefaults] synchronize];
74+
}
75+
76+
- (NSString *)actionText
77+
{
78+
return [[NSUserDefaults standardUserDefaults] objectForKey:@"AWVersionAgentActionText"];
79+
}
80+
81+
- (void)setActionText:(NSString *)actionText
82+
{
83+
[[NSUserDefaults standardUserDefaults] setObject:actionText forKey:@"AWVersionAgentActionText"];
84+
[[NSUserDefaults standardUserDefaults] synchronize];
85+
}
86+
6287
- (void)checkNewVersionForApp:(NSString *)appid
6388
{
64-
self.appid = appid;
89+
self.appID = appid;
6590
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
66-
NSString *url = [NSString stringWithFormat:kAppleLookupURLTemplate, _appid];
91+
NSString *url = [NSString stringWithFormat:kAppleLookupURLTemplate, self.appID];
6792
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
6893
if (data && [data length]>0) {
6994
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
@@ -112,14 +137,14 @@ - (void)showUpgradeNotification
112137
{
113138
if ([self conditionHasBeenMet]) {
114139
UILocalNotification *notification = [[UILocalNotification alloc] init];
115-
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:kUpgradeAlertDelay];
140+
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:self.delay];
116141
notification.timeZone = [NSTimeZone defaultTimeZone];
117142
NSString *curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
118143
NSString *newVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"kAppNewVersion"];
119144
NSString *msg = [NSString stringWithFormat:kUpgradeAlertMessage,
120145
curVersion, newVersion];
121146
notification.alertBody = msg;
122-
notification.alertAction = kUpgradeAlertAction;
147+
notification.alertAction = self.actionText;
123148
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
124149

125150
[[NSUserDefaults standardUserDefaults] setDouble:[[NSDate date] timeIntervalSince1970]
@@ -130,14 +155,25 @@ - (void)showUpgradeNotification
130155

131156
- (void)upgradeAppWithNotification:(UILocalNotification *)notification
132157
{
133-
if ([notification.alertAction isEqualToString:kUpgradeAlertAction]) {
158+
if ([notification.alertAction isEqualToString:self.actionText]) {
134159
[[UIApplication sharedApplication] cancelLocalNotification:notification];
135160

136-
NSString *url = [NSString stringWithFormat:kAppStoreURLTemplate, _appid];
161+
NSString *url = [NSString stringWithFormat:kAppStoreURLTemplate, self.appID];
137162
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
138163

139164
self.newVersionAvailable = NO;
140165
}
141166
}
142167

168+
- (BOOL)isNewVersion
169+
{
170+
NSString* appNewVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"kAppNewVersion"];
171+
NSString* curVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
172+
if (appNewVersion && curVersion && ![appNewVersion isEqualToString:curVersion]) {
173+
return YES;
174+
} else {
175+
return NO;
176+
}
177+
}
178+
143179
@end

AWVersionAgentDemo/AWVersionAgentDemo.xcodeproj/project.pbxproj

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
04DED3EF195ABB41004D6CFD /* AWVersionAgent.m in Sources */ = {isa = PBXBuildFile; fileRef = 04DED3EE195ABB41004D6CFD /* AWVersionAgent.m */; };
1011
571B2E7F16E5D4F900E69036 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 571B2E7E16E5D4F900E69036 /* UIKit.framework */; };
1112
571B2E8116E5D4F900E69036 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 571B2E8016E5D4F900E69036 /* Foundation.framework */; };
1213
571B2E8316E5D4F900E69036 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 571B2E8216E5D4F900E69036 /* CoreGraphics.framework */; };
@@ -18,10 +19,11 @@
1819
571B2E9516E5D4F900E69036 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 571B2E9416E5D4F900E69036 /* [email protected] */; };
1920
571B2E9816E5D4F900E69036 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 571B2E9716E5D4F900E69036 /* ViewController.m */; };
2021
571B2E9B16E5D4F900E69036 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 571B2E9916E5D4F900E69036 /* ViewController.xib */; };
21-
571B2EAD16E5D83000E69036 /* AWVersionAgent.m in Sources */ = {isa = PBXBuildFile; fileRef = 571B2EAC16E5D83000E69036 /* AWVersionAgent.m */; };
2222
/* End PBXBuildFile section */
2323

2424
/* Begin PBXFileReference section */
25+
04DED3ED195ABB41004D6CFD /* AWVersionAgent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AWVersionAgent.h; sourceTree = "<group>"; };
26+
04DED3EE195ABB41004D6CFD /* AWVersionAgent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AWVersionAgent.m; sourceTree = "<group>"; };
2527
571B2E7B16E5D4F900E69036 /* AWVersionAgentDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AWVersionAgentDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
2628
571B2E7E16E5D4F900E69036 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
2729
571B2E8016E5D4F900E69036 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -38,8 +40,6 @@
3840
571B2E9616E5D4F900E69036 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
3941
571B2E9716E5D4F900E69036 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
4042
571B2E9A16E5D4F900E69036 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = "<group>"; };
41-
571B2EAB16E5D83000E69036 /* AWVersionAgent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AWVersionAgent.h; sourceTree = "<group>"; };
42-
571B2EAC16E5D83000E69036 /* AWVersionAgent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AWVersionAgent.m; sourceTree = "<group>"; };
4343
/* End PBXFileReference section */
4444

4545
/* Begin PBXFrameworksBuildPhase section */
@@ -56,6 +56,16 @@
5656
/* End PBXFrameworksBuildPhase section */
5757

5858
/* Begin PBXGroup section */
59+
04DED3EC195ABB41004D6CFD /* AWVersionAgent */ = {
60+
isa = PBXGroup;
61+
children = (
62+
04DED3ED195ABB41004D6CFD /* AWVersionAgent.h */,
63+
04DED3EE195ABB41004D6CFD /* AWVersionAgent.m */,
64+
);
65+
name = AWVersionAgent;
66+
path = ../../AWVersionAgent;
67+
sourceTree = "<group>";
68+
};
5969
571B2E7216E5D4F900E69036 = {
6070
isa = PBXGroup;
6171
children = (
@@ -86,7 +96,7 @@
8696
571B2E8416E5D4F900E69036 /* AWVersionAgentDemo */ = {
8797
isa = PBXGroup;
8898
children = (
89-
571B2EA116E5D53900E69036 /* Vendor */,
99+
04DED3EC195ABB41004D6CFD /* AWVersionAgent */,
90100
571B2E8D16E5D4F900E69036 /* AppDelegate.h */,
91101
571B2E8E16E5D4F900E69036 /* AppDelegate.m */,
92102
571B2E9616E5D4F900E69036 /* ViewController.h */,
@@ -111,24 +121,6 @@
111121
name = "Supporting Files";
112122
sourceTree = "<group>";
113123
};
114-
571B2EA116E5D53900E69036 /* Vendor */ = {
115-
isa = PBXGroup;
116-
children = (
117-
571B2EAA16E5D83000E69036 /* AWVersionAgent */,
118-
);
119-
path = Vendor;
120-
sourceTree = "<group>";
121-
};
122-
571B2EAA16E5D83000E69036 /* AWVersionAgent */ = {
123-
isa = PBXGroup;
124-
children = (
125-
571B2EAB16E5D83000E69036 /* AWVersionAgent.h */,
126-
571B2EAC16E5D83000E69036 /* AWVersionAgent.m */,
127-
);
128-
name = AWVersionAgent;
129-
path = ../../../AWVersionAgent;
130-
sourceTree = "<group>";
131-
};
132124
/* End PBXGroup section */
133125

134126
/* Begin PBXNativeTarget section */
@@ -195,10 +187,10 @@
195187
isa = PBXSourcesBuildPhase;
196188
buildActionMask = 2147483647;
197189
files = (
190+
04DED3EF195ABB41004D6CFD /* AWVersionAgent.m in Sources */,
198191
571B2E8B16E5D4F900E69036 /* main.m in Sources */,
199192
571B2E8F16E5D4F900E69036 /* AppDelegate.m in Sources */,
200193
571B2E9816E5D4F900E69036 /* ViewController.m in Sources */,
201-
571B2EAD16E5D83000E69036 /* AWVersionAgent.m in Sources */,
202194
);
203195
runOnlyForDeploymentPostprocessing = 0;
204196
};
@@ -283,21 +275,25 @@
283275
571B2E9F16E5D4F900E69036 /* Debug */ = {
284276
isa = XCBuildConfiguration;
285277
buildSettings = {
278+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
286279
GCC_PRECOMPILE_PREFIX_HEADER = YES;
287280
GCC_PREFIX_HEADER = "AWVersionAgentDemo/AWVersionAgentDemo-Prefix.pch";
288281
INFOPLIST_FILE = "AWVersionAgentDemo/AWVersionAgentDemo-Info.plist";
289282
PRODUCT_NAME = "$(TARGET_NAME)";
283+
PROVISIONING_PROFILE = "D98F8A0C-9BCF-4CCB-8D43-E1C482A98282";
290284
WRAPPER_EXTENSION = app;
291285
};
292286
name = Debug;
293287
};
294288
571B2EA016E5D4F900E69036 /* Release */ = {
295289
isa = XCBuildConfiguration;
296290
buildSettings = {
291+
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
297292
GCC_PRECOMPILE_PREFIX_HEADER = YES;
298293
GCC_PREFIX_HEADER = "AWVersionAgentDemo/AWVersionAgentDemo-Prefix.pch";
299294
INFOPLIST_FILE = "AWVersionAgentDemo/AWVersionAgentDemo-Info.plist";
300295
PRODUCT_NAME = "$(TARGET_NAME)";
296+
PROVISIONING_PROFILE = "D98F8A0C-9BCF-4CCB-8D43-E1C482A98282";
301297
WRAPPER_EXTENSION = app;
302298
};
303299
name = Release;

AWVersionAgentDemo/AWVersionAgentDemo/AppDelegate.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,24 @@ @implementation AppDelegate
1717
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1818
{
1919
[[AWVersionAgent sharedAgent] setDebug:YES];
20+
[AWVersionAgent sharedAgent].actionText = @"test";
2021
[[AWVersionAgent sharedAgent] checkNewVersionForApp:@"453718989"];
2122

2223
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
2324
// Override point for customization after application launch.
2425
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
2526
self.window.rootViewController = self.viewController;
2627
[self.window makeKeyAndVisible];
27-
return YES;
28+
NSLog(@"------------launchOptions:%@",launchOptions);
29+
if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
30+
[self application:application didReceiveLocalNotification:launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]];
31+
}
32+
return NO;
2833
}
2934

3035
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
3136
{
37+
NSLog(@"------------notification:%@",notification);
3238
[[AWVersionAgent sharedAgent] upgradeAppWithNotification:notification];
3339
}
3440

0 commit comments

Comments
 (0)