如何使用IOS實(shí)現(xiàn)WIFI傳輸
問題
業(yè)務(wù)場景上存在需要將手機(jī)里的文件、圖片傳遞給其他的設(shè)備,
不僅僅局限于傳書、資料啥的都有可能傳遞
方案
最base的方法:設(shè)備之間加個(gè)云,設(shè)備上傳資料到云,云同步資料到各個(gè)設(shè)備,適用于多設(shè)備之間,這個(gè)沒講的必要
如果是兩設(shè)備之間,忽略服務(wù)器,怎么搞?聯(lián)想到圖書App中的WiFi傳書,貌似沒云端概念的,怎么做到的?
上菜
采用框架GCDWebServer,通過CocoaPods引入
pod "GCDWebServer", "~> 3.0"
設(shè)置本地接收目錄,初始化Server并啟動(dòng)
override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) if let filepath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first as NSString? { let path = filepath.appendingPathComponent("transfer") if !FileManager.default.fileExists(atPath: path) { do { try FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: false, attributes: nil) } catch { print(error) } } webServer = GCDWebUploader(uploadDirectory: path) webServer?.delegate = self webServer?.allowHiddenItems = true webServer?.allowedFileExtensions = ["doc", "docx", "xls", "xlsx", "txt", "pdf", "jpeg", "jpg"] webServer?.title = "善齋工具" webServer?.prologue = "歡飲使用善齋工具的WIFI管理平臺" webServer?.epilogue = "善齋書屋制作" if webServer?.start() == true, let address = IPHelper.deviceIPAdress(), address.count > 0, let port = webServer?.port { ipLb.text = "1.確保設(shè)備在同一局域網(wǎng) \n2.上傳時(shí)勿關(guān)閉該頁面 \n3.請網(wǎng)頁中輸入該地址 \nhttp://\(address):\(port)/" } else { ipLb.text = "GCDWebServer not running!" } } }
局域網(wǎng)內(nèi)獲取本機(jī)的ip地址,并設(shè)置其他設(shè)備訪問鏈接
#import <ifaddrs.h> #import <arpa/inet.h> #import <net/if.h> @implementation IPHelper + (NSString *)deviceIPAdress { NSString *address = @""; struct ifaddrs *interfaces = NULL; struct ifaddrs *temp_addr = NULL; int success = 0; success = getifaddrs(&interfaces); if (success == 0) { // 0 表示獲取成功 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 from C String address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } freeifaddrs(interfaces); return address; } #define IOS_CELLULAR @"pdp_ip0" #define IOS_WIFI @"en0" #define IOS_VPN @"utun0" #define IP_ADDR_IPv4 @"ipv4" #define IP_ADDR_IPv6 @"ipv6" #pragma mark - 獲取設(shè)備當(dāng)前網(wǎng)絡(luò)IP地址 + (NSString *)getIPAddress:(BOOL)preferIPv4 { NSArray *searchArray = preferIPv4 ? @[ IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] : @[ IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ; NSDictionary *addresses = [self getIPAddresses]; NSLog(@"addresses: %@", addresses); __block NSString *address; [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) { address = addresses[key]; //篩選出IP地址格式 if([self isValidatIP:address]) *stop = YES; } ]; return address ? address : @"0.0.0.0"; } + (BOOL)isValidatIP:(NSString *)ipAddress { if (ipAddress.length == 0) { return NO; } NSString *urlRegEx = @"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; NSError *error; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlRegEx options:0 error:&error]; if (regex != nil) { NSTextCheckingResult *firstMatch=[regex firstMatchInString:ipAddress options:0 range:NSMakeRange(0, [ipAddress length])]; if (firstMatch) { NSRange resultRange = [firstMatch rangeAtIndex:0]; NSString *result=[ipAddress substringWithRange:resultRange]; //輸出結(jié)果 NSLog(@"%@",result); return YES; } } return NO; } + (NSDictionary *)getIPAddresses { NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8]; // retrieve the current interfaces - returns 0 on success struct ifaddrs *interfaces; if(!getifaddrs(&interfaces)) { // Loop through linked list of interfaces struct ifaddrs *interface; for(interface=interfaces; interface; interface=interface->ifa_next) { if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) { continue; // deeply nested code harder to read } const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr; char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ]; if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) { NSString *name = [NSString stringWithUTF8String:interface->ifa_name]; NSString *type; if(addr->sin_family == AF_INET) { if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) { type = IP_ADDR_IPv4; } } else { const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr; if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) { type = IP_ADDR_IPv6; } } if(type) { NSString *key = [NSString stringWithFormat:@"%@/%@", name, type]; addresses[key] = [NSString stringWithUTF8String:addrBuf]; } } } // Free memory freeifaddrs(interfaces); } return [addresses count] ? addresses : nil; }
在其他設(shè)備中訪問該地址即可
let address = IPHelper.deviceIPAdress() let port = webServer?.port http://\(address):\(port)/
備注:
- 確保設(shè)備在同一局域網(wǎng)
- 上傳時(shí)勿關(guān)閉該頁面
Game Over
局域網(wǎng)中,設(shè)備作為server,其他設(shè)備作為client,簡單的HTTP方式上傳文件到server,初始配置的路徑即為server接收后存放文件的路徑
以上就是如何使用IOS實(shí)現(xiàn)WIFI傳輸?shù)脑敿?xì)內(nèi)容,更多關(guān)于IOS實(shí)現(xiàn)WIFI傳輸?shù)馁Y料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解iOS的Core Animation框架中的CATransform3D圖形變換
CATransform3D一般用于操作view的layer的,是Core Animation的結(jié)構(gòu)體,可以用來做比較復(fù)雜的3D操作,這里我們就帶大家來詳解iOS的Core Animation框架中的CATransform3D圖形變換2016-07-07iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Flutter之TabBarView組件項(xiàng)目實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Flutter之TabBarView組件項(xiàng)目實(shí)戰(zhàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10React Native搭建iOS開發(fā)環(huán)境
React Native的門檻不管是對于前端開發(fā)者還是移動(dòng)端開發(fā)者來說都是很高的,既要懂原生又要懂js,技術(shù)棧是相當(dāng)長的。但是沒有關(guān)系,下面我們一步步來學(xué)習(xí),慢慢成長吧!2016-09-09iOS App開發(fā)中UITextField組件的常用屬性小結(jié)
這篇文章主要介紹了iOS App開發(fā)中UITextField組件的常用屬性小結(jié),文中還介紹了UITextField隱藏鍵盤及為內(nèi)容增加校驗(yàn)的兩個(gè)使用技巧,需要的朋友可以參考下2016-04-04iOS手勢識別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢依賴,自定義手勢)
這篇文章主要介紹了iOS手勢識別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢依賴,自定義手勢),具有一定的參考價(jià)值,有需要的可以參考一下。2016-11-11