#import <SystemConfiguration/CaptiveNetwork.h> html
+ (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); java
在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 python
註冊賬號---》開發----》發佈----》等待審覈----》上架 linux
發佈過程 android
1: 登陸到到開發者賬戶,再登陸到iTunes Connect 中 設置 要發佈版本的對應信息 ios
注意:設置的信息必定要和你發佈的程序對上 ,如:identifter,其餘看看就知道了 算法
2:發佈 數據庫
使用xcode 4發佈app 實例操做是本文介紹的內容,很少說,咱們直接進入話題。 編程
一、iOS Provisioning Portal 和iTunes Connect 沒有變,下載與安裝.mobileprovision文件與之前一下。 ubuntu
二、Xcode 4 整合相關的工具(All in One), 集成了Organizer與Application Loader,Xcode4多了Run/Build Scheme的概念,指之前的編譯的選項如 device/simulator, iOS version等。發佈應用時須要選擇iOS Device:
點擊 Edit Scheme... 以編輯Archive選項,它的默認 編譯配置爲Distribution 而且將歸檔顯示在Organizer中:
三、檢查 編譯配置文件:product name, info.plist, version, Target Device Family, iOS Deployment Target, Code Signing Identity etc. (這與Xcode 3.x一致)。
四、進入Product菜單->Archive,而後會自動彈出Organizer
Archive包是用來上傳到AppStore的
Xcode4讓你不須要關心編譯發佈的程序包在哪裏,只須要在Organizer中管理便可。
固然你能夠在XCode->Preferences->Locations tab配置程序的存儲位置。
五、在Organizer中選擇 Archive, 驗證與上傳。
這裏驗證的時候要檢查你在iTunes Connect中設置的信息
六、成功上傳App以後,Archive狀態會變爲 已提交。
方法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);
Android 上有一些頗有趣的應用,例如《吹裙子》、《吹氣球》之類的。利用的是實時獲取麥克風輸入音量,而後進行相應的處理。錄音過程當中怎樣得到聲音的大小呢?網上 也很多人問如何處理這個事情,也有一些解答,不過都沒有實際的代碼。簡單摸索了一下,寫了個小 Demo 試了試,果真能夠。給你們共享一下。
不解釋代碼了,你們看註釋。
轉評:
原文中提到「平方和除以數據總長度,獲得音量大小」,有些文章中提到這個音量值在不一樣的手機中表現得不同,一樣的發聲,但出來的值相差很大。進而有經過一些計算,調整「音量」的算法,其中有兩個,分別是:
一、計算了噪音,對音量進行調整:
value 的 值 控制 爲 0 到 100 之間 0爲最小 》= 100爲最大!!
int value = (int) (Math.abs((int)(v /(float)r)/10000) >> 1);
二、計算分貝值:
那個值應該是聲音的振幅,並非音量的大小,
聲音的大小應該是用分貝爲單位的吧,
double dB = 10*Math.log10(v/(double)r);
即:經傅立葉變化後獲得的複數數組是個二維數組,實部和虛部的平方和取對數後乘以10就大體等於咱們一般表示音量的分貝了。
這些是之前的數學知識,如今已經忘得一乾二淨了。。。。。
官方文檔:http://developer.apple.com /library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
中文的好文章
http://www.devdiv.com/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後臺執行是本文要介紹的內容,大多數應用程序進入後臺狀態不久後轉入暫停狀態。在這種狀態下,應用程序不執行任何代碼,並有可能在任意時候從內存中刪除。應用程序提供特定的服務,用戶能夠請求後臺執行時間,以提供這些服務。
判斷是否支持多線程
聲明你須要的後臺任務
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上或者從網絡下載一個重要的文件。有兩種方式來初始化這樣的任務:
一、將長時間運行的重要任務用beginBackgroundTaskWithExpirationHandler:和endBackgroundTask:包裝。這樣就在程序忽然切入後臺的時候保護了這些任務不被中斷。
二、當你的應用程序委託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];
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 在程序中安排並在當前的設備上執行。知足以下條件可使用該能力:
一、一個基於時間的程序,能夠在未來特定的時間讓程序post 一個alert,好比鬧鐘
二、一個在後臺運行的程序,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.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:方法將取而代之。