emoij发送及显示 ios
2019-03-28 12:18:00 weixin_33728268 阅读数 2
//发送async异步请求
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSURLRequest *requset = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        //这里使用主队列 [NSOperationQueue mainQueue]
        
        //response          服务器返回的相应头
        //data              服务器返回的响应体
        //connectionError   链接错误
        
        //判断请求是否有错误
        if (!connectionError) {
            //把二进制数据转换成NSString
            NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"html的字符串=%@",html);
        }else{
            NSLog(@"连接错误");
        }
    }];

转载于:https://www.jianshu.com/p/6cb1361318a4

2014-05-14 11:16:23 linaiyuan114 阅读数 1629

#import "ViewController.h"



@implementation ViewController



- (void)viewDidLoad

{

    [super viewDidLoad];

    

    

    //录音设置

    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init];

    //设置录音格式  AVFormatIDKey==kAudioFormatLinearPCM

    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];

    //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)

    [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];

    //录音通道数  1 2

    [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

    //线性采样位数  8162432

    [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];

    //录音的质量

    [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];

    NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/aaa"];

    _recorder=[[AVAudioRecorder alloc]initWithURL:[NSURL fileURLWithPath:path] settings:recordSetting error:nil];

    [_recorder prepareToRecord];

    

    _button=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    _button.frame=CGRectMake(100, 100, 100, 40);

    [_button setTitle:@"发送录音" forState:UIControlStateNormal];

    [_button addTarget:self action:@selector(begin) forControlEvents:UIControlEventTouchDown];

    [_button addTarget:self action:@selector(end) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_button];

}

- (void)begin{

    [_button setTitle:@"录音中" forState:UIControlStateNormal];

    [_recorder record];

}

- (void)end{

    [_button setTitle:@"开始录音" forState:UIControlStateNormal];

    [_recorder stop];

    NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/aaa"];


    AVAudioPlayer *play=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path]  error:nil];

    [play play];

}


@end

2019-05-02 19:16:03 qq_43361450 阅读数 18
- (void)timeOut
{
    __block NSInteger timeOut = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _Timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //每秒执行一次
    dispatch_source_set_timer(_Timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_Timer, ^{
        if (timeOut <= 0)
        {
            //如果倒计时结束,则关闭
            
            dispatch_source_cancel(_Timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //倒计时结束,关闭之后进行的操作。
                [self.seconds setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                [self.seconds setTitle:@"00s" forState:UIControlStateNormal];
                self.seconds.userInteractionEnabled = YES;
                
            });
            
        } else {
            int allTime = (int)60 + 1;
            
            int seconds = timeOut % allTime;
            
            NSString *timeStr = [NSString stringWithFormat:@"%0.2d",seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //倒计时进行中进行的操作
                
                [self.seconds setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
                self.seconds.layer.borderColor = [UIColor redColor].CGColor;
                
                [self.seconds setTitle:[NSString stringWithFormat:@"%@s",timeStr] forState:UIControlStateNormal];
                self.seconds.userInteractionEnabled = NO;
            });
            timeOut--;
        }
    });
    dispatch_resume(_Timer);
}

2018-07-11 18:27:00 weixin_34281477 阅读数 1

最近看SDWebImage的源码,发现发送通知都是切换到主线程中发送。

    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self];
    });

以往开发中一直认为在主线程和子线程中发送通知都是一样的,因为只是发送通知并没有更新UI,但这是有名的SDWebImage第三方库,不可能无缘无故的切换到主线程发通知。我猜想可能是接收通知后的操作和发送通知的操作在同一线程里,通过查询找到这样一句话

In a multithreaded application, notifications are always delivered in
the thread in which the notification was posted, which may not be the
same thread in which an observer registered itself.

所以也就是说,在哪个线程里发送,就在那个线程里执行接收通知后的操作。

转载于:https://www.jianshu.com/p/cc8b6c59bbed

2016-04-28 15:35:42 Zhi_Sheng 阅读数 274

1,

 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions下添加


    //安装异常捕获
#ifdef DEBUG
    [NSThread detachNewThreadSelector:@selector(startCatchException) toTarget:self withObject:nil];
#endif

- (void)startCatchException
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [self setCompileTime];
    //保存错误日志
    [CatchExceptionHandler setDefaultHandler];

    [pool drain];
}

3,捕获异常类

.h

@interface CatchExceptionHandler : NSObject

+ (void)setDefaultHandler;
+ (NSUncaughtExceptionHandler *)getHandler;

@end

.m

#import "CatchExceptionHandler.h"
#import "ABServices.h"

#define NSExceptionFile @"errorFile.txt"

@implementation CatchExceptionHandler


NSString *applicationDocumentsDirectory() {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString * documentsDirectory  =[paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:NSExceptionFile];
}

void UncaughtExceptionHandler(NSException *exception)
{
    NSArray *arr = [exception callStackSymbols];
    NSString *reason = [exception reason];
    NSString *name = [exception name];
    NSString *strTime =[ABServices getCurrentSysTime];

    NSString *strError = [NSString stringWithFormat:@"\n\n\n=============异常崩溃报告=============\n当前版本的编译时间:\n%@\n崩溃发生的时间:\n %@\n崩溃名称:\n%@\n崩溃原因:\n%@\n堆栈信息:\n%@", [ABServices currentCompileTime],strTime,name,reason,[arr componentsJoinedByString:@"\n"]];

    NSString *path = applicationDocumentsDirectory();
    if ([[NSFileManager defaultManager]fileExistsAtPath:path])
    {
        NSString *lasterror = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
        strError  = [NSString stringWithFormat:@"%@%@", lasterror, strError];
        [lasterror release];
    }
    [strError writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];
    //添加收件人
    NSArray *toRecipients = [NSArray arrayWithObjects: @"a@b.com", @"a@a.com",nil];
    [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
    //添加抄送
    NSArray *ccRecipients = [NSArray arrayWithObjects:@"e@e.com" ,nil];
    [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
    //添加密送
    NSArray *bccRecipients = [NSArray arrayWithObjects:@"", nil];
    [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
    [mailUrl appendString:@"&subject=崩溃日志"];
    //添加邮件内容
    [mailUrl appendString:[NSString stringWithFormat:@"&body=%@", strError]];
    NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    //#ifdef _DEBUG_LOG_
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
    //#endif
}


+ (void)setDefaultHandler
{
    NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
}

+ (NSUncaughtExceptionHandler*)getHandler
{
    return NSGetUncaughtExceptionHandler();
}

@end

iOS发送本地通知

阅读数 738

iOS发送http请求

阅读数 2940

iOS 显示%

阅读数 691

IOS发送Email的方法

阅读数 2754

没有更多推荐了,返回首页