本地推送二维码生成以及保存

本地推送二维码生成以及保存_第1张图片

手动导入sdk包


AppDelegate.m中

#import "AppDelegate.h"

#import "UMessage.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

//推送

- (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)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo

{

[UMessage didReceiveRemoteNotification:userInfo];

//    self.userInfo = userInfo;

//    //定制自定的的弹出框

//    if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)

//    {

//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题"

//                                                            message:@"Test On ApplicationStateActive"

//                                                          delegate:self

//                                                  cancelButtonTitle:@"确定"

//                                                  otherButtonTitles:nil];

//

//        [alertView show];

//

//    }

}

//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{

//应用处于后台时的本地推送接受

}

}

Main.storyboard中

本地推送二维码生成以及保存_第2张图片

注意拉线多重拉线会报错


ViewController.m中

#import "ViewController.h"

#import

#import "LoadData.h"

#import "imgViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *text;

@property (weak, nonatomic) IBOutlet UIImageView *img;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

//二维码生成

- (IBAction)sheng:(id)sender {

self.img.image = [QRCodeGenerator qrImageForString:self.text.text imageSize:self.img.bounds.size.width];

}

//历史记录

- (IBAction)lishi:(id)sender {

[[LoadData initShaertData]initOpenData];

Model *mm = [[Model alloc]init];

mm.imgName = self.text.text;

mm.img = UIImagePNGRepresentation(self.img.image);

[[LoadData initShaertData]addData:mm];

[[LoadData initShaertData]close];

//    跳转

imgViewController *img = [[imgViewController alloc]init];

[self.navigationController pushViewController:img animated:YES];

}

创建Model类

Model.h中 继承NSObject

@interface Model : NSObject

@property(nonatomic,strong)NSString *imgName;

@property(nonatomic,strong)NSData *img;

创建LoadData类

LoadData.h中继承NSObject

#import

#import

#import "Model.h"

@interface LoadData : NSObject

{

sqlite3 *ss;

}

//单例类

+(instancetype)initShaertData;

//打开数据库

-(void)initOpenData;

//添加

-(void)addData:(Model *)mm;

//查询

-(NSMutableArray *)getAllData;

//关闭

-(void)close;

@end

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);

}

@end


创建新的视图控制器imgViewController继承UIViewController

imgViewController.m中

#import "imgViewController.h"

#import "LoadData.h"

@interface imgViewController ()

{

UITableView *myTable;

NSMutableArray *arr;

}

@end

@implementation imgViewController

- (void)viewDidLoad {

[super viewDidLoad];

//表格

myTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

myTable.delegate = self;

myTable.dataSource = self;

myTable.rowHeight = 100;

[self.view addSubview:myTable];

arr = [[NSMutableArray alloc]init];

//    接受数据

[[LoadData initShaertData]initOpenData];

arr = [[LoadData initShaertData]getAllData];

[[LoadData initShaertData]close];

[myTable reloadData];

}

//行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return arr.count;

}

//单元格内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//    复用池

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sy"];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"sy"];

}

Model *mm = arr[indexPath.row];

cell.imageView.image = [[UIImage alloc]initWithData:mm.img];

cell.textLabel.text = mm.imgName;

return cell;

}

你可能感兴趣的:(本地推送二维码生成以及保存)