SlideShare a Scribd company logo
1 of 19
Download to read offline
どこでもサクッと通信対戦
通信対戦
Multipeer Connectivity
Wi-Fi 
Bluetooth 
Multipeer Connectivity - iOS7
チャットアプリ
フレームワークのインポート 
// フレームワークをインポート 
#import <MultipeerConnectivity/MultipeerConnectivity.h> 
! 
! 
// デリゲートを設定 
@interface ViewController ()<MCBrowserViewControllerDelegate, 
MCSessionDelegate>
宣言 
@property (nonatomic, strong) MCPeerID *myPeerID; 
@property (nonatomic, strong) MCSession *mySession; 
@property (nonatomic, strong) MCBrowserViewController *browserVC; 
@property (nonatomic, strong) MCAdvertiserAssistant *advertiser;
- (void) setUpMultipeer{ 
! 
// Peerという端末のIDを設定 
self.myPeerID = [[MCPeerID alloc]initWithDisplayName:[UIDevice 
currentDevice].name]; 
// Sessionという送受信を行うための仕組みを確立 
self.mySession = [[MCSession alloc]initWithPeer:self.myPeerID]; 
self.mySession.delegate = self; 
// MCBrowserViewControllerという接続を自動的にしてくれるクラスを設定 
self.browserVC = [[MCBrowserViewController alloc] 
initWithServiceType:@"chat" session:self.mySession]; 
self.browserVC.delegate = self; 
// 自分の端末を他の端末に見つけてもらえるようにする 
self.advertiser = [[MCAdvertiserAssistant alloc] 
initWithServiceType:@"chat" discoveryInfo:nil 
session:self.mySession]; 
[self.advertiser start]; 
} 
セットアップ
MCBrowserViewController 
- (void) showBrowserVC{ 
! 
// MCBrowserViewControllerを表示 
[self presentViewController:self.browserVC animated:YES 
completion:nil]; 
! 
}
- (void) sendText{ 
// 入力したメッセージを代入 
NSString *message = self.textField.text; 
self.textField.text = @""; 
// メッセージをNSData型に変換 
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding]; 
// メッセージを送信 
NSError *error; 
[self.mySession sendData:data toPeers:[self.mySession 
connectedPeers] withMode:MCSessionSendDataUnreliable 
error:&error]; 
// receiveMessage:メソッドを呼び出す 
[self receiveMessage: message fromPeer: self.myPeerID]; 
! 
} 
メッセージを送信
メッセージを受信 
- (void)session:(MCSession *)session didReceiveData:(NSData *)data 
fromPeer:(MCPeerID *)peerID{ 
// NSDataをNSStringに変換 
NSString *message = [[NSString alloc] initWithData:data 
encoding:NSUTF8StringEncoding]; 
// receiveMessage:メソッドを実行 
dispatch_async(dispatch_get_main_queue(), ^{ 
! 
[self receiveMessage:message fromPeer:peerID]; 
! 
}); 
! 
}
メッセージを表示 
- (void) receiveMessage: (NSString *) message fromPeer: 
(MCPeerID *) peer{ 
NSString *finalText; 
if (peer == self.myPeerID)   // 自分のメッセージを受け取ったとき 
{ 
finalText = [NSString stringWithFormat:@"nMe: %@ n", 
message]; 
} 
! 
   else   // 相手のメッセージを受け取ったとき 
{ 
finalText = [NSString stringWithFormat:@"n%@: %@ n", 
peer.displayName, message]; 
} 
// メッセージを表示 
self.textView.text = [self.textView.text 
stringByAppendingString:finalText]; 
}
FireChat
End

More Related Content

Viewers also liked

GameKitを使ったBluetoothプログラミング
GameKitを使ったBluetoothプログラミングGameKitを使ったBluetoothプログラミング
GameKitを使ったBluetoothプログラミング
taiko19xx
 

Viewers also liked (6)

GameKitを使ったBluetoothプログラミング
GameKitを使ったBluetoothプログラミングGameKitを使ったBluetoothプログラミング
GameKitを使ったBluetoothプログラミング
 
Sprite kitでの横スクロールジャンプ アクションゲーム開発
Sprite kitでの横スクロールジャンプ アクションゲーム開発Sprite kitでの横スクロールジャンプ アクションゲーム開発
Sprite kitでの横スクロールジャンプ アクションゲーム開発
 
UnityのフリーライセンスでPC-Android通信を実装するまで
UnityのフリーライセンスでPC-Android通信を実装するまでUnityのフリーライセンスでPC-Android通信を実装するまで
UnityのフリーライセンスでPC-Android通信を実装するまで
 
Unityで通信してみよう!
Unityで通信してみよう!Unityで通信してみよう!
Unityで通信してみよう!
 
iOS7アプリ同士の近距離通信どれがいい?
iOS7アプリ同士の近距離通信どれがいい?iOS7アプリ同士の近距離通信どれがいい?
iOS7アプリ同士の近距離通信どれがいい?
 
Swift Code in Swift - 2日間でゲームを作ってみた
Swift Code in Swift - 2日間でゲームを作ってみたSwift Code in Swift - 2日間でゲームを作ってみた
Swift Code in Swift - 2日間でゲームを作ってみた
 

Similar to どこでもサクッと通信対戦

Tech talk salesforce mobile sdk
Tech talk   salesforce mobile sdkTech talk   salesforce mobile sdk
Tech talk salesforce mobile sdk
Kazuki Nakajima
 
勉強会force#3 iOSアプリ開発
勉強会force#3 iOSアプリ開発勉強会force#3 iOSアプリ開発
勉強会force#3 iOSアプリ開発
Kazuki Nakajima
 
Html5, Web Applications 2
Html5, Web Applications 2Html5, Web Applications 2
Html5, Web Applications 2
totty jp
 

Similar to どこでもサクッと通信対戦 (20)

Tech talk salesforce mobile sdk
Tech talk   salesforce mobile sdkTech talk   salesforce mobile sdk
Tech talk salesforce mobile sdk
 
Clrh 110716 wcfwf
Clrh 110716 wcfwfClrh 110716 wcfwf
Clrh 110716 wcfwf
 
iOS の動画アプリ開発に Xamarin を使ってみた @JXUG #2 East
iOS の動画アプリ開発に Xamarin を使ってみた @JXUG #2 EastiOS の動画アプリ開発に Xamarin を使ってみた @JXUG #2 East
iOS の動画アプリ開発に Xamarin を使ってみた @JXUG #2 East
 
開発チームもIaCやってみたい~VSOとDockerの組合せにチャレンジ~
開発チームもIaCやってみたい~VSOとDockerの組合せにチャレンジ~開発チームもIaCやってみたい~VSOとDockerの組合せにチャレンジ~
開発チームもIaCやってみたい~VSOとDockerの組合せにチャレンジ~
 
勉強会force#3 iOSアプリ開発
勉強会force#3 iOSアプリ開発勉強会force#3 iOSアプリ開発
勉強会force#3 iOSアプリ開発
 
リアルFacebookガジェットを作った(ロングバージョン)
リアルFacebookガジェットを作った(ロングバージョン)リアルFacebookガジェットを作った(ロングバージョン)
リアルFacebookガジェットを作った(ロングバージョン)
 
ニフティクラウド Mobile backendとio tの良い関係
ニフティクラウド Mobile backendとio tの良い関係ニフティクラウド Mobile backendとio tの良い関係
ニフティクラウド Mobile backendとio tの良い関係
 
Wolcome to swift
Wolcome to swiftWolcome to swift
Wolcome to swift
 
hbstudy37 doc
hbstudy37 dochbstudy37 doc
hbstudy37 doc
 
HTML5 APIを使ったデモとソースの解説
HTML5 APIを使ったデモとソースの解説HTML5 APIを使ったデモとソースの解説
HTML5 APIを使ったデモとソースの解説
 
WebRTC on Native App
WebRTC on Native AppWebRTC on Native App
WebRTC on Native App
 
Spring bootでweb セキュリティ(ログイン認証)編
Spring bootでweb セキュリティ(ログイン認証)編Spring bootでweb セキュリティ(ログイン認証)編
Spring bootでweb セキュリティ(ログイン認証)編
 
CleanArchitecture with AssemblyDefinition in unity
CleanArchitecture with AssemblyDefinition in unityCleanArchitecture with AssemblyDefinition in unity
CleanArchitecture with AssemblyDefinition in unity
 
Hbstudy41 auto scaling
Hbstudy41 auto scalingHbstudy41 auto scaling
Hbstudy41 auto scaling
 
"Up" with vagrant and docker
"Up" with vagrant and docker"Up" with vagrant and docker
"Up" with vagrant and docker
 
Html5, Web Applications 2
Html5, Web Applications 2Html5, Web Applications 2
Html5, Web Applications 2
 
20200516 selenium-meetup-winappdriver
20200516 selenium-meetup-winappdriver20200516 selenium-meetup-winappdriver
20200516 selenium-meetup-winappdriver
 
自宅k8s/vSphere入門
自宅k8s/vSphere入門自宅k8s/vSphere入門
自宅k8s/vSphere入門
 
Windows azure mobile services による mobile + cloud アプリケーション超高速開発
Windows azure mobile services による mobile + cloud アプリケーション超高速開発Windows azure mobile services による mobile + cloud アプリケーション超高速開発
Windows azure mobile services による mobile + cloud アプリケーション超高速開発
 
Container Storage Interface のすべて
Container Storage Interface のすべてContainer Storage Interface のすべて
Container Storage Interface のすべて
 

Recently uploaded

The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
koheioishi1
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
YukiTerazawa
 

Recently uploaded (8)

世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
世界を変えるクレーンを生み出そう! 高知エンジニアリングキャンプ2024プログラム
 
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
生成AIの回答内容の修正を課題としたレポートについて:お茶の水女子大学「授業・研究における生成系AIの活用事例」での講演資料
 
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
次世代機の製品コンセプトを描く ~未来の機械を創造してみよう~
 
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
2024年度 東京工業大学 工学院 機械系 大学院 修士課程 入試 説明会 資料
 
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
東京工業大学 環境・社会理工学院 建築学系 大学院入学入試・進学説明会2024_v2
 
The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024The_Five_Books_Overview_Presentation_2024
The_Five_Books_Overview_Presentation_2024
 
TokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentationTokyoTechGraduateExaminationPresentation
TokyoTechGraduateExaminationPresentation
 
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
ゲーム理論 BASIC 演習106 -価格の交渉ゲーム-#ゲーム理論 #gametheory #数学
 

どこでもサクッと通信対戦

  • 4. Wi-Fi Bluetooth Multipeer Connectivity - iOS7
  • 6.
  • 7. フレームワークのインポート // フレームワークをインポート #import <MultipeerConnectivity/MultipeerConnectivity.h> ! ! // デリゲートを設定 @interface ViewController ()<MCBrowserViewControllerDelegate, MCSessionDelegate>
  • 8. 宣言 @property (nonatomic, strong) MCPeerID *myPeerID; @property (nonatomic, strong) MCSession *mySession; @property (nonatomic, strong) MCBrowserViewController *browserVC; @property (nonatomic, strong) MCAdvertiserAssistant *advertiser;
  • 9. - (void) setUpMultipeer{ ! // Peerという端末のIDを設定 self.myPeerID = [[MCPeerID alloc]initWithDisplayName:[UIDevice currentDevice].name]; // Sessionという送受信を行うための仕組みを確立 self.mySession = [[MCSession alloc]initWithPeer:self.myPeerID]; self.mySession.delegate = self; // MCBrowserViewControllerという接続を自動的にしてくれるクラスを設定 self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:@"chat" session:self.mySession]; self.browserVC.delegate = self; // 自分の端末を他の端末に見つけてもらえるようにする self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat" discoveryInfo:nil session:self.mySession]; [self.advertiser start]; } セットアップ
  • 10. MCBrowserViewController - (void) showBrowserVC{ ! // MCBrowserViewControllerを表示 [self presentViewController:self.browserVC animated:YES completion:nil]; ! }
  • 11.
  • 12. - (void) sendText{ // 入力したメッセージを代入 NSString *message = self.textField.text; self.textField.text = @""; // メッセージをNSData型に変換 NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding]; // メッセージを送信 NSError *error; [self.mySession sendData:data toPeers:[self.mySession connectedPeers] withMode:MCSessionSendDataUnreliable error:&error]; // receiveMessage:メソッドを呼び出す [self receiveMessage: message fromPeer: self.myPeerID]; ! } メッセージを送信
  • 13. メッセージを受信 - (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{ // NSDataをNSStringに変換 NSString *message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // receiveMessage:メソッドを実行 dispatch_async(dispatch_get_main_queue(), ^{ ! [self receiveMessage:message fromPeer:peerID]; ! }); ! }
  • 14. メッセージを表示 - (void) receiveMessage: (NSString *) message fromPeer: (MCPeerID *) peer{ NSString *finalText; if (peer == self.myPeerID)   // 自分のメッセージを受け取ったとき { finalText = [NSString stringWithFormat:@"nMe: %@ n", message]; } !    else   // 相手のメッセージを受け取ったとき { finalText = [NSString stringWithFormat:@"n%@: %@ n", peer.displayName, message]; } // メッセージを表示 self.textView.text = [self.textView.text stringByAppendingString:finalText]; }
  • 15.
  • 17.
  • 18.
  • 19. End