導入第三方。?libqrencode?
#import "QRCodeGenerator.h"
#import "OneViewController.h"
@interface ViewController ()
{
? ? UIImageView *imgV;
? ? UITextField *text;
? ? UITextField*textF;
}
@end
@implementation ViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.navigationItem.title = @"二維碼";
? ? UIBarButtonItem*right = [[UIBarButtonItemalloc]initWithTitle:@"查看二維碼"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(tiaoZhaun)];
? ? self.navigationItem.rightBarButtonItem = right;
? ? imgV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 250, 200, 200)];
? ? imgV.backgroundColor = [UIColor redColor];
? ? [self.view addSubview:imgV];
? ? // 設置按鈕
? ? UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
? ? // 設置背景顏色
? ? btn.backgroundColor = [UIColor greenColor];
? ? // 設置文字
? ? [btnsetTitle:@"生成二維碼" forState:UIControlStateNormal];
? ? // 設置文字顏色
? ? [btnsetTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
? ? // 點擊事件
? ? [btnaddTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
? ? // 添加到主界面
? ? [self.viewaddSubview:btn];
}
// 跳轉到下一個界面
- (void)tiaoZhaun{
? ? [self presentViewController:[OneViewController new] animated:YES completion:nil];
}
// 點擊事件
- (void)click{
? ? // 創建提示框
? ? UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"創建二維碼" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
? ? [alertsetAlertViewStyle:UIAlertViewStylePlainTextInput];
????textF = [alerttextFieldAtIndex:0];
? ? textF.placeholder=@"請輸入字符串";
? ? [alertshow];
}
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
? ? if(buttonIndex ==1) {
? ? ? ? text= [alertViewtextFieldAtIndex:0];
? ? }
? ? // 生成二維碼
? ? UIImage *image = [QRCodeGenerator qrImageForString:text.text imageSize:imgV.frame.size.width];
? ? imgV.image= image;
? ? //保存二維碼并給與二維碼圖片名稱
? ? ? ? NSString *filePath = [NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),textF.text];
? ? [UIImagePNGRepresentation(imgV.image) writeToFile:filePath atomically:YES];
}
//注冊通知,點擊點擊進行通知的時候調用
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
? ? UIApplication * application=[UIApplication sharedApplication];
? ? //如果當前應用程序沒有注冊本地通知,需要注冊
? ? if([application currentUserNotificationSettings].types==UIUserNotificationTypeNone){
? ? ? ? //設置提示支持的提示方式
? ? ? ? //? ? ? UIUserNotificationTypeBadge? 提示圖標
? ? ? ? //? ? ? UIUserNotificationTypeSound? 提示聲音
? ? ? ? //? ? ? UIUserNotificationTypeAlert? 提示彈框
? ? ? ? UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
? ? ? ? [applicationregisterUserNotificationSettings:setting];
? ? }
? ? //刪除之前的重復通知
? ? [applicationcancelAllLocalNotifications];
? ? //添加本地通知
? ? NSDate * date=[NSDate dateWithTimeIntervalSinceNow:10];
? ? [self LocalNotificationSleep :date];
}
#pragma mark - 添加本地通知
- (void) LocalNotificationSleep:(NSDate*) date{
? ? UILocalNotification * noti=[[UILocalNotification alloc] init];
? ? //設置開始時間
? ? noti.fireDate=date;
? ? //設置body
? ? noti.alertBody=@"你有一條消息提醒,請查收!";
? ? //設置action
? ? noti.alertAction=@"詳情";
? ? //設置鬧鈴
? ? noti.soundName=@"4195.mp3";
#warning 注冊完之后如果不刪除,下次會繼續存在,即使從模擬器卸載掉也會保留
? ? //注冊通知
? ? [[UIApplication sharedApplication] scheduleLocalNotification:noti];
}
下一個頁面
@interface OneViewController ()
{
? ? UIImageView *imgV;
}
@end
@implementationOneViewController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? // 創建圖片框
? ? imgV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 250, 200, 200)];
? ? // 設置背景顏色
? ? imgV.backgroundColor = [UIColor redColor];
? ? // 添加到主界面
? ? [self.view addSubview:imgV];
? ? NSString *filePath = [NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),textF.text];
? ? imgV.image = [UIImage imageWithContentsOfFile:filePath];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
? ? [self dismissViewControllerAnimated:YES completion:nil];
}