分享

iOS Wi-Fi连接

 现在决定明天 2014-03-11

#import <SystemConfiguration/CaptiveNetwork.h>

+ (id)fetchSSIDInfo
{
    NSArray *ifs = (id)CNCopySupportedInterfaces();
    NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
        if (info && [info count]) {
            break;
        }
        [info release];
    }
    [ifs release];
    return [info autorelease];
}

//check wifi sid
        NSDictionary *ifs = [BaseFunction fetchSSIDInfo];
        NSString *ssid = [[ifs objectForKey:@"SSID"] lowercaseString];
        debug_NSLog(@"ssid:%@",ssid);

posted @ 2013-02-25 17:47 剑气纵横 阅读(85) 评论(0)  编辑

在ios扫描公共区域内wifi信息中,写了实现wifi扫描的一种办法,然则那种办法扫描出来的wifi信息不全,下面是扫描全部wifi信息的实现办法: 

#import <CoreFoundation/CoreFoundation.h> 
#import <Foundation/NSTimer.h> 
#import <Foundation/Foundation.h> 
#include <dlfcn.h> 
#include <ifaddrs.h> 
#include <arpa/inet.h> 
@interface MSNetworksManager : NSObject { 

    NSMutableDictionary *networks; 
    NSArray *types; 
    int autoScanInterval; 
    bool scanning; 
    bool autoScanning; 
    void *libHandle; 
    void *airportHandle; 
   
    int (*open)(void *); 
    int (*bind)(void *, NSString *); 
    int (*close)(void *); 
    int (*associate)(void *, NSDictionary*, NSString*); 
    int (*scan)(void *, NSArray **, void *); 
   
    //int (*open)(void *); 
    //int (*bind)(void *, NSString *); 
    //int (*close)(void *); 
    //int (*scan)(void *, NSArray **, void *); 
    //int (*associate)(void*, NSDictionary *, NSString *); 
    int (*getpower)(void *, char *); 
    int (*setpower)(void*, char*); 

+ (MSNetworksManager *)sharedNetworksManager; 
+ (NSNumber *)numberFromBSSID:(NSString *) bssid; 
- (NSMutableDictionary *)networks; 
- (NSDictionary *)networks:(int) type; 
- (NSDictionary *)network:(NSString *) aNetwork; 
- (id)init; 
- (void)dealloc; 
- (int)numberOfNetworks; 
- (int)numberOfNetworks:(int) type; 
- (int)autoScanInterval; 
- (void)scan; 
- (void)removeNetwork:(NSString *)aNetwork; 
- (void)removeAllNetworks; 
- (void)removeAllNetworks:(int) type; 
- (void)autoScan:(bool)scan; 
- (bool)autoScan; 
- (void)scanSelector:(id)param; 
- (void)setAutoScanInterval:(int) scanInterval; 
- (int)associateNetwork: (NSDictionary *)bss: (NSString *)password; 
- (int)getPower: (char *)power; 
- (int)setPower: (char *)power; 
- (NSString *) localIPAddress; 

@end 

.m文件: 

#import "MSNetworksManager.h" 
static MSNetworksManager *NetworksManager; 

@implementation MSNetworksManager 
+ (MSNetworksManager *)sharedNetworksManager 

    if (!NetworksManager) 
        NetworksManager = [[MSNetworksManager alloc] init]; 
    return NetworksManager; 


+ (NSNumber *)numberFromBSSID:(NSString *) bssid 

    int x = 0; 
    uint64_t longmac; 
    int MAC_LEN = 6; 
    short unsigned int *bs_in = malloc(sizeof(short unsigned int) * MAC_LEN); 
    if (sscanf([bssid cStringUsingEncoding: [NSString defaultCStringEncoding]],"%hX:%hX:%hX:%hX:%hX:%hX",&bs_in[0], &bs_in[1], &bs_in[2], &bs_in[3], &bs_in[4], &bs_in[5]) == MAC_LEN) 
    { 
        for (x = 0; x < MAC_LEN; x++) 
            longmac |= (uint64_t) bs_in[x] << ((MAC_LEN – x – 1) *; 
    } else { 
        NSLog(@"WARN: invalid mac address! %@",self); 
    } 
    free(bs_in); 
    return [NSNumber numberWithUnsignedLongLong:longmac]; 


- (NSDictionary *)networks 

    // TODO: Implement joining of network types 
    return networks; 

- (NSDictionary *)networks:(int) type 

    // TODO: Implement ing of network types 
    if(type != 0) 
        NSLog(@"WARN: Non 80211 networks are not supported. %@",self); 
    return networks; 


- (NSDictionary *)network:(NSString *) aNetwork 

    return [networks objectForKey: aNetwork]; 


- (id)init 

    self = [super init]; 
    NetworksManager = self; 
    networks = [[NSMutableDictionary alloc] init]; 
    types = [NSArray arrayWithObjects:@"80211", @"Bluetooth", @"GSM", nil]; 
    [types retain]; 
    autoScanInterval = 5; //seconds 
    // For iPhone 2.0 
    // libHandle = dlopen("/System/Library/PrivateFrameworks/Apple80211.framework/Apple80211", RTLD_LAZY); 
    // For iPhone 3.0 

    libHandle = dlopen("/System/Library/SystemConfiguration/WiFiManager.bundle/WiFiManager", RTLD_LAZY); 
    open = dlsym(libHandle, "Apple80211Open"); 
    bind = dlsym(libHandle, "Apple80211BindToInterface"); 
    close = dlsym(libHandle, "Apple80211Close"); 
    scan = dlsym(libHandle, "Apple80211Scan"); 
    associate = dlsym(libHandle, "Apple80211Associate"); 
    getpower = dlsym(libHandle, "Apple80211GetPower"); 
    setpower = dlsym(libHandle, "Apple80211SetPower"); 
   
    open(&airportHandle); 
    bind(airportHandle, @"en0"); 
   
    return self; 


- (void)dealloc 

    close(&airportHandle); 
    [super dealloc]; 


- (int)numberOfNetworks 

    return [networks count]; 

- (int)numberOfNetworks:(int) type 

    // TODO: Implement ing of network types 
    if(type != 0) 
        NSLog(@"WARN: Non 80211 networks are not supported. %@",self); 
    return [networks count]; 


- (int)autoScanInterval 

    return autoScanInterval; 


- (void)scan 

//    NSLog(@"Scanning…"); 
    scanning = true; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"startedScanning" object:self]; 
    NSArray *scan_networks; 
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 
    [parameters setObject:@"YES" forKey:@"SCAN_MERGE"]; 
    scan(airportHandle, &scan_networks, parameters); 
    int i; 
    //bool changed; 
    [networks removeAllObjects]; 
    for (i = 0; i < [scan_networks count]; i++) { 
                [networks setObject:[[scan_networks objectAtIndex: i] objectForKey:@"BSSID"] forKey:[[scan_networks objectAtIndex: i] objectForKey:@"RSSI"]];    
    } 
   NSLog(@"Scan Finished…"); 


- (void)removeNetwork:(NSString *)aNetwork 

    [networks removeObjectForKey:aNetwork]; 


- (void)removeAllNetworks 

    [networks removeAllObjects]; 


- (void)removeAllNetworks:(int) type 

    if(type != 0) 
        NSLog(@"WARN: Non 80211 networks are not supported. %@",self); 
    [networks removeAllObjects]; 


- (void)autoScan:(bool) bScan 

    autoScanning = bScan; 
    if(bScan) { 
        [self scan]; 
        [NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self or:@or (scanSelector:) userInfo:nil repeats:NO]; 
    } 
    NSLog(@"WARN: Automatic scanning not fully supported yet. %@",self); 


- (bool)autoScan 

    return autoScanning; 


- (void)scanSelector:(id)param { 
    if(autoScanning) { 
        [self scan]; 
        [NSTimer scheduledTimerWithTimeInterval:autoScanInterval target:self or:@or (scanSelector:) userInfo:nil repeats:NO]; 
    } 


- (void)setAutoScanInterval:(int) scanInterval 

    autoScanInterval = scanInterval; 


- (int)associateNetwork:(NSDictionary *)bss: (NSString *)password 

    if(bss!=nil) { 
        NSLog(@"associateNetwork"); 
        int ret = associate(airportHandle, bss, password); 
        return ret; 
    }else 
        return -1; 


- (int)getPower: (char *)power 

    return getpower(airportHandle, power); 


- (int)setPower: (char *)power 

    return setpower(airportHandle, power); 


- (NSString *) localIPAddress 

    NSString *address = @"error"; 
    struct ifaddrs *interfaces = NULL; 
    struct ifaddrs *temp_addr = NULL; 
    int success = 0; 
   
    // retrieve the current interfaces – returns 0 on success 
    success = getifaddrs(&interfaces); 
    if (success == 0) 
    { 
        // Loop through linked list of interfaces 
        temp_addr = interfaces; 
        while(temp_addr != NULL) 
        { 
            if(temp_addr->ifa_addr->sa_family == AF_INET) 
            { 
                // Check if interface is en0 which is the wifi connection on the iPhone 
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) 
                { 
                    // Get NSString C String 
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; 
                } 
            } 
           
            temp_addr = temp_addr->ifa_next; 
        } 
    } 
   
    // Free memory 
    freeifaddrs(interfaces); 
    return address; 


@end

posted @ 2013-02-25 17:41 剑气纵横 阅读(28) 评论(0)  编辑

注册账号---》开发----》发布----》等待审核----》上架

发布过程

1: 登录到到开发者帐户,再登录到iTunes Connect 中 设置 要发布版本的对应信息

       注意:设置的信息一定要和你发布的程序对上 ,如:identifter,其他看看就知道了

2:发布 

 

使用xcode 4发布app 实例操作是本文介绍的内容,不多说,我们直接进入话题。

1、iOS Provisioning Portal 和iTunes Connect 没有变,下载与安装.mobileprovision文件与以前一下。

2、Xcode 4 整合相关的工具(All in One), 集成了Organizer与Application Loader,Xcode4多了Run/Build Scheme的概念,指以前的编译的选项如 device/simulator, iOS version等。发布应用时需要选择iOS Device:

使用Xcode 4发布App 实例操作

点击 Edit Scheme... 以编辑Archive选项,它的默认 编译配置为Distribution 并且将归档显示在Organizer中:

使用Xcode 4发布App 实例操作

3、检查 编译配置文件:product name, info.plist, version, Target Device Family, iOS Deployment Target, Code Signing Identity etc. (这与Xcode 3.x一致)。

使用Xcode 4发布App 实例操作

4、进入Product菜单->Archive,然后会自动弹出Organizer

Archive包是用来上传到AppStore的

Xcode4让你不需要关心编译发布的程序包在哪里,只需要在Organizer中管理即可。

当然你可以在XCode->Preferences->Locations tab配置程序的存储位置。

使用Xcode 4发布App 实例操作

5、在Organizer中选择 Archive, 验证与上传。

这里验证的时候要检查你在iTunes Connect中设置的信息

  1. Validate and submit your app to AppStore 

6、成功上传App之后,Archive状态会变为 已提交。

posted @ 2013-02-25 15:08 剑气纵横 阅读(28) 评论(0)  编辑

方法1:
     在applicationDidFinishLaunching函数里添加
      [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(volumeChanged:)
                                             name:@"AVSystemController_SystemVolumeDidChangeNotification"
                                             object:nil];

  - (void)volumeChanged:(NSNotification *)notification
{
    float volume =
    [[[notification userInfo]
      objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
     floatValue];
    
    DDLogVerbose(@"current volume = %f", volume);   
}

   弊端:当app进入后天后,依然会监听到volume的变化

2. 对 AudioSession 添加volume变化的 listener, 可以放在startAudioSession函数里
    //add a listener for Outputvolume
    AudioSessionAddPropertyListener(kAudioSessionProperty_CurrentHardwareOutputVolume ,
                                    volumeListenerCallback,
                                    self
                                    );


void volumeListenerCallback (
                             void                      *inClientData,
                             AudioSessionPropertyID    inID,
                             UInt32                    inDataSize,
                             const void                *inData
                             ){
    const float *volumePointer = inData;
    float volume = *volumePointer;
    NSLog(@"volumeListenerCallback %f", volume);
}

3. 获取当前的volume
     float volume = 0.0;
    UInt32 dataSize = sizeof(float);
    OSStatus status = AudioSessionGetProperty (kAudioSessionProperty_CurrentHardwareOutputVolume,
                             &dataSize,
                             &volume);

posted @ 2013-02-25 14:32 剑气纵横 阅读(12) 评论(0)  编辑

Android 上有一些很有趣的应用,例如《吹裙子》、《吹气球》之类的。利用的是实时获取麦克风输入音量,然后进行相应的处理。录音过程中怎样获得声音的大小呢?网上 也不少人问如何处理这个事情,也有一些解答,不过都没有实际的代码。简单摸索了一下,写了个小 Demo 试了试,果然可以。给大家共享一下。

不解释代码了,大家看注释。

[java]  view plain copy
  1. package com.xxiyy.spl;  
  2.    
  3. import android.media.AudioFormat;  
  4. import android.media.AudioRecord;  
  5. import android.media.MediaRecorder;  
  6. import android.util.Log;  
  7.    
  8. public class RecordThread extends Thread {  
  9.     private AudioRecord ar;  
  10.     private int bs;  
  11.     private static int SAMPLE_RATE_IN_HZ = 8000;  
  12.     private boolean isRun = false;  
  13.    
  14.     public RecordThread() {  
  15.         super();  
  16.         bs = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,  
  17.                 AudioFormat.CHANNEL_CONFIGURATION_MONO,  
  18.                 AudioFormat.ENCODING_PCM_16BIT);  
  19.         ar = new AudioRecord(MediaRecorder.AudioSource.MIC, SAMPLE_RATE_IN_HZ,  
  20.                 AudioFormat.CHANNEL_CONFIGURATION_MONO,  
  21.                 AudioFormat.ENCODING_PCM_16BIT, bs);  
  22.     }  
  23.    
  24.     public void run() {  
  25.         super.run();  
  26.         ar.startRecording();  
  27.                 // 用于读取的 buffer  
  28.         byte[] buffer = new byte[bs];  
  29.         isRun = true;  
  30.         while (isRun) {  
  31.             int r = ar.read(buffer, 0, bs);  
  32.             int v = 0;  
  33.                         // 将 buffer 内容取出,进行平方和运算  
  34.             for (int i = 0; i < buffer.length; i++) {  
  35.                 // 这里没有做运算的优化,为了更加清晰的展示代码  
  36.                 v += buffer[i] * buffer[i];  
  37.             }  
  38.             // 平方和除以数据总长度,得到音量大小。可以获取白噪声值,然后对实际采样进行标准化。  
  39.             // 如果想利用这个数值进行操作,建议用 sendMessage 将其抛出,在 Handler 里进行处理。  
  40.             Log.d("spl", String.valueOf(v / (float) r));  
  41.         }  
  42.         ar.stop();  
  43.     }  
  44.    
  45.     public void pause() {  
  46.                 // 在调用本线程的 Activity 的 onPause 里调用,以便 Activity 暂停时释放麦克风  
  47.         isRun = false;  
  48.     }  
  49.    
  50.     public void start() {  
  51.                 // 在调用本线程的 Activity 的 onResume 里调用,以便 Activity 恢复后继续获取麦克风输入音量  
  52.         if (!isRun) {  
  53.             super.start();  
  54.         }  
  55.     }  
  56. }  


转评:

原文中提到“平方和除以数据总长度,得到音量大小”,有些文章中提到这个音量值在不同的手机中表现得不一样,同样的发声,但出来的值相差很大。进而有通过一些计算,调整“音量”的算法,其中有两个,分别是:

1、计算了噪音,对音量进行调整:
value 的 值 控制 为 0 到 100 之间 0为最小 》= 100为最大!!
 int value = (int) (Math.abs((int)(v /(float)r)/10000) >> 1);

2、计算分贝值:

那个值应该是声音的振幅,并不是音量的大小,
声音的大小应该是用分贝为单位的吧,
double dB = 10*Math.log10(v/(double)r);

即:经傅立叶变化后得到的复数数组是个二维数组,实部和虚部的平方和取对数后乘以10就大致等于我们通常表示音量的分贝了。


这些是以前的数学知识,现在已经忘得一干二净了。。。。。

posted @ 2013-02-25 14:27 剑气纵横 阅读(46) 评论(0)  编辑

官方文档:http://developer.apple.com /library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

中文的好文章

http://www./thread-47004-1-1.html

http://www.cnblogs.com/haipingwu/archive/2011/03/18/1987962.html

iOS4 请求更多后台时间

http://blog.csdn.net/zhangao0086/article/details/6739266

 

在IOS后台执行是本文要介绍的内容,大多数应用程序进入后台状态不久后转入暂停状态。在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除。应用程序提供特定的服务,用户可以请求后台执行时间,以提供这些服务。

判断是否支持多线程

UIDevice* device = [UIDevice currentDevice];  
BOOL backgroundSupported = NO;  
if ([device respondsToSelector:@selector(isMultitaskingSupported)])  
backgroundSupported = device.multitaskingSupported; 

 

声明你需要的后台任务

Info.plist中添加UIBackgroundModes键值,它包含一个或多个string的值,包括

audio:在后台提供声音播放功能,包括音频流和播放视频时的声音

location:在后台可以保持用户的位置信息

voip:在后台使用VOIP功能

前 面的每个value让系统知道你的应用程序应该在适当的时候被唤醒。例如,一个应用程序,开始播放音乐,然后移动到后台仍然需要执行时间,以填补 音频输 出缓冲区。添加audio键用来告诉系统框架,需要继续播放音频,并且可以在合适的时间间隔下回调应用程序;如果应用程序不包括此项,任何音频播放 在移 到后台后将停止运行。

除了添加键值的方法,IOS还提供了两种途径使应用程序在后台工作:

Task completion—应用程序可以向系统申请额外的时间去完成给定的任务

Local notifications—应用程序可以预先安排时间执行local notifications 传递

实现长时间的后台任务

应用程序可以请求在后台运行以实现特殊的服务。这些应用程序并不连续的运行,但是会被系统框架在合适的时间唤醒,以实现这些服务

1.追踪用户位置:略

2.在后台播放音频:

1. //后台播放 

AVAudioSession *session= [AVAudioSession sharedInstance]; 

[session setActive:YES error:nil]; 

[session setCategory:AVAudioSessionCategoryPlayback error:nil]; 

 

2. 让后台可以处理多媒体的事件 

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 

Remote-control events originate as commands issued by headsets and external accessories that are intended to control multimedia presented by an application. To stop the reception of remote-control events, you must call endReceivingRemoteControlEvents. 

 

3.系统进入后台运行时,让程序可以运行一段时间。使用此方法争取一定的时间,在程序进入后台后处理一些事情。 

- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))handler 

This method lets your application continue to run for a period of time after it transitions to the background. 

your application could call this method to ensure that had enough time to transfer an important file to a remote server or at least attempt to make the transfer and note any errors. You should not use this method simply to keep your application running after it moves to the background. 

1. //后台播放
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];

2. 让后台可以处理多媒体的事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
Remote-control events originate as commands issued by headsets and external accessories that are intended to control multimedia presented by an application. To stop the reception of remote-control events, you must call endReceivingRemoteControlEvents.

3.系统进入后台运行时,让程序可以运行一段时间。使用此方法争取一定的时间,在程序进入后台后处理一些事情。
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void(^)(void))handler
This method lets your application continue to run for a period of time after it transitions to the background.
your application could call this method to ensure that had enough time to transfer an important file to a remote server or at least attempt to make the transfer and note any errors. You should not use this method simply to keep your application running after it moves to the background.


3.实现VOIP应用:

 

在后台完成有限长度的任务

官 方文档http://developer.apple.com/library/iOS/#documentation/iPhone /Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

在 被终止之前的任意时间,应用程序会调用beginBackgroundTaskWithExpirationHandler:方法让系统给出额外 的时间 来完成一些需要在后台长时间执行的任务。(UIApplication的backgroundTimeRemaining属性包含程序运行的总时 间)

可以使用task completion去保证那些比较重要但是需要长时间运行的程序不会由于用户切入后台而突然关闭。比如,你可以用这项功能来将用户的信息保存到disk上或者从网络下载一个重要的文件。有两种方式来初始化这样的任务:

1、将长时间运行的重要任务用beginBackgroundTaskWithExpirationHandler:和endBackgroundTask:包装。这样就在程序突然切入后台的时候保护了这些任务不被中断。

2、当你的应用程序委托applicationDidEnterBackground:方法被调用时再启动任务

中 的两个方法必须是一一对应的,endBackgroundTask:方法告诉系统任务已经完成,程序在此时可以被终止。由于应用程序只有有限的时 间去完 成后台任务,你必须在超时或系统将要终止这个程序之前调用这个方法。为了避免被终止,你也可以在一个任务开始的时候提供一个 expiration handler和endBackgroundTask:方法。(可以查看backgroundTimeRemaining属性来确 定还剩多少时间)。

一个程序可以同时提供多个任务,每当你 启动一个任务的时候,beginBackgroundTaskWithExpirationHandler: 方法将返回一个独一无二的handler去 识别这个任务。你必须在endBackgroundTask:方法中传递相同的handler来终止该任务。

Listing 4-2 Starting a background task at quit time 

- (void)applicationDidEnterBackground:(UIApplication *)application 

UIApplication* app= [UIApplication sharedApplication]; 

bgTask= [app beginBackgroundTaskWithExpirationHandler:^{ 

[app endBackgroundTask:bgTask]; 

bgTaskUIBackgroundTaskInvalid

}]; 

// Start the long-running task and return immediately. 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 

0), ^{ 

// Do the work associated with the task. 

[app endBackgroundTask:bgTask]; 

bgTaskUIBackgroundTaskInvalid

}); 

Listing 4-2 Starting a background task at quit time - (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication* app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task. [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); } 

上述例子中,bgTask变量是一个类的成员变量,存储着指向该后台任务标示的指针。

在expriation handler中,可以添加关闭任务所需的代码。尽管如此,加入的代码不能执行太长的时间,当expriation handler被调用的时候,该程序已经非常接近被关闭,所以只有极短的时间来清除状态信息并终止任务。

安排Local Notification的传递

UILocalNotification 类提供了一种方法来传递local notifications。和push notifications需要设置remote server不 同,local notifications 在程序中安排并在当前的设备上执行。满足如下条件可以使用该能力:

1、一个基于时间的程序,可以在将来特定的时间让程序post 一个alert,比如闹钟

2、一个在后台运行的程序,post 一个local notification去引起用户的注意

为 了安排local notification 的传递,需要创建一个UILocalNotification的实例,并设置它,使用 UIApplication类方法来安排它。Local notification对象包含了所要传递的类型(sound,alert,或者badge) 和时间何时呈现) 。UIApplication类方法提供选项去确定是立即传递还是在指定的时间传递

Listing 4-3 Scheduling an alarm notification 

- (void)scheduleAlarmForDate:(NSDate*)theDate 

UIApplication* app= [UIApplication sharedApplication]; 

NSArray* oldNotifications= [app scheduledLocalNotifications]; 

// Clear out the old notification before scheduling a new one. 

if ([oldNotifications count] >0) 

[app cancelAllLocalNotifications]; 

// Create a new notification. 

UILocalNotification* alarm= [[[UILocalNotification alloc] init] autorelease]; 

if (alarm) 

alarm.fireDatetheDate

alarm.timeZone= [NSTimeZone defaultTimeZone]; 

alarm.repeatInterval0

alarm.soundName= @"alarmsound.caf"; 

alarm.alertBody= @"Time to wake up!"; 

[app scheduleLocalNotification:alarm]; 

Listing 4-3 Scheduling an alarm notification - (void)scheduleAlarmForDate:(NSDate*)theDate { UIApplication* app = [UIApplication sharedApplication]; NSArray* oldNotifications = [app scheduledLocalNotifications]; // Clear out the old notification before scheduling a new one. if ([oldNotifications count] > 0) [app cancelAllLocalNotifications]; // Create a new notification. UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease]; if (alarm) { alarm.fireDate = theDate; alarm.timeZone = [NSTimeZone defaultTimeZone]; alarm.repeatInterval = 0; alarm.soundName = @"alarmsound.caf"; alarm.alertBody = @"Time to wake up!"; [app scheduleLocalNotification:alarm]; } } 

(可 以最多包含128 个 local notifications active at any given time, any of which can be configured to repeat at a specified interval.) 如果在调用该notification的时候,程序已经处于前台,那 么 application:didReceiveLocalNotification:方法将取而代之。

posted @ 2013-02-25 13:59 剑气纵横 阅读(34) 评论(0)  编辑

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多