iOS--(轉)WiFi傳輸數據


什么是Multipeer Connectivity?

在iOS7中,引入了一個全新的框架——Multipeer Connectivity(多點連接)。利用Multipeer Connectivity框架,即使在沒有連接到WiFi(WLAN)或移動網絡(xG)的情況下,距離較近的Apple設備(iMac/iPad/iPhone)之間可基於藍牙和WiFi(P2P WiFi)技術進行發現和連接實現近場通信。

Multipeer Connectivity擴充的功能與利用AirDrop傳輸文件非常類似,可以將其看作AirDrop不能直接使用的補償,代價是需要自己實現。

本Demo主要用到4個類:

MCBrowserViewController:MCBrowserViewController繼承自UIViewController,提供了基本的UI應用框架。

MCAdvertiserAssistant、MCAdvertiserAssistant為針對Advertiser封裝的管理助手,主要處理廣播信息。

MCSession:類似TCP鏈接中的socket。創建MCSession時,需指定自身MCPeerID,類似bind。

MCPeerID:類似sockaddr,用於標識連接的兩端endpoint,通常是昵稱或設備名稱。

1、簡單地建立一個界面,主要有連接和發送2個UIButton。

create_button.png

2、Multipeer Connectivity框架初始化這4個類。

#pragma mark - Wifi Sharing Methods
-(void)setUpMultipeer
{
    //  Setup peer ID
    self.myPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];

    //  Setup session
    self.mySession = [[MCSession alloc] initWithPeer:self.myPeerID];
    self.mySession.delegate = self;

    //  Setup BrowserViewController
    self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:@"chat" session:self.mySession];
    self.browserVC.delegate = self;

    //  Setup Advertiser
    self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat" discoveryInfo:nil session:self.mySession];
    [self.advertiser start];
}

-(void)showBrowserVC
{
    [self presentViewController:self.browserVC animated:YES completion:nil];
}

-(void)dismissBrowserVC
{
    [self.browserVC dismissViewControllerAnimated:YES completion:^(void){
        [self invokeAlertMethod:@"連接成功" Body:@"Both device connected successfully." Delegate:nil];
    }];
}

-(void)stopWifiSharing:(BOOL)isClear
{
    if(isClear && self.mySession != nil){
        [self.mySession disconnect];

        [self.mySession setDelegate:nil];

        self.mySession = nil;

        self.browserVC = nil;
    }
}

3、MCBrowserViewController代理方法

#pragma marks MCBrowserViewControllerDelegate
// 點擊完成
-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
    [self dismissBrowserVC];
    [marrReceiveData removeAllObjects];
}

// 點擊取消
-(void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController
{
    [self dismissBrowserVC];
}

4、MCSession代理方法

主要處理發送方傳遞的文件或者信息

// Received data from remote peer
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
    NSLog(@"data receiveddddd : %lu",(unsigned long)data.length);

    if (data.length > 0) {
        if (data.length < 2) {
            noOfDataSend++;
            NSLog(@"noofdatasend : %zd",noOfDataSend);
            NSLog(@"array count : %zd",marrFileData.count);
            if (noOfDataSend < ([marrFileData count])) {
                [self.mySession sendData:[marrFileData objectAtIndex:noOfDataSend] toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
            }else {
                [self.mySession sendData:[@"File Transfer Done" dataUsingEncoding:NSUTF8StringEncoding] toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
            }
        } else {
            if ([[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] isEqualToString:@"File Transfer Done"]) {
                [self appendFileData];
            }else {
                [self.mySession sendData:[@"1" dataUsingEncoding:NSUTF8StringEncoding] toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
                [marrReceiveData addObject:data];
            }
        }
    }
}

// Received a byte stream from remote peer
- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID
{
    NSLog(@"did receive stream");
}

// Start receiving a resource from remote peer
- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
{
    NSLog(@"start receiving");
}

// Finished receiving a resource from remote peer and saved the content in a temporary location - the app is responsible for moving the file to a permanent location within its sandbox
- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error
{
    NSLog(@"finish receiving resource");
}

-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
{
    NSLog(@"change state : %zd",state);
}

5、發送圖片(此Demo只是簡單地做了個收發圖片的Demo,此框架可實現的功能當然不止這么簡單。)

-(void)sendData
{
    [marrFileData removeAllObjects];

    NSData *sendData = UIImagePNGRepresentation([UIImage imageNamed:@"test2.png"]);
    NSUInteger length = [sendData length];
    NSUInteger chunkSize = 100 * 1024;
    NSUInteger offset = 0;
    do {
        NSUInteger thisChunkSize = length - offset > chunkSize ? chunkSize : length - offset;
        NSData* chunk = [NSData dataWithBytesNoCopy:(char *)[sendData bytes] + offset
                                             length:thisChunkSize
                                       freeWhenDone:NO];
        NSLog(@"chunk length : %lu",(unsigned long)chunk.length);

        [marrFileData addObject:[NSData dataWithData:chunk]];
        offset += thisChunkSize;
    } while (offset < length);

    noOfdata = [marrFileData count];
    noOfDataSend = 0;

    if ([marrFileData count] > 0) {
        [self.mySession sendData:[marrFileData objectAtIndex:noOfDataSend] toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataReliable error:nil];
    }
}

-(void)appendFileData
{
    NSMutableData *fileData = [NSMutableData data];

    for (int i = 0; i < [marrReceiveData count]; i++) {
        [fileData appendData:[marrReceiveData objectAtIndex:i]];
    }

    [fileData writeToFile:[NSString stringWithFormat:@"%@/Image.png", [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]] atomically:YES];

    UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:fileData], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        [self invokeAlertMethod:@"發送成功" Body:@"圖片已保存到手機相冊" Delegate:nil];
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM