调试代码若出现错误,为了快速定位错误代码位置,可用一些系统定义的宏,如:
NSLog(@"错误发生位置:文件名:%s,函数名:%s,代码行数:%d",__FILE__,__FUNCTION__,__LINE__);
NSString *path=[[NSBundle mainBundle] pathForResource:@"视频名称" ofType:@"mp4"];
if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)){
UISaveVideoAtPathToSavedPhotosAlbum(path,nil,nil,nil);
}else {
//NSLog(@"no available");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"no available"delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[alert show];
[alert release];
转自: http://xiongzhend.blog.163.com/blog/static/6409850120108821651530/
在 iPhone 静音情况下,播放 MP3 文件的代码
在 iPhone 静音的情况下,iOS 系统自带的 iPod 应用可以正常播放音乐,但很多开发者的第三方应用中却不行,下面这段代码能保证您的应用在 iPhone 静音情况下,照常播放 MP3 文件。
- (IBAction) record:(id)sender
{
NSError *error = nil;
BOOL b = [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:&error];
[[AVAudioSession sharedInstance] setDelegate:self];
if(b){
NSLog(@"set audio session category record victory%d",b);
}else {
NSLog(@"error=================%@",error);
}
b = [[AVAudioSession sharedInstance] setActive:YES error:&error];
if(b){
NSLog(@"set active yes %d",b);
}else {
NSLog(@"error=================%@",error);
}
NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"voice.caf"];
//NSLog(@"filePath=========================%@",filePath);
self.recordFileName = filePath;
NSURL *url = [NSURL fileURLWithPath:filePath];
AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
mRecorder = recorder;
[recorder setDelegate:self];
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
BOOL audioHWAvailable = [[AVAudioSession sharedInstance] inputIsAvailable];
// start recording
[recorder recordForDuration:(NSTimeInterval) 3];
}
播放相对简单:
- (IBAction) play:(id)sender
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[[AVAudioSession sharedInstance] setDelegate:self];
NSError *error = nil;
BOOL b = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&error];
// if(b){
// NSLog(@"====================%d",b);
// }else {
// NSLog(@"error=================%@",error);
// }
b = [audioSession setActive:YES error:&error];
// if(b){
// NSLog(@"====================%d",b);
// }else {
// NSLog(@"error=================%@",error);
// }
NSURL *url = [NSURL fileURLWithPath:self.recordFileName];
//NSLog(@"url============================%@",url);
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.meteringEnabled = YES;
player.volume = 1.0;
player.delegate = self;
[player prepareToPlay];
[player play];
[pool release];
}
在适当的地方加入下面的代码:
NSString* gifFileName = @"文件名.gif";
NSMutableString* htmlStr = [NSMutableString string];
[htmlStr appendString:@"Hello Honey"];
[htmlStr appendString:@"<p><img src=\""];
[htmlStr appendFormat:@"%@",gifFileName];
[htmlStr appendString:@"\" alt=\"picture\"/>"];
[WebView loadHTMLString:htmlStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
别忘了添加资源文件 “文件名.gif”
捕获 iPhone 电话呼叫事件的方法
这是 iOS 4.0 以后才公开的接口,用于捕获 iPhone 通话事件,这样有助于开发者在应用里添加靠电话播出或接入来触发的功能,比如电话录音。代码介绍如下
CTCallCenter *center = [[CTCallCenter alloc] init];
center.callEventHandler = ^(CTCall *call) {
NSLog(@"call:%@", call.callState);
};
Cocoa生成UUID的方法
NSString *UUID(){
CFUUIDRef uuid = CFUUIDCreate(NULL);
return (NSString *)CFUUIDCreateString(NULL, uuid);
}
Objective C单态模式
+ (CLASS*)SharedObject
{
@synchronized(self) {
if (_SharedCLASS==nil) {
_SharedCLASS = [[self alloc] init];
}
return _SharedCLASS;
}
return nil;
}