首先 ,先导入第三方 UMessage_Sdk_1.5
在 AppDelegate.m中导入头文件 #import "UMessage.h"- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[UMessage startWithAppkey:@"your appkey" launchOptions:launchOptions];
//注册通知,如果要使用category的自定义策略,可以参考demo中的代码。
[UMessage registerForRemoteNotifications];
//iOS10必须,加下面这段代码。
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate=self;
UNAuthorizationOptions types10=UNAuthorizationOptionBadge| UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:types10 completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
//点击允许
//这里可以添加一些自己的逻辑
} else {
//点击不允许
//这里可以添加一些自己的逻辑
}
}];
return YES;
}
//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
//关闭U-Push自带的弹出框
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//应用处于前台时的本地推送接受
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
//应用处于后台时的本地推送接受
}
}
在ViewController.m
创建 imgViewController
创建 LoadData.h
#import
#import
#import "Model.h"
@interface LoadData : NSObject{
sqlite3 *ss;
}
//单例类
+(instancetype)initShaertData;
//打开数据库
-(void)initOpenData;
//添加
-(void)addData:(Model *)mm;
//查询
-(NSMutableArray *)getAllData;
//关闭
-(void)close;
在LoadData.m
#import "LoadData.h"
static LoadData *ld = nil;
@implementation LoadData
//单例类
+(instancetype)initShaertData{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ld = [[LoadData alloc]init];
});
return ld;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
if (!ld) {
ld = [super allocWithZone:zone];
}
return ld;
}
-(id)copy{
return self;
}
-(id)mutableCopy{
return self;
}
//打开数据库
-(void)initOpenData{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *newpath = [path stringByAppendingPathComponent:@"sy.db"];
NSLog(@"new == %@",newpath);
if (sqlite3_open([newpath UTF8String],&(ss)) == SQLITE_OK) {
NSLog(@"打开了");
[self initTable];
}
}
//初始化
-(void)initTable{
const char *sql = "create table if not exists zoo(id intager primary key, imgName text,img BLOB)";
sqlite3_stmt *stmt;
sqlite3_prepare(ss, sql, -1, &stmt, nil);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
//添加
-(void)addData:(Model *)mm{
const char *sql = "insert into zoo values(null,?,?)";
sqlite3_stmt *stmt;
sqlite3_prepare(ss, sql, -1, &stmt, nil);
sqlite3_bind_text(stmt, 1, [mm.imgName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [mm.img bytes], (int)[mm.img length], SQLITE_TRANSIENT);
if (sqlite3_step(stmt) == SQLITE_DONE) {
NSLog(@"添加成功");
}
sqlite3_finalize(stmt);
}
//查询
-(NSMutableArray *)getAllData{
const char *sql = "select *from zoo";
sqlite3_stmt *stmt;
sqlite3_prepare(ss, sql, -1, &stmt, nil);
NSMutableArray *arr = [[NSMutableArray alloc]init];
while (sqlite3_step(stmt) == SQLITE_ROW) {
Model *mm = [[Model alloc]init];
mm.imgName = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];
mm.img = [NSData dataWithBytes:sqlite3_column_blob(stmt, 2) length:sqlite3_column_bytes(stmt, 2)];
[arr addObject:mm];
}
sqlite3_finalize(stmt);
return arr;
}
//关闭
-(void)close{
sqlite3_close(ss);
}
创建新的 Model
在 .h中写入以下
#import
@interface Model : NSObject
@property(nonatomic,strong)NSString *imgName;
@property(nonatomic,strong)NSData *img;
@end