UIWebView 相应本地事件

Objective-c代码   收藏代码
  1. - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {  
  2.         AppDelegate *appDelegate=(   AppDelegate *)[[UIApplication sharedApplication] delegate];  
  3.     if (navigationType == UIWebViewNavigationTypeLinkClicked) {  
  4.         NSURL *URL = [request URL];   
  5.         NSLog([URL scheme]);  
  6.         NSLog([URL absoluteString]);  
  7.           
  8.         //code here and call the delegate method  
  9.   
  10.   
  11.         NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];  
  12.         BOOL audioResult=FALSE;  
  13.         NSFileManager *fileManager = [NSFileManager defaultManager];  
  14.         //check if file existed in cache folder  
  15.         NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  16.         NSString *cachePath = [cache objectAtIndex:0] ;  
  17.         NSString *filepath ;  
  18.                   
  19.         filepath=[cachePath stringByAppendingPathComponent:[audioId stringByAppendingString:@".wav"]];  
  20.                   
  21.         if ([fileManager fileExistsAtPath:filepath]==YES){  
  22.             [self playVoice:filepath];  
  23.         }  
  24.         else  
  25.         {  
  26.             audioResult=[appDelegate saveAudio:audioId];  
  27.             if (audioResult==YES)  
  28.             {  
  29.                 //NSLog(@"Speak the audio by the path=%@",filepath);  
  30.                 [self playVoice:filepath];  
  31.             }  
  32.         }  
  33.         [pool release];  
  34.   
  35.           
  36.           
  37.         [appDelegate saveAudio:gotFM];  
  38.           
  39.     }  


保存声音的方法: 
Objective-c代码   收藏代码
  1. -(BOOL)saveAudio:(NSString *)key  
  2. {  
  3.     NSLog(key);  
  4.     BOOL saveSuccess=FALSE;  
  5.     NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"testVoice.db"];     
  6.       
  7.     if(sqlite3_open([dbPath UTF8String], &database_voice) == SQLITE_OK)  
  8.     {  
  9.           
  10.         NSString *sqlString=[NSString stringWithFormat:@"select * from tb_voice where id='%@'",key];  
  11.         NSLog(sqlString);  
  12.         const char *sql_char=[sqlString UTF8String];  
  13.         //prepare the select statement  
  14.         int returnValue = sqlite3_prepare_v2(database_voice, sql_char, -1, &selectStatement, NULL);  
  15.         if(returnValue == SQLITE_OK)  
  16.         {  
  17.             //loop all the rows returned by the query.  
  18.             if(sqlite3_step(selectStatement) == SQLITE_ROW)  
  19.             {  
  20.                   
  21.                 NSFileManager *fileManager = [NSFileManager defaultManager];  
  22.                 NSArray *cache = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  23.                 NSString *cachePath = [cache objectAtIndex:0] ;  
  24.                   
  25.                   
  26.                 NSString *filename=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 0)];  
  27.                 NSLog(filename);  
  28.                   
  29.                 NSData *fileData=[NSData dataWithBytes:sqlite3_column_blob(selectStatement, 2) length:sqlite3_column_bytes(selectStatement,2)];  
  30.                 //save the file data  
  31.                   
  32.                 //check   
  33.                 NSString *filepath = [cachePath stringByAppendingPathComponent:filename];     
  34.                   
  35.                 filepath=[filepath stringByAppendingString:@".wav"];  
  36.                 if ([fileManager createDirectoryAtPath:cachePath attributes:nil]==NO){  
  37.                     NSLog(@"fileManager createDirectoryAtPath:cachePath attributes:nil");  
  38.                 }  
  39.                   
  40.                 if ([fileData writeToFile:filepath atomically:YES]){  
  41.                     saveSuccess=TRUE;  
  42.                     //NSLog(@"saveSuccess");  
  43.                     NSLog(@"returnValue");  
  44.                 }  
  45.                 else  
  46.                 {  
  47.                     //NSLog(@"save Fail");  
  48.                 }  
  49.             }  
  50.             sqlite3_finalize(selectStatement);  
  51.         }  
  52.     }  
  53.     return saveSuccess;  
  54. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值