2013-04-22 54 views
0

在我的應用程序中,我在我的第一個視圖控制器上安排了本地通知。它工作正常。 要設置「查看」按鈕的動作我的代碼如下:iPhone SDK中的本地通知設置操作

//AppDelegate.m 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
UILocalNotification *localNotif = 
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; 
    if (localNotif) 
    { 
// did something Here when app launch by View button of notification 
// added row in Db 
} 
    return YES; 
} 

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{ 

    // Handle the notificaton when the app is running 
    NSLog(@"Recieved Notification %@",notif); 

    app.applicationIconBadgeNumber=notif.applicationIconBadgeNumber++; 
} 

問題:它工作正常的只有第一次。

但第二次,該通知顯示,但該行沒有在我的數據庫添加

//編輯

我想給一些行動時,用戶按下按鈕「視圖」。

如何識別該操作?

編輯

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif 
{ 


    // Handle the notificaton when the app is running 
    NSLog(@"Recieved Notification %@",notif); 

    app.applicationIconBadgeNumber=notif.applicationIconBadgeNumber++; 

    UIApplicationState state = [app applicationState]; 
    if (state == UIApplicationStateInactive) { 
     // Application was in the background when notification 
     // was delivered. 

     [self localNotif]; 

    } 


} 

- (void) localNotif 
{ 

    [self databaseOpen]; 
    NSString *maxIdPt = [NSString stringWithFormat:@"select Max(id) from Points"]; 
    NSLog(@"Max id from points table %@",maxIdPt); 
    NSMutableArray *p1 = [[NSMutableArray alloc]init]; 
    p1 = [[[database executeQuery:maxIdPt]mutableCopy]retain]; 


    int p=[[[p1 objectAtIndex:0]valueForKey:@"Max(id)"]intValue]; 
    NSLog(@"%d",p); 


    NSString *maxPt = [NSString stringWithFormat:@"select Points from Points where id = %d",p]; 
    NSLog(@"Maxp %@",maxPt); 
    NSMutableArray * p2 = [[NSMutableArray alloc]init]; 
    p2 =[[[database executeQuery:maxPt]mutableCopy]retain]; 

    int k = [[[p2 objectAtIndex:0]valueForKey:@"Points"]intValue]; 
    NSLog(@"Points %d",k); 

    k = k + 250; 

    NSLog(@"%d",k); 


    NSString *mtpoints = [NSString stringWithFormat:@"insert into Points(Points) values (%d)",k]; 
    NSLog(@" lbl Points are %@",mtpoints); 
    NSMutableArray * m1 = [[NSMutableArray alloc]init]; 
    m1 = [[[database executeQuery:mtpoints]mutableCopy]retain]; 
    [database close]; 


} 

相同的方法i的應用didFinishLaunchingWithOptions

第一次得分被更新,但第二次它不被調用。即使通知沒有顯示。

+0

這聽起來更像是你的數據庫代碼的問題,可能更好地發佈它,而不是你的通知代碼。 – 2013-04-22 13:59:43

回答

2

當用戶按下通知中的按鈕時,可能第二次調用didFinishLaunchingWithOptions函數,因爲該應用程序已在後臺運行(與從非運行狀態啓動相反)。

你還必須處理函數調用

(void)application:(UIApplication *)application 
     didReceiveLocalNotification:(UILocalNotification *)notification { 
// do your db update here, too ... 
} 

,以涵蓋您的應用程序已經在後臺運行的情況。

編輯:

要查看應用程序是否已啓動或已在運行(=用戶按下上通知與應用程序在前臺運行時,「查看」),檢查應用程序狀態:

UIApplicationState state = [application applicationState]; 
    if (state == UIApplicationStateInactive) { 
    // Application was in the background when notification 
    // was delivered. 
    } 

致謝Use Your Loaf - Adding Local Notifications With iOS 4

+0

是的,你說得對,但有什麼方法可以在用戶點擊按鈕「查看」時識別? – Manthan 2013-04-23 09:19:45

+0

在我的情況是發生了什麼是通知顯示,但是當我按下「OKAY」按鈕。它打開了應用程序,這很好,但沒有更新在我的數據庫。 – Manthan 2013-04-23 09:26:27

+0

您是否嘗試過didReceiveLocalNotification函數中的檢查,並在您的數據庫未更新時進行更新?如果這不起作用,你必須顯示你的數據庫更新代碼。 – TheEye 2013-04-23 09:40:03

0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 


     sk = [[SKDatabase alloc] init]; 
     NSString *db = @"MindEditor.db"; 
     [sk initWithDynamicFile:db]; 
     [email protected]"0"; 
    } 


    SKDatabase.h 


// 
// SKDatabase.h 
// Version 1.1 
// 
// Created by Shannon Appelcline on 9/11/08. 
// Copyright 2008 __MyCompanyName__. All rights reserved. 
// 

#import <UIKit/UIKit.h> 
#import <sqlite3.h> 

@protocol SKDatabaseDelegate <NSObject> 
@optional 
- (void)databaseTableWasUpdated:(NSString *)table; 
@end 

@interface SKDatabase : NSObject { 

    id<SKDatabaseDelegate> delegate; 
    sqlite3 *dbh; 
    BOOL dynamic; 
} 

@property (assign) id<SKDatabaseDelegate> delegate; 
@property sqlite3 *dbh; 
@property BOOL dynamic; 

- (id)initWithFile:(NSString *)dbFile; 
- (id)initWithDynamicFile:(NSString *)dbFile; 
- (void)close; 

- (sqlite3_stmt *)prepare:(NSString *)sql; 

- (id)lookupColForSQL:(NSString *)sql; 
- (NSDictionary *)lookupRowForSQL:(NSString *)sql; 
- (NSArray *)lookupAllForSQL:(NSString *)sql; 

- (int)lookupCountWhere:(NSString *)where forTable:(NSString *)table; 
- (int)lookupMax:(NSString *)key Where:(NSString *)where forTable:(NSString *)table; 
- (int)lookupSum:(NSString *)key Where:(NSString *)where forTable:(NSString *)table; 

- (void)insertArray:(NSArray *)dbData forTable:(NSString *)table; 
- (void)insertDictionary:(NSDictionary *)dbData forTable:(NSString *)table; 

- (void)updateArray:(NSArray *)dbData forTable:(NSString *)table; 
- (void)updateArray:(NSArray *)dbData forTable:(NSString *)table where:(NSString *)where; 
- (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table; 
- (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table where:(NSString *)where; 
- (void)updateSQL:(NSString *)sql forTable:(NSString *)table; 

- (void)deleteWhere:(NSString *)where forTable:(NSString *)table; 

- (BOOL)runDynamicSQL:(NSString *)sql forTable:(NSString *)table; 

@end 

    SKDatabase.m 




// 
// SKDatabase.m 
// Version 1.1 
// 
// Created by Shannon Appelcline on 9/11/08. 
// Copyright 2008 __MyCompanyName__. All rights reserved. 
// 

#import "SKDatabase.h" 

@implementation SKDatabase 

@synthesize delegate; 
@synthesize dbh; 
@synthesize dynamic; 

// Two ways to init: one if you're just SELECTing from a database, one if you're UPDATing 
// and or INSERTing 

- (id)initWithFile:(NSString *)dbFile { 
    if (self = [super init]) { 

     NSString *paths = [[NSBundle mainBundle] resourcePath]; 
     NSString *path = [paths stringByAppendingPathComponent:dbFile]; 

     int result = sqlite3_open([path UTF8String], &dbh); 
     NSAssert1(SQLITE_OK == result, NSLocalizedStringFromTable(@"Unable to open the sqlite database (%@).", @"Database", @""), [NSString stringWithUTF8String:sqlite3_errmsg(dbh)]); 
     self.dynamic = NO; 
    } 

    return self;  
} 

- (id)initWithDynamicFile:(NSString *)dbFile { 
    if (self = [super init]) { 

     NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *docDir = [docPaths objectAtIndex:0]; 
     NSString *docPath = [docDir stringByAppendingPathComponent:dbFile]; 

     NSFileManager *fileManager = [NSFileManager defaultManager]; 

     if (![fileManager fileExistsAtPath:docPath]) { 

      NSString *origPaths = [[NSBundle mainBundle] resourcePath]; 
      NSString *origPath = [origPaths stringByAppendingPathComponent:dbFile]; 

      NSError *error; 
      int success = [fileManager copyItemAtPath:origPath toPath:docPath error:&error];    
      NSAssert1(success,[NSString stringWithString:@"Failed to copy database into dynamic location"],error); 
     } 
     int result = sqlite3_open([docPath UTF8String], &dbh); 
     NSAssert1(SQLITE_OK == result, NSLocalizedStringFromTable(@"Unable to open the sqlite database (%@).", @"Database", @""), [NSString stringWithUTF8String:sqlite3_errmsg(dbh)]); 
     self.dynamic = YES; 
    } 

    return self;  
} 

// Users should never need to call prepare 

- (sqlite3_stmt *)prepare:(NSString *)sql { 

    const char *utfsql = [sql UTF8String]; 

    sqlite3_stmt *statement; 

    if (sqlite3_prepare([self dbh],utfsql,-1,&statement,NULL) == SQLITE_OK) { 
     return statement; 
    } else { 
     return 0; 
    } 
} 

// Three ways to lookup results: for a variable number of responses, for a full row 
// of responses, or for a singular bit of data 

- (NSArray *)lookupAllForSQL:(NSString *)sql { 
    sqlite3_stmt *statement; 
    id result; 
    NSMutableArray *thisArray = [NSMutableArray arrayWithCapacity:4]; 
    if (statement = [self prepare:sql]) { 
     while (sqlite3_step(statement) == SQLITE_ROW) { 
      NSMutableDictionary *thisDict = [NSMutableDictionary dictionaryWithCapacity:4]; 
      for (int i = 0 ; i < sqlite3_column_count(statement) ; i++) 
      { 
       if (sqlite3_column_decltype(statement,i) != NULL && 
        strcasecmp(sqlite3_column_decltype(statement,i),"Boolean") == 0) 
       { 
        result = [NSNumber numberWithBool:(BOOL)sqlite3_column_int(statement,i)]; 
       } 
       else if (sqlite3_column_type(statement, i) == SQLITE_TEXT) 
       { 
        result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
       } 
       else if 
        (sqlite3_column_type(statement,i) == SQLITE_INTEGER) 
       { 
        result = [NSNumber numberWithInt:(int)sqlite3_column_int(statement,i)]; 
       } 
       else if (sqlite3_column_type(statement,i) == SQLITE_FLOAT) 
       { 
        result = [NSNumber numberWithFloat:(float)sqlite3_column_double(statement,i)];     
       } 
       else 
       { 
        result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
       } 
       if (result) 
       { 
        [thisDict setObject:result 
           forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement,i)]]; 
       } 
      } 
      [thisArray addObject:[NSDictionary dictionaryWithDictionary:thisDict]]; 
      [thisArray retain]; 
     } 
    } 
    sqlite3_finalize(statement); 
    return thisArray; 
} 

- (NSDictionary *)lookupRowForSQL:(NSString *)sql { 
    sqlite3_stmt *statement; 
    id result; 
    NSMutableDictionary *thisDict = [NSMutableDictionary dictionaryWithCapacity:4]; 
    if (statement = [self prepare:sql]) 
    { 
     if (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      for (int i = 0 ; i < sqlite3_column_count(statement) ; i++) 
      { 
       if (strcasecmp(sqlite3_column_decltype(statement,i),"Boolean") == 0) 
       { 
        result = [NSNumber numberWithBool:(BOOL)sqlite3_column_int(statement,i)]; 
       } 
       else if (sqlite3_column_type(statement, i) == SQLITE_TEXT) 
       { 
        result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
       } 
       else if (sqlite3_column_type(statement,i) == SQLITE_INTEGER) 
       { 
        result = [NSNumber numberWithInt:(int)sqlite3_column_int(statement,i)]; 
       } 
       else if (sqlite3_column_type(statement,i) == SQLITE_FLOAT) 
       { 
        result = [NSNumber numberWithFloat:(float)sqlite3_column_double(statement,i)];     
       } 
       else 
       { 
        result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,i)]; 
       } 
       if (result) 
       { 
        [thisDict setObject:result 
           forKey:[NSString stringWithUTF8String:sqlite3_column_name(statement,i)]]; 
       } 
      } 
     } 
    } 
    sqlite3_finalize(statement); 
    return thisDict; 
} 

- (id)lookupColForSQL:(NSString *)sql { 

    sqlite3_stmt *statement; 
    id result; 
    if (statement = [self prepare:sql]) { 
     if (sqlite3_step(statement) == SQLITE_ROW) {   
      if (strcasecmp(sqlite3_column_decltype(statement,0),"Boolean") == 0) { 
       result = [NSNumber numberWithBool:(BOOL)sqlite3_column_int(statement,0)]; 
      } else if (sqlite3_column_type(statement, 0) == SQLITE_TEXT) { 
       result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]; 
      } else if (sqlite3_column_type(statement,0) == SQLITE_INTEGER) { 
       result = [NSNumber numberWithInt:(int)sqlite3_column_int(statement,0)]; 
      } else if (sqlite3_column_type(statement,0) == SQLITE_FLOAT) { 
       result = [NSNumber numberWithDouble:(double)sqlite3_column_double(statement,0)];      
      } else { 
       result = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)]; 
      } 
     } 
    } 
    sqlite3_finalize(statement); 
    return result; 

} 

// Simple use of COUNTS, MAX, etc. 

- (int)lookupCountWhere:(NSString *)where forTable:(NSString *)table { 

    int tableCount = 0; 
    NSString *sql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ WHERE %@", 
        table,where];  
    sqlite3_stmt *statement; 

    if (statement = [self prepare:sql]) { 
     if (sqlite3_step(statement) == SQLITE_ROW) {   
      tableCount = sqlite3_column_int(statement,0); 
     } 
    } 
    sqlite3_finalize(statement); 
    return tableCount; 

} 

- (int)lookupMax:(NSString *)key Where:(NSString *)where forTable:(NSString *)table { 

    int tableMax = 0; 
    NSString *sql = [NSString stringWithFormat:@"SELECT MAX(%@) FROM %@ WHERE %@", 
        key,table,where];  
    sqlite3_stmt *statement; 
    if (statement = [self prepare:sql]) { 
     if (sqlite3_step(statement) == SQLITE_ROW) {   
      tableMax = sqlite3_column_int(statement,0); 
     } 
    } 
    sqlite3_finalize(statement); 
    return tableMax; 

} 

- (int)lookupSum:(NSString *)key Where:(NSString *)where forTable:(NSString *)table { 

    int tableSum = 0; 
    NSString *sql = [NSString stringWithFormat:@"SELECT SUM(%@) FROM %@ WHERE %@", 
        key,table,where];  
    sqlite3_stmt *statement; 
    if (statement = [self prepare:sql]) { 
     if (sqlite3_step(statement) == SQLITE_ROW) {   
      tableSum = sqlite3_column_int(statement,0); 
     } 
    } 
    sqlite3_finalize(statement); 
    return tableSum; 

} 

// INSERTing and UPDATing 

- (void)insertArray:(NSArray *)dbData forTable:(NSString *)table { 

// NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
// [sql appendFormat:@"INSERT INTO %@ (",table]; 
// 
// 
// for (int i = 0 ; i < [dbData count] ; i++) { 
//  NSLog(@"%@",[[dbData objectAtIndex:i] objectForKey:@"mid"]); 
//  [sql appendFormat:@"%@",[[dbData objectAtIndex:i] objectForKey:@"key"]]; 
//  if (i + 1 < [dbData count]) { 
//   [sql appendFormat:@", "]; 
//  } 
// } 
// [sql appendFormat:@") VALUES("]; 
// for (int i = 0 ; i < [dbData count] ; i++) { 
//  if ([[[dbData objectAtIndex:i] objectForKey:@"value"] intValue]) { 
//   [sql appendFormat:@"%@",[[[dbData objectAtIndex:i] objectForKey:@"value"] intValue]]; 
//  } else { 
//   [sql appendFormat:@"'%@'",[[dbData objectAtIndex:i] objectForKey:@"value"]]; 
//  } 
//  if (i + 1 < [dbData count]) { 
//   [sql appendFormat:@", "]; 
//  } 
// } 
// [sql appendFormat:@")"]; 
// [self runDynamicSQL:sql forTable:table]; 
    for(int i=0;i<[dbData count];i++) 
    { 
     NSDictionary *dict=[dbData objectAtIndex:i]; 
     NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
     [sql appendFormat:@"INSERT INTO %@ (",table]; 

     NSArray *dataKeys = [dict allKeys]; 
     for (int i = 0 ; i < [dataKeys count] ; i++) { 
      [sql appendFormat:@"%@",[dataKeys objectAtIndex:i]]; 
      if (i + 1 < [dataKeys count]) { 
       [sql appendFormat:@", "]; 
      } 
     } 

     [sql appendFormat:@") VALUES("]; 
     for (int i = 0 ; i < [dataKeys count] ; i++) { 
      if ([[dict objectForKey:[dataKeys objectAtIndex:i]] intValue]) { 
       [sql appendFormat:@"%@",[dict objectForKey:[dataKeys objectAtIndex:i]]]; 
      } else { 
       [sql appendFormat:@"'%@'",[dict objectForKey:[dataKeys objectAtIndex:i]]]; 
      } 
      if (i + 1 < [dict count]) { 
       [sql appendFormat:@", "]; 
      } 
     } 

     [sql appendFormat:@")"]; 
     [self runDynamicSQL:sql forTable:table]; 
    } 
} 

- (void)insertDictionary:(NSDictionary *)dbData forTable:(NSString *)table { 

    NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
    [sql appendFormat:@"INSERT INTO %@ (",table]; 

    NSArray *dataKeys = [dbData allKeys]; 
    for (int i = 0 ; i < [dataKeys count] ; i++) { 
     [sql appendFormat:@"%@",[dataKeys objectAtIndex:i]]; 
     if (i + 1 < [dbData count]) { 
      [sql appendFormat:@", "]; 
     } 
    } 

    [sql appendFormat:@") VALUES("]; 
    for (int i = 0 ; i < [dataKeys count] ; i++) { 
     //if ([[dbData objectForKey:[dataKeys objectAtIndex:i]] intValue]) { 
//   [sql appendFormat:@"%@",[dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
//  } else { 

     [sql appendFormat:@"'%@'",[dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
     //} 
     if (i + 1 < [dbData count]) { 
      [sql appendFormat:@", "]; 
     } 
    } 

    [sql appendFormat:@")"]; 
    [self runDynamicSQL:sql forTable:table]; 
} 

- (void)updateArray:(NSArray *)dbData forTable:(NSString *)table { 
    [self updateArray:dbData forTable:table where:NULL]; 
} 

- (void)updateArray:(NSArray *)dbData forTable:(NSString *)table where:(NSString *)where { 

    NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
    [sql appendFormat:@"UPDATE %@ SET ",table]; 

    for (int i = 0 ; i < [dbData count] ; i++) { 
     if ([[[dbData objectAtIndex:i] objectForKey:@"value"] intValue]) { 
      [sql appendFormat:@"%@=%@", 
      [[dbData objectAtIndex:i] objectForKey:@"key"], 
      [[dbData objectAtIndex:i] objectForKey:@"value"]]; 
     } else { 
      [sql appendFormat:@"%@='%@'", 
      [[dbData objectAtIndex:i] objectForKey:@"key"], 
      [[dbData objectAtIndex:i] objectForKey:@"value"]]; 
     }  
     if (i + 1 < [dbData count]) { 
      [sql appendFormat:@", "]; 
     } 
    } 
    if (where != NULL) { 
     [sql appendFormat:@" WHERE %@",where]; 
    } else { 
     [sql appendFormat:@" WHERE 1",where]; 
    }  
    [self runDynamicSQL:sql forTable:table]; 
} 

- (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table { 
    [self updateDictionary:dbData forTable:table where:NULL]; 
} 

- (void)updateDictionary:(NSDictionary *)dbData forTable:(NSString *)table where:(NSString *)where { 

    NSMutableString *sql = [NSMutableString stringWithCapacity:16]; 
    [sql appendFormat:@"UPDATE %@ SET ",table]; 

    NSArray *dataKeys = [dbData allKeys]; 
    for (int i = 0 ; i < [dataKeys count] ; i++) { 
     if ([[dbData objectForKey:[dataKeys objectAtIndex:i]] intValue]) { 
      [sql appendFormat:@"%@=%@", 
      [dataKeys objectAtIndex:i], 
      [dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
     } else { 
      [sql appendFormat:@"%@='%@'", 
      [dataKeys objectAtIndex:i], 
      [dbData objectForKey:[dataKeys objectAtIndex:i]]]; 
     }  
     if (i + 1 < [dbData count]) { 
      [sql appendFormat:@", "]; 
     } 
    } 
    if (where != NULL) { 
     [sql appendFormat:@" WHERE %@",where]; 
    } 
    [self runDynamicSQL:sql forTable:table]; 
} 

- (void)updateSQL:(NSString *)sql forTable:(NSString *)table { 
    [self runDynamicSQL:sql forTable:table]; 
} 

- (void)deleteWhere:(NSString *)where forTable:(NSString *)table { 

    NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@", 
        table,where]; 
    [self runDynamicSQL:sql forTable:table]; 
} 

// INSERT/UPDATE/DELETE Subroutines 

- (BOOL)runDynamicSQL:(NSString *)sql forTable:(NSString *)table { 

    int result; 
    //NSAssert1(self.dynamic == 1,[NSString stringWithString:@"Tried to use a dynamic function on a static database"],NULL); 
    sqlite3_stmt *statement; 
    if (statement = [self prepare:sql]) { 
     result = sqlite3_step(statement); 
    }  
    sqlite3_finalize(statement); 
    if (result) { 
     if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(databaseTableWasUpdated:)]) { 
      [delegate databaseTableWasUpdated:table]; 
     } 
     return YES; 
    } else { 
     return NO; 
    } 

} 

// requirements for closing things down 

- (void)dealloc { 
    [self close]; 
    [delegate release]; 
    [super dealloc]; 
} 

- (void)close { 

    if (dbh) { 
     sqlite3_close(dbh); 
    } 
} 

@end