回答編集履歴

1

コード追加

2016/10/19 15:11

投稿

toki_td
toki_td

スコア2850

test CHANGED
@@ -1,3 +1,97 @@
1
1
  iOSは許可された特定の状態のアプリ以外はプロセスが完全に停止します。
2
2
 
3
3
  Appleが規定する動作(アプリがオーディオを再生しているとかGPSを取得している等)以外でバックグラウンドで動くことはできないのでそういったログ行為はできないと思います。
4
+
5
+
6
+
7
+ ```Objective-C
8
+
9
+
10
+
11
+ - (BOOL)application:(UIApplication *)application
12
+
13
+ didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14
+
15
+ {
16
+
17
+ _player = [MPMusicPlayerController systemMusicPlayer];
18
+
19
+
20
+
21
+ [_player beginGeneratingPlaybackNotifications];
22
+
23
+
24
+
25
+ [[NSNotificationCenter defaultCenter] addObserver:self
26
+
27
+ selector:@selector(onPlayItemChanged:)
28
+
29
+ name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
30
+
31
+ object:nil];
32
+
33
+
34
+
35
+ [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
36
+
37
+
38
+
39
+ return YES;
40
+
41
+ }
42
+
43
+
44
+
45
+
46
+
47
+ - (void)onPlayItemChanged:(NSNotification*)notify
48
+
49
+ {
50
+
51
+ MPMediaPropertyPredicate* pred;
52
+
53
+ NSNumber* persistentId = notify.userInfo[@"MPMusicPlayerControllerNowPlayingItemPersistentIDKey"];
54
+
55
+
56
+
57
+ pred = [MPMediaPropertyPredicate predicateWithValue:persistentId
58
+
59
+ forProperty:MPMediaItemPropertyPersistentID];
60
+
61
+
62
+
63
+ MPMediaQuery* query;
64
+
65
+
66
+
67
+ query = [[MPMediaQuery alloc] initWithFilterPredicates:[NSSet setWithObject:pred]];
68
+
69
+
70
+
71
+ for(MPMediaItem* item in query.items){
72
+
73
+ NSLog(@"%s '%@' '%@' '%@' %@", __func__, item.title, item.artist, item.albumTitle, item.lastPlayedDate);
74
+
75
+ }
76
+
77
+ }
78
+
79
+
80
+
81
+ - (void)application:(UIApplication *)application
82
+
83
+ performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
84
+
85
+ {
86
+
87
+ NSLog(@"%s", __func__);
88
+
89
+ completionHandler(UIBackgroundFetchResultNewData);
90
+
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+ ```