libqrencode
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
movie.h
movie.m
qrViewController.h
qrViewController.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vip = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vip];
self.window.rootViewController = nav;
[self.window makeKeyWindow];
return YES;
}
#import "ViewController.h"
#import "movie.h"
#import "qrViewController.h"
@interface ViewController ()
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(100, 500, 100, 100);
[btn1 setTitle:@"视频播放" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(movie) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(100, 300, 100, 100);
[btn2 setTitle:@"二维码生成" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(qr) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
}
-(void)movie
{
[self.navigationController pushViewController:[[movie alloc] init] animated:YES];
}
-(void)qr
{
[self.navigationController pushViewController:[[qrViewController alloc] init] animated:YES];
}
@end
#import <UIKit/UIKit.h>
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
@interface movie : AVPlayerViewController
@end
#import "movie.h"
@implementation movie
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setupVideoPlayback];
}
-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if ([self isPlaying]) {
// stop the video playback
[self stopPlayback];
}
}
-(void)stopPlayback {
[self.player setRate:0];
self.player = nil;
}
-(BOOL)isPlaying {
if (self.player.currentItem && self.player.rate > 0) {
return YES;
}
return NO;
}
-(void)setupVideoPlayback {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"movie" withExtension:@"mp4"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
self.player = [AVPlayer playerWithPlayerItem:item];
[self.player seekToTime:kCMTimeZero];
[self.player play];
}
@end
#import <UIKit/UIKit.h>
#import "QRCodeGenerator.h"
@interface qrViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *text;
@property (strong, nonatomic) IBOutlet UIImageView *image;
- (IBAction)save:(id)sender;
@end
#import "qrViewController.h"
@interface qrViewController ()
@end
@implementation qrViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)save:(id)sender
{
//将输入的文本生成二维码
self.image.image = [QRCodeGenerator qrImageForString:self.text.text imageSize:128];
//将图片存储到沙盒
NSString *docpath = [NSString stringWithFormat:@"%@/%@.png",NSHomeDirectory(),self.text.text];
[UIImagePNGRepresentation(self.image.image) writeToFile:docpath atomically:YES];
NSLog(@"%@",docpath);
}
@end
CoreData.framework
Entity+CoreDataProperties.h
Entity+CoreDataProperties.m
Entity.h
Entity.m
Property List.plist
AppDelegate.h
AppDelegate.m
ViewController.h
ViewController.m
TwoViewController.h
TwoViewController.m
DataHandle.h
DataHandle.m
Entity+CoreDataProperties.h
#import "Entity.h"
NS_ASSUME_NONNULL_BEGIN
@interface Entity (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *age;
@property (nullable, nonatomic, retain) NSString *name;
@property (nullable, nonatomic, retain) NSString *sex;
@end
NS_ASSUME_NONNULL_END
Entity+CoreDataProperties.m
#import "Entity+CoreDataProperties.h"
@implementation Entity (CoreDataProperties)
@dynamic age;
@dynamic name;
@dynamic sex;
@end
Entity.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
NS_ASSUME_NONNULL_BEGIN
@interface Entity : NSManagedObject
// Insert code here to declare functionality of your managed object subclass
@end
NS_ASSUME_NONNULL_END
#import "Entity+CoreDataProperties.h"
Entity.m
#import "Entity.h"
@implementation Entity
// Insert code here to add functionality to your managed object subclass
@end
Property List.plist
AppDelegate.h
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;
@end
AppDelegate.m
//
// AppDelegate.m
// CoreDataTest
//
// Created by King on 16/5/20.
// Copyright © 2016年 ?HL. All rights reserved.
//
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *Main_vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:Main_vc];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}
#pragma mark - Core Data stack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory {
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.lih.----.CoreDataTest" in the application's documents directory.
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CoreDataTest" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
// Create the coordinator and store
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataTest.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
@end
ViewController.h
ViewController.m
//
// ViewController.m
// CoreDataTest
//
// Created by King on 16/5/20.
// Copyright © 2016年 ?HL. All rights reserved.
//
#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "DataHandle.h"
#import "TwoViewController.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
{
NSArray *arr;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];
UITableView *table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
NSString *s = [[NSBundle mainBundle] pathForResource:@"Property List.plist" ofType:nil];
arr = [NSArray arrayWithContentsOfFile:s];
}
- (void)rightClick
{
TwoViewController *two = [[TwoViewController alloc]init];
[self.navigationController pushViewController:two animated:YES];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell.textLabel.text = arr[indexPath.row][@"name"];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@",arr[indexPath.row][@"age"],arr[indexPath.row][@"sex"]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertController *AlertController = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"是否保存!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSDictionary *dic = arr[indexPath.row];
[[DataHandle defaultHandle]insert:dic];
}];
[AlertController addAction:cancelAction];
[AlertController addAction:okAction];
[self presentViewController:AlertController animated:YES completion:nil];
}
@end
TwoViewController.h
TwoViewController.m
//
// TwoViewController.m
// CoreDataTest
//
// Created by King on 16/5/21.
// Copyright © 2016年 ?HL. All rights reserved.
//
#import "TwoViewController.h"
#import "DataHandle.h"
#import "Entity.h"
@interface TwoViewController () <UITableViewDataSource,UITableViewDelegate>
{
NSArray *arr;
}
@end
@implementation TwoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UITableView *table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
arr = [[DataHandle defaultHandle]selectAll];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"cellId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
Entity *ent = arr[indexPath.row];
cell.textLabel.text = ent.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@",ent.age,ent.sex];
return cell;
}
@end
DataHandle.h
#import <Foundation/Foundation.h>
#import "AppDelegate.h"
@interface DataHandle : NSObject
+ (instancetype)defaultHandle;
- (void)insert:(NSDictionary *)dict;
- (NSArray *)selectAll;
@end
DataHandle.m
//
// DataHandle.m
// CoreDataTest
//
// Created by King on 16/5/21.
// Copyright © 2016年 ?HL. All rights reserved.
//
#import "DataHandle.h"
#import "Entity.h"
@interface DataHandle()
{
AppDelegate *app;
Entity *ent;
}
@end
static DataHandle *_shareHandle = nil;
@implementation DataHandle
+ (instancetype)defaultHandle
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!_shareHandle)
{
_shareHandle = [[DataHandle alloc]init];
}
});
return _shareHandle;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
if (!_shareHandle)
{
_shareHandle = [super allocWithZone:zone];
}
return _shareHandle;
}
- (id)copy
{
return self;
}
- (id)mutableCopy
{
return self;
}
- (void)insert:(NSDictionary *)dict
{
app = [UIApplication sharedApplication].delegate;
ent = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:app.managedObjectContext];
// stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:app.managedObjectContext];
ent.name = [dict objectForKey:@"name"];
ent.age = [dict objectForKey:@"age"];
ent.sex = [dict objectForKey:@"sex"];
NSLog(@"ent = %@",ent);
[app.managedObjectContext save:nil];
}
- (NSArray *)selectAll
{
app = [UIApplication sharedApplication].delegate;
NSFetchRequest *req = [[NSFetchRequest alloc]init];
NSEntityDescription *ption = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:app.managedObjectContext];
[req setEntity:ption];
NSMutableArray *arr = [NSMutableArray arrayWithArray:[app.managedObjectContext executeFetchRequest:req error:nil]];
return arr;
}
@end